]>
Commit | Line | Data |
---|---|---|
5a166c5c KG |
1 | <?php |
2 | ||
23634d5d | 3 | namespace Tests\Wallabag\CoreBundle\Helper; |
5a166c5c | 4 | |
a1146b65 NL |
5 | use Monolog\Handler\TestHandler; |
6 | use Monolog\Logger; | |
bd91bd5c | 7 | use PHPUnit\Framework\TestCase; |
5a166c5c KG |
8 | use Wallabag\CoreBundle\Entity\Config; |
9 | use Wallabag\CoreBundle\Entity\Entry; | |
10 | use Wallabag\CoreBundle\Entity\Tag; | |
11 | use Wallabag\CoreBundle\Entity\TaggingRule; | |
5a166c5c | 12 | use Wallabag\CoreBundle\Helper\RuleBasedTagger; |
619cc453 | 13 | use Wallabag\UserBundle\Entity\User; |
5a166c5c | 14 | |
bd91bd5c | 15 | class RuleBasedTaggerTest extends TestCase |
5a166c5c KG |
16 | { |
17 | private $rulerz; | |
625acf33 KG |
18 | private $tagRepository; |
19 | private $entryRepository; | |
5a166c5c | 20 | private $tagger; |
a1146b65 NL |
21 | private $logger; |
22 | private $handler; | |
5a166c5c KG |
23 | |
24 | public function setUp() | |
25 | { | |
347fa6be NL |
26 | $this->rulerz = $this->getRulerZMock(); |
27 | $this->tagRepository = $this->getTagRepositoryMock(); | |
625acf33 | 28 | $this->entryRepository = $this->getEntryRepositoryMock(); |
a1146b65 NL |
29 | $this->logger = $this->getLogger(); |
30 | $this->handler = new TestHandler(); | |
31 | $this->logger->pushHandler($this->handler); | |
5a166c5c | 32 | |
a1146b65 | 33 | $this->tagger = new RuleBasedTagger($this->rulerz, $this->tagRepository, $this->entryRepository, $this->logger); |
5a166c5c KG |
34 | } |
35 | ||
36 | public function testTagWithNoRule() | |
37 | { | |
38 | $entry = new Entry($this->getUser()); | |
39 | ||
40 | $this->tagger->tag($entry); | |
41 | ||
42 | $this->assertTrue($entry->getTags()->isEmpty()); | |
a1146b65 NL |
43 | $records = $this->handler->getRecords(); |
44 | $this->assertCount(0, $records); | |
5a166c5c KG |
45 | } |
46 | ||
47 | public function testTagWithNoMatchingRule() | |
48 | { | |
4094ea47 | 49 | $taggingRule = $this->getTaggingRule('rule as string', ['foo', 'bar']); |
347fa6be NL |
50 | $user = $this->getUser([$taggingRule]); |
51 | $entry = new Entry($user); | |
5a166c5c KG |
52 | |
53 | $this->rulerz | |
54 | ->expects($this->once()) | |
55 | ->method('satisfies') | |
56 | ->with($entry, 'rule as string') | |
57 | ->willReturn(false); | |
58 | ||
59 | $this->tagger->tag($entry); | |
60 | ||
61 | $this->assertTrue($entry->getTags()->isEmpty()); | |
a1146b65 NL |
62 | $records = $this->handler->getRecords(); |
63 | $this->assertCount(0, $records); | |
5a166c5c KG |
64 | } |
65 | ||
66 | public function testTagWithAMatchingRule() | |
67 | { | |
4094ea47 | 68 | $taggingRule = $this->getTaggingRule('rule as string', ['foo', 'bar']); |
347fa6be NL |
69 | $user = $this->getUser([$taggingRule]); |
70 | $entry = new Entry($user); | |
5a166c5c KG |
71 | |
72 | $this->rulerz | |
73 | ->expects($this->once()) | |
74 | ->method('satisfies') | |
75 | ->with($entry, 'rule as string') | |
76 | ->willReturn(true); | |
77 | ||
78 | $this->tagger->tag($entry); | |
79 | ||
80 | $this->assertFalse($entry->getTags()->isEmpty()); | |
81 | ||
82 | $tags = $entry->getTags(); | |
83 | $this->assertSame('foo', $tags[0]->getLabel()); | |
5a166c5c | 84 | $this->assertSame('bar', $tags[1]->getLabel()); |
a1146b65 NL |
85 | |
86 | $records = $this->handler->getRecords(); | |
87 | $this->assertCount(1, $records); | |
5a166c5c KG |
88 | } |
89 | ||
90 | public function testTagWithAMixOfMatchingRules() | |
91 | { | |
4094ea47 JB |
92 | $taggingRule = $this->getTaggingRule('bla bla', ['hey']); |
93 | $otherTaggingRule = $this->getTaggingRule('rule as string', ['foo']); | |
5a166c5c | 94 | |
347fa6be | 95 | $user = $this->getUser([$taggingRule, $otherTaggingRule]); |
5a166c5c KG |
96 | $entry = new Entry($user); |
97 | ||
98 | $this->rulerz | |
99 | ->method('satisfies') | |
100 | ->will($this->onConsecutiveCalls(false, true)); | |
101 | ||
102 | $this->tagger->tag($entry); | |
103 | ||
104 | $this->assertFalse($entry->getTags()->isEmpty()); | |
105 | ||
106 | $tags = $entry->getTags(); | |
107 | $this->assertSame('foo', $tags[0]->getLabel()); | |
a1146b65 NL |
108 | $records = $this->handler->getRecords(); |
109 | $this->assertCount(1, $records); | |
5a166c5c KG |
110 | } |
111 | ||
112 | public function testWhenTheTagExists() | |
113 | { | |
4094ea47 | 114 | $taggingRule = $this->getTaggingRule('rule as string', ['foo']); |
347fa6be NL |
115 | $user = $this->getUser([$taggingRule]); |
116 | $entry = new Entry($user); | |
fc732227 | 117 | $tag = new Tag(); |
5a166c5c KG |
118 | |
119 | $this->rulerz | |
120 | ->expects($this->once()) | |
121 | ->method('satisfies') | |
122 | ->with($entry, 'rule as string') | |
123 | ->willReturn(true); | |
124 | ||
625acf33 | 125 | $this->tagRepository |
5a166c5c | 126 | ->expects($this->once()) |
fc732227 JB |
127 | // the method `findOneByLabel` doesn't exist, EntityRepository will then call `_call` method |
128 | // to magically call the `findOneBy` with ['label' => 'foo'] | |
129 | ->method('__call') | |
5a166c5c KG |
130 | ->willReturn($tag); |
131 | ||
132 | $this->tagger->tag($entry); | |
133 | ||
134 | $this->assertFalse($entry->getTags()->isEmpty()); | |
135 | ||
136 | $tags = $entry->getTags(); | |
1bb1939a | 137 | $this->assertSame($tag, $tags[0]); |
a1146b65 NL |
138 | $records = $this->handler->getRecords(); |
139 | $this->assertCount(1, $records); | |
5a166c5c KG |
140 | } |
141 | ||
fc031e57 JB |
142 | public function testSameTagWithDifferentfMatchingRules() |
143 | { | |
4094ea47 JB |
144 | $taggingRule = $this->getTaggingRule('bla bla', ['hey']); |
145 | $otherTaggingRule = $this->getTaggingRule('rule as string', ['hey']); | |
fc031e57 JB |
146 | |
147 | $user = $this->getUser([$taggingRule, $otherTaggingRule]); | |
148 | $entry = new Entry($user); | |
149 | ||
150 | $this->rulerz | |
151 | ->method('satisfies') | |
152 | ->willReturn(true); | |
153 | ||
154 | $this->tagger->tag($entry); | |
155 | ||
156 | $this->assertFalse($entry->getTags()->isEmpty()); | |
157 | ||
158 | $tags = $entry->getTags(); | |
159 | $this->assertCount(1, $tags); | |
a1146b65 NL |
160 | $records = $this->handler->getRecords(); |
161 | $this->assertCount(2, $records); | |
fc031e57 JB |
162 | } |
163 | ||
e9fa8c40 JB |
164 | public function testTagAllEntriesForAUser() |
165 | { | |
4094ea47 | 166 | $taggingRule = $this->getTaggingRule('bla bla', ['hey']); |
e9fa8c40 JB |
167 | |
168 | $user = $this->getUser([$taggingRule]); | |
169 | ||
170 | $this->rulerz | |
171 | ->method('satisfies') | |
172 | ->willReturn(true); | |
173 | ||
174 | $this->rulerz | |
175 | ->method('filter') | |
4094ea47 | 176 | ->willReturn([new Entry($user), new Entry($user)]); |
e9fa8c40 JB |
177 | |
178 | $entries = $this->tagger->tagAllForUser($user); | |
179 | ||
180 | $this->assertCount(2, $entries); | |
181 | ||
182 | foreach ($entries as $entry) { | |
183 | $tags = $entry->getTags(); | |
184 | ||
185 | $this->assertCount(1, $tags); | |
f808b016 | 186 | $this->assertSame('hey', $tags[0]->getLabel()); |
e9fa8c40 JB |
187 | } |
188 | } | |
189 | ||
5a166c5c KG |
190 | private function getUser(array $taggingRules = []) |
191 | { | |
347fa6be | 192 | $user = new User(); |
5a166c5c KG |
193 | $config = new Config($user); |
194 | ||
195 | $user->setConfig($config); | |
196 | ||
197 | foreach ($taggingRules as $rule) { | |
198 | $config->addTaggingRule($rule); | |
199 | } | |
200 | ||
201 | return $user; | |
202 | } | |
203 | ||
204 | private function getTaggingRule($rule, array $tags) | |
205 | { | |
206 | $taggingRule = new TaggingRule(); | |
207 | $taggingRule->setRule($rule); | |
208 | $taggingRule->setTags($tags); | |
209 | ||
210 | return $taggingRule; | |
211 | } | |
212 | ||
213 | private function getRulerZMock() | |
214 | { | |
215 | return $this->getMockBuilder('RulerZ\RulerZ') | |
216 | ->disableOriginalConstructor() | |
217 | ->getMock(); | |
218 | } | |
219 | ||
220 | private function getTagRepositoryMock() | |
221 | { | |
222 | return $this->getMockBuilder('Wallabag\CoreBundle\Repository\TagRepository') | |
223 | ->disableOriginalConstructor() | |
224 | ->getMock(); | |
225 | } | |
625acf33 KG |
226 | |
227 | private function getEntryRepositoryMock() | |
228 | { | |
229 | return $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | |
230 | ->disableOriginalConstructor() | |
231 | ->getMock(); | |
232 | } | |
3554364b NL |
233 | |
234 | private function getLogger() | |
235 | { | |
a1146b65 | 236 | return new Logger('foo'); |
3554364b | 237 | } |
5a166c5c | 238 | } |