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