]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | ||
3 | namespace Tests\Wallabag\CoreBundle\Helper; | |
4 | ||
5 | use Monolog\Handler\TestHandler; | |
6 | use Monolog\Logger; | |
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; | |
14 | ||
15 | class RuleBasedTaggerTest extends TestCase | |
16 | { | |
17 | private $rulerz; | |
18 | private $tagRepository; | |
19 | private $entryRepository; | |
20 | private $tagger; | |
21 | private $logger; | |
22 | private $handler; | |
23 | ||
24 | public function setUp() | |
25 | { | |
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); | |
32 | ||
33 | $this->tagger = new RuleBasedTagger($this->rulerz, $this->tagRepository, $this->entryRepository, $this->logger); | |
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()); | |
43 | $records = $this->handler->getRecords(); | |
44 | $this->assertCount(0, $records); | |
45 | } | |
46 | ||
47 | public function testTagWithNoMatchingRule() | |
48 | { | |
49 | $taggingRule = $this->getTaggingRule('rule as string', ['foo', 'bar']); | |
50 | $user = $this->getUser([$taggingRule]); | |
51 | $entry = new Entry($user); | |
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()); | |
62 | $records = $this->handler->getRecords(); | |
63 | $this->assertCount(0, $records); | |
64 | } | |
65 | ||
66 | public function testTagWithAMatchingRule() | |
67 | { | |
68 | $taggingRule = $this->getTaggingRule('rule as string', ['foo', 'bar']); | |
69 | $user = $this->getUser([$taggingRule]); | |
70 | $entry = new Entry($user); | |
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()); | |
84 | $this->assertSame('bar', $tags[1]->getLabel()); | |
85 | ||
86 | $records = $this->handler->getRecords(); | |
87 | $this->assertCount(1, $records); | |
88 | } | |
89 | ||
90 | public function testTagWithAMixOfMatchingRules() | |
91 | { | |
92 | $taggingRule = $this->getTaggingRule('bla bla', ['hey']); | |
93 | $otherTaggingRule = $this->getTaggingRule('rule as string', ['foo']); | |
94 | ||
95 | $user = $this->getUser([$taggingRule, $otherTaggingRule]); | |
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()); | |
108 | $records = $this->handler->getRecords(); | |
109 | $this->assertCount(1, $records); | |
110 | } | |
111 | ||
112 | public function testWhenTheTagExists() | |
113 | { | |
114 | $taggingRule = $this->getTaggingRule('rule as string', ['foo']); | |
115 | $user = $this->getUser([$taggingRule]); | |
116 | $entry = new Entry($user); | |
117 | $tag = new Tag(); | |
118 | ||
119 | $this->rulerz | |
120 | ->expects($this->once()) | |
121 | ->method('satisfies') | |
122 | ->with($entry, 'rule as string') | |
123 | ->willReturn(true); | |
124 | ||
125 | $this->tagRepository | |
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'] | |
129 | ->method('__call') | |
130 | ->willReturn($tag); | |
131 | ||
132 | $this->tagger->tag($entry); | |
133 | ||
134 | $this->assertFalse($entry->getTags()->isEmpty()); | |
135 | ||
136 | $tags = $entry->getTags(); | |
137 | $this->assertSame($tag, $tags[0]); | |
138 | $records = $this->handler->getRecords(); | |
139 | $this->assertCount(1, $records); | |
140 | } | |
141 | ||
142 | public function testSameTagWithDifferentfMatchingRules() | |
143 | { | |
144 | $taggingRule = $this->getTaggingRule('bla bla', ['hey']); | |
145 | $otherTaggingRule = $this->getTaggingRule('rule as string', ['hey']); | |
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); | |
160 | $records = $this->handler->getRecords(); | |
161 | $this->assertCount(2, $records); | |
162 | } | |
163 | ||
164 | public function testTagAllEntriesForAUser() | |
165 | { | |
166 | $taggingRule = $this->getTaggingRule('bla bla', ['hey']); | |
167 | ||
168 | $user = $this->getUser([$taggingRule]); | |
169 | ||
170 | $this->rulerz | |
171 | ->method('satisfies') | |
172 | ->willReturn(true); | |
173 | ||
174 | $this->rulerz | |
175 | ->method('filter') | |
176 | ->willReturn([new Entry($user), new Entry($user)]); | |
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); | |
186 | $this->assertSame('hey', $tags[0]->getLabel()); | |
187 | } | |
188 | } | |
189 | ||
190 | private function getUser(array $taggingRules = []) | |
191 | { | |
192 | $user = new User(); | |
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 | } | |
226 | ||
227 | private function getEntryRepositoryMock() | |
228 | { | |
229 | return $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | |
230 | ->disableOriginalConstructor() | |
231 | ->getMock(); | |
232 | } | |
233 | ||
234 | private function getLogger() | |
235 | { | |
236 | return new Logger('foo'); | |
237 | } | |
238 | } |