]> git.immae.eu Git - github/wallabag/wallabag.git/blame - tests/Wallabag/CoreBundle/Helper/RuleBasedTaggerTest.php
Add a real configuration for CS-Fixer
[github/wallabag/wallabag.git] / tests / Wallabag / CoreBundle / Helper / RuleBasedTaggerTest.php
CommitLineData
5a166c5c
KG
1<?php
2
23634d5d 3namespace Tests\Wallabag\CoreBundle\Helper;
5a166c5c 4
a1146b65
NL
5use Monolog\Handler\TestHandler;
6use Monolog\Logger;
5a166c5c
KG
7use Wallabag\CoreBundle\Entity\Config;
8use Wallabag\CoreBundle\Entity\Entry;
9use Wallabag\CoreBundle\Entity\Tag;
10use Wallabag\CoreBundle\Entity\TaggingRule;
5a166c5c 11use Wallabag\CoreBundle\Helper\RuleBasedTagger;
619cc453 12use Wallabag\UserBundle\Entity\User;
5a166c5c
KG
13
14class RuleBasedTaggerTest extends \PHPUnit_Framework_TestCase
15{
16 private $rulerz;
625acf33
KG
17 private $tagRepository;
18 private $entryRepository;
5a166c5c 19 private $tagger;
a1146b65
NL
20 private $logger;
21 private $handler;
5a166c5c
KG
22
23 public function setUp()
24 {
347fa6be
NL
25 $this->rulerz = $this->getRulerZMock();
26 $this->tagRepository = $this->getTagRepositoryMock();
625acf33 27 $this->entryRepository = $this->getEntryRepositoryMock();
a1146b65
NL
28 $this->logger = $this->getLogger();
29 $this->handler = new TestHandler();
30 $this->logger->pushHandler($this->handler);
5a166c5c 31
a1146b65 32 $this->tagger = new RuleBasedTagger($this->rulerz, $this->tagRepository, $this->entryRepository, $this->logger);
5a166c5c
KG
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());
a1146b65
NL
42 $records = $this->handler->getRecords();
43 $this->assertCount(0, $records);
5a166c5c
KG
44 }
45
46 public function testTagWithNoMatchingRule()
47 {
4094ea47 48 $taggingRule = $this->getTaggingRule('rule as string', ['foo', 'bar']);
347fa6be
NL
49 $user = $this->getUser([$taggingRule]);
50 $entry = new Entry($user);
5a166c5c
KG
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());
a1146b65
NL
61 $records = $this->handler->getRecords();
62 $this->assertCount(0, $records);
5a166c5c
KG
63 }
64
65 public function testTagWithAMatchingRule()
66 {
4094ea47 67 $taggingRule = $this->getTaggingRule('rule as string', ['foo', 'bar']);
347fa6be
NL
68 $user = $this->getUser([$taggingRule]);
69 $entry = new Entry($user);
5a166c5c
KG
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());
5a166c5c 83 $this->assertSame('bar', $tags[1]->getLabel());
a1146b65
NL
84
85 $records = $this->handler->getRecords();
86 $this->assertCount(1, $records);
5a166c5c
KG
87 }
88
89 public function testTagWithAMixOfMatchingRules()
90 {
4094ea47
JB
91 $taggingRule = $this->getTaggingRule('bla bla', ['hey']);
92 $otherTaggingRule = $this->getTaggingRule('rule as string', ['foo']);
5a166c5c 93
347fa6be 94 $user = $this->getUser([$taggingRule, $otherTaggingRule]);
5a166c5c
KG
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());
a1146b65
NL
107 $records = $this->handler->getRecords();
108 $this->assertCount(1, $records);
5a166c5c
KG
109 }
110
111 public function testWhenTheTagExists()
112 {
4094ea47 113 $taggingRule = $this->getTaggingRule('rule as string', ['foo']);
347fa6be
NL
114 $user = $this->getUser([$taggingRule]);
115 $entry = new Entry($user);
fc732227 116 $tag = new Tag();
5a166c5c
KG
117
118 $this->rulerz
119 ->expects($this->once())
120 ->method('satisfies')
121 ->with($entry, 'rule as string')
122 ->willReturn(true);
123
625acf33 124 $this->tagRepository
5a166c5c 125 ->expects($this->once())
fc732227
JB
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')
5a166c5c
KG
129 ->willReturn($tag);
130
131 $this->tagger->tag($entry);
132
133 $this->assertFalse($entry->getTags()->isEmpty());
134
135 $tags = $entry->getTags();
1bb1939a 136 $this->assertSame($tag, $tags[0]);
a1146b65
NL
137 $records = $this->handler->getRecords();
138 $this->assertCount(1, $records);
5a166c5c
KG
139 }
140
fc031e57
JB
141 public function testSameTagWithDifferentfMatchingRules()
142 {
4094ea47
JB
143 $taggingRule = $this->getTaggingRule('bla bla', ['hey']);
144 $otherTaggingRule = $this->getTaggingRule('rule as string', ['hey']);
fc031e57
JB
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);
a1146b65
NL
159 $records = $this->handler->getRecords();
160 $this->assertCount(2, $records);
fc031e57
JB
161 }
162
e9fa8c40
JB
163 public function testTagAllEntriesForAUser()
164 {
4094ea47 165 $taggingRule = $this->getTaggingRule('bla bla', ['hey']);
e9fa8c40
JB
166
167 $user = $this->getUser([$taggingRule]);
168
169 $this->rulerz
170 ->method('satisfies')
171 ->willReturn(true);
172
173 $this->rulerz
174 ->method('filter')
4094ea47 175 ->willReturn([new Entry($user), new Entry($user)]);
e9fa8c40
JB
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);
f808b016 185 $this->assertSame('hey', $tags[0]->getLabel());
e9fa8c40
JB
186 }
187 }
188
5a166c5c
KG
189 private function getUser(array $taggingRules = [])
190 {
347fa6be 191 $user = new User();
5a166c5c
KG
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 }
625acf33
KG
225
226 private function getEntryRepositoryMock()
227 {
228 return $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
229 ->disableOriginalConstructor()
230 ->getMock();
231 }
3554364b
NL
232
233 private function getLogger()
234 {
a1146b65 235 return new Logger('foo');
3554364b 236 }
5a166c5c 237}