use Wallabag\CoreBundle\Entity\Entry;
use Wallabag\CoreBundle\Entity\Tag;
use Wallabag\CoreBundle\Entity\TaggingRule;
-use Wallabag\UserBundle\Entity\User;
use Wallabag\CoreBundle\Helper\RuleBasedTagger;
+use Wallabag\UserBundle\Entity\User;
class RuleBasedTaggerTest extends \PHPUnit_Framework_TestCase
{
public function testTagWithNoMatchingRule()
{
- $taggingRule = $this->getTaggingRule('rule as string', array('foo', 'bar'));
+ $taggingRule = $this->getTaggingRule('rule as string', ['foo', 'bar']);
$user = $this->getUser([$taggingRule]);
$entry = new Entry($user);
public function testTagWithAMatchingRule()
{
- $taggingRule = $this->getTaggingRule('rule as string', array('foo', 'bar'));
+ $taggingRule = $this->getTaggingRule('rule as string', ['foo', 'bar']);
$user = $this->getUser([$taggingRule]);
$entry = new Entry($user);
$tags = $entry->getTags();
$this->assertSame('foo', $tags[0]->getLabel());
- $this->assertSame($user, $tags[0]->getUser());
$this->assertSame('bar', $tags[1]->getLabel());
- $this->assertSame($user, $tags[1]->getUser());
}
public function testTagWithAMixOfMatchingRules()
{
- $taggingRule = $this->getTaggingRule('bla bla', array('hey'));
- $otherTaggingRule = $this->getTaggingRule('rule as string', array('foo'));
+ $taggingRule = $this->getTaggingRule('bla bla', ['hey']);
+ $otherTaggingRule = $this->getTaggingRule('rule as string', ['foo']);
$user = $this->getUser([$taggingRule, $otherTaggingRule]);
$entry = new Entry($user);
$tags = $entry->getTags();
$this->assertSame('foo', $tags[0]->getLabel());
- $this->assertSame($user, $tags[0]->getUser());
}
public function testWhenTheTagExists()
{
- $taggingRule = $this->getTaggingRule('rule as string', array('foo'));
+ $taggingRule = $this->getTaggingRule('rule as string', ['foo']);
$user = $this->getUser([$taggingRule]);
$entry = new Entry($user);
- $tag = new Tag($user);
+ $tag = new Tag();
$this->rulerz
->expects($this->once())
$this->tagRepository
->expects($this->once())
- ->method('findOneByLabelAndUserId')
+ // the method `findOneByLabel` doesn't exist, EntityRepository will then call `_call` method
+ // to magically call the `findOneBy` with ['label' => 'foo']
+ ->method('__call')
->willReturn($tag);
$this->tagger->tag($entry);
public function testSameTagWithDifferentfMatchingRules()
{
- $taggingRule = $this->getTaggingRule('bla bla', array('hey'));
- $otherTaggingRule = $this->getTaggingRule('rule as string', array('hey'));
+ $taggingRule = $this->getTaggingRule('bla bla', ['hey']);
+ $otherTaggingRule = $this->getTaggingRule('rule as string', ['hey']);
$user = $this->getUser([$taggingRule, $otherTaggingRule]);
$entry = new Entry($user);
$this->assertCount(1, $tags);
}
+ public function testTagAllEntriesForAUser()
+ {
+ $taggingRule = $this->getTaggingRule('bla bla', ['hey']);
+
+ $user = $this->getUser([$taggingRule]);
+
+ $this->rulerz
+ ->method('satisfies')
+ ->willReturn(true);
+
+ $this->rulerz
+ ->method('filter')
+ ->willReturn([new Entry($user), new Entry($user)]);
+
+ $entries = $this->tagger->tagAllForUser($user);
+
+ $this->assertCount(2, $entries);
+
+ foreach ($entries as $entry) {
+ $tags = $entry->getTags();
+
+ $this->assertCount(1, $tags);
+ $this->assertEquals('hey', $tags[0]->getLabel());
+ }
+ }
+
private function getUser(array $taggingRules = [])
{
$user = new User();