3 namespace Tests\Wallabag\CoreBundle\Helper
;
5 use Monolog\Handler\TestHandler
;
7 use PHPUnit\Framework\TestCase
;
8 use Wallabag\CoreBundle\Entity\Config
;
9 use Wallabag\CoreBundle\Entity\Entry
;
10 use Wallabag\CoreBundle\Entity\Tag
;
11 use Wallabag\CoreBundle\Entity\TaggingRule
;
12 use Wallabag\CoreBundle\Helper\RuleBasedTagger
;
13 use Wallabag\UserBundle\Entity\User
;
15 class RuleBasedTaggerTest
extends TestCase
18 private $tagRepository;
19 private $entryRepository;
24 public function setUp()
26 $this->rulerz
= $this->getRulerZMock();
27 $this->tagRepository
= $this->getTagRepositoryMock();
28 $this->entryRepository
= $this->getEntryRepositoryMock();
29 $this->logger
= $this->getLogger();
30 $this->handler
= new TestHandler();
31 $this->logger
->pushHandler($this->handler
);
33 $this->tagger
= new RuleBasedTagger($this->rulerz
, $this->tagRepository
, $this->entryRepository
, $this->logger
);
36 public function testTagWithNoRule()
38 $entry = new Entry($this->getUser());
40 $this->tagger
->tag($entry);
42 $this->assertTrue($entry->getTags()->isEmpty());
43 $records = $this->handler
->getRecords();
44 $this->assertCount(0, $records);
47 public function testTagWithNoMatchingRule()
49 $taggingRule = $this->getTaggingRule('rule as string', ['foo', 'bar']);
50 $user = $this->getUser([$taggingRule]);
51 $entry = new Entry($user);
54 ->expects($this->once())
56 ->with($entry, 'rule as string')
59 $this->tagger
->tag($entry);
61 $this->assertTrue($entry->getTags()->isEmpty());
62 $records = $this->handler
->getRecords();
63 $this->assertCount(0, $records);
66 public function testTagWithAMatchingRule()
68 $taggingRule = $this->getTaggingRule('rule as string', ['foo', 'bar']);
69 $user = $this->getUser([$taggingRule]);
70 $entry = new Entry($user);
73 ->expects($this->once())
75 ->with($entry, 'rule as string')
78 $this->tagger
->tag($entry);
80 $this->assertFalse($entry->getTags()->isEmpty());
82 $tags = $entry->getTags();
83 $this->assertSame('foo', $tags[0]->getLabel());
84 $this->assertSame('bar', $tags[1]->getLabel());
86 $records = $this->handler
->getRecords();
87 $this->assertCount(1, $records);
90 public function testTagWithAMixOfMatchingRules()
92 $taggingRule = $this->getTaggingRule('bla bla', ['hey']);
93 $otherTaggingRule = $this->getTaggingRule('rule as string', ['foo']);
95 $user = $this->getUser([$taggingRule, $otherTaggingRule]);
96 $entry = new Entry($user);
100 ->will($this->onConsecutiveCalls(false, true));
102 $this->tagger
->tag($entry);
104 $this->assertFalse($entry->getTags()->isEmpty());
106 $tags = $entry->getTags();
107 $this->assertSame('foo', $tags[0]->getLabel());
108 $records = $this->handler
->getRecords();
109 $this->assertCount(1, $records);
112 public function testWhenTheTagExists()
114 $taggingRule = $this->getTaggingRule('rule as string', ['foo']);
115 $user = $this->getUser([$taggingRule]);
116 $entry = new Entry($user);
120 ->expects($this->once())
121 ->method('satisfies')
122 ->with($entry, 'rule as string')
126 ->expects($this->once())
127 // the method `findOneByLabel` doesn't exist, EntityRepository will then call `_call` method
128 // to magically call the `findOneBy` with ['label' => 'foo']
132 $this->tagger
->tag($entry);
134 $this->assertFalse($entry->getTags()->isEmpty());
136 $tags = $entry->getTags();
137 $this->assertSame($tag, $tags[0]);
138 $records = $this->handler
->getRecords();
139 $this->assertCount(1, $records);
142 public function testSameTagWithDifferentfMatchingRules()
144 $taggingRule = $this->getTaggingRule('bla bla', ['hey']);
145 $otherTaggingRule = $this->getTaggingRule('rule as string', ['hey']);
147 $user = $this->getUser([$taggingRule, $otherTaggingRule]);
148 $entry = new Entry($user);
151 ->method('satisfies')
154 $this->tagger
->tag($entry);
156 $this->assertFalse($entry->getTags()->isEmpty());
158 $tags = $entry->getTags();
159 $this->assertCount(1, $tags);
160 $records = $this->handler
->getRecords();
161 $this->assertCount(2, $records);
164 public function testTagAllEntriesForAUser()
166 $taggingRule = $this->getTaggingRule('bla bla', ['hey']);
168 $user = $this->getUser([$taggingRule]);
171 ->method('satisfies')
176 ->willReturn([new Entry($user), new Entry($user)]);
178 $entries = $this->tagger
->tagAllForUser($user);
180 $this->assertCount(2, $entries);
182 foreach ($entries as $entry) {
183 $tags = $entry->getTags();
185 $this->assertCount(1, $tags);
186 $this->assertSame('hey', $tags[0]->getLabel());
190 private function getUser(array $taggingRules = [])
193 $config = new Config($user);
195 $user->setConfig($config);
197 foreach ($taggingRules as $rule) {
198 $config->addTaggingRule($rule);
204 private function getTaggingRule($rule, array $tags)
206 $taggingRule = new TaggingRule();
207 $taggingRule->setRule($rule);
208 $taggingRule->setTags($tags);
213 private function getRulerZMock()
215 return $this->getMockBuilder('RulerZ\RulerZ')
216 ->disableOriginalConstructor()
220 private function getTagRepositoryMock()
222 return $this->getMockBuilder('Wallabag\CoreBundle\Repository\TagRepository')
223 ->disableOriginalConstructor()
227 private function getEntryRepositoryMock()
229 return $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
230 ->disableOriginalConstructor()
234 private function getLogger()
236 return new Logger('foo');