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