3 namespace Tests\Wallabag\CoreBundle\Helper
;
5 use Psr\Log\NullLogger
;
6 use Wallabag\CoreBundle\Entity\Config
;
7 use Wallabag\CoreBundle\Entity\Entry
;
8 use Wallabag\CoreBundle\Entity\Tag
;
9 use Wallabag\CoreBundle\Entity\TaggingRule
;
10 use Wallabag\CoreBundle\Helper\RuleBasedTagger
;
11 use Wallabag\UserBundle\Entity\User
;
13 class RuleBasedTaggerTest
extends \PHPUnit_Framework_TestCase
16 private $tagRepository;
17 private $entryRepository;
20 public function setUp()
22 $this->rulerz
= $this->getRulerZMock();
23 $this->tagRepository
= $this->getTagRepositoryMock();
24 $this->entryRepository
= $this->getEntryRepositoryMock();
26 $this->tagger
= new RuleBasedTagger($this->rulerz
, $this->tagRepository
, $this->entryRepository
, $this->getLogger());
29 public function testTagWithNoRule()
31 $entry = new Entry($this->getUser());
33 $this->tagger
->tag($entry);
35 $this->assertTrue($entry->getTags()->isEmpty());
38 public function testTagWithNoMatchingRule()
40 $taggingRule = $this->getTaggingRule('rule as string', ['foo', 'bar']);
41 $user = $this->getUser([$taggingRule]);
42 $entry = new Entry($user);
45 ->expects($this->once())
47 ->with($entry, 'rule as string')
50 $this->tagger
->tag($entry);
52 $this->assertTrue($entry->getTags()->isEmpty());
55 public function testTagWithAMatchingRule()
57 $taggingRule = $this->getTaggingRule('rule as string', ['foo', 'bar']);
58 $user = $this->getUser([$taggingRule]);
59 $entry = new Entry($user);
62 ->expects($this->once())
64 ->with($entry, 'rule as string')
67 $this->tagger
->tag($entry);
69 $this->assertFalse($entry->getTags()->isEmpty());
71 $tags = $entry->getTags();
72 $this->assertSame('foo', $tags[0]->getLabel());
73 $this->assertSame('bar', $tags[1]->getLabel());
76 public function testTagWithAMixOfMatchingRules()
78 $taggingRule = $this->getTaggingRule('bla bla', ['hey']);
79 $otherTaggingRule = $this->getTaggingRule('rule as string', ['foo']);
81 $user = $this->getUser([$taggingRule, $otherTaggingRule]);
82 $entry = new Entry($user);
86 ->will($this->onConsecutiveCalls(false, true));
88 $this->tagger
->tag($entry);
90 $this->assertFalse($entry->getTags()->isEmpty());
92 $tags = $entry->getTags();
93 $this->assertSame('foo', $tags[0]->getLabel());
96 public function testWhenTheTagExists()
98 $taggingRule = $this->getTaggingRule('rule as string', ['foo']);
99 $user = $this->getUser([$taggingRule]);
100 $entry = new Entry($user);
104 ->expects($this->once())
105 ->method('satisfies')
106 ->with($entry, 'rule as string')
110 ->expects($this->once())
111 // the method `findOneByLabel` doesn't exist, EntityRepository will then call `_call` method
112 // to magically call the `findOneBy` with ['label' => 'foo']
116 $this->tagger
->tag($entry);
118 $this->assertFalse($entry->getTags()->isEmpty());
120 $tags = $entry->getTags();
121 $this->assertSame($tag, $tags[0]);
124 public function testSameTagWithDifferentfMatchingRules()
126 $taggingRule = $this->getTaggingRule('bla bla', ['hey']);
127 $otherTaggingRule = $this->getTaggingRule('rule as string', ['hey']);
129 $user = $this->getUser([$taggingRule, $otherTaggingRule]);
130 $entry = new Entry($user);
133 ->method('satisfies')
136 $this->tagger
->tag($entry);
138 $this->assertFalse($entry->getTags()->isEmpty());
140 $tags = $entry->getTags();
141 $this->assertCount(1, $tags);
144 public function testTagAllEntriesForAUser()
146 $taggingRule = $this->getTaggingRule('bla bla', ['hey']);
148 $user = $this->getUser([$taggingRule]);
151 ->method('satisfies')
156 ->willReturn([new Entry($user), new Entry($user)]);
158 $entries = $this->tagger
->tagAllForUser($user);
160 $this->assertCount(2, $entries);
162 foreach ($entries as $entry) {
163 $tags = $entry->getTags();
165 $this->assertCount(1, $tags);
166 $this->assertEquals('hey', $tags[0]->getLabel());
170 private function getUser(array $taggingRules = [])
173 $config = new Config($user);
175 $user->setConfig($config);
177 foreach ($taggingRules as $rule) {
178 $config->addTaggingRule($rule);
184 private function getTaggingRule($rule, array $tags)
186 $taggingRule = new TaggingRule();
187 $taggingRule->setRule($rule);
188 $taggingRule->setTags($tags);
193 private function getRulerZMock()
195 return $this->getMockBuilder('RulerZ\RulerZ')
196 ->disableOriginalConstructor()
200 private function getTagRepositoryMock()
202 return $this->getMockBuilder('Wallabag\CoreBundle\Repository\TagRepository')
203 ->disableOriginalConstructor()
207 private function getEntryRepositoryMock()
209 return $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
210 ->disableOriginalConstructor()
214 private function getLogger()
216 return new NullLogger();