]> git.immae.eu Git - github/wallabag/wallabag.git/blob - tests/Wallabag/CoreBundle/Helper/RuleBasedTaggerTest.php
522cf3b36c397a378eeaf0539c9185d35927d5f5
[github/wallabag/wallabag.git] / tests / Wallabag / CoreBundle / Helper / RuleBasedTaggerTest.php
1 <?php
2
3 namespace Tests\Wallabag\CoreBundle\Helper;
4
5 use Psr\Log\NullLogger;
6 use Wallabag\CoreBundle\Entity\Config;
7 use Wallabag\CoreBundle\Entity\Entry;
8 use Wallabag\CoreBundle\Entity\Tag;
9 use Wallabag\CoreBundle\Entity\TaggingRule;
10 use Wallabag\CoreBundle\Helper\RuleBasedTagger;
11 use Wallabag\UserBundle\Entity\User;
12
13 class RuleBasedTaggerTest extends \PHPUnit_Framework_TestCase
14 {
15 private $rulerz;
16 private $tagRepository;
17 private $entryRepository;
18 private $tagger;
19
20 public function setUp()
21 {
22 $this->rulerz = $this->getRulerZMock();
23 $this->tagRepository = $this->getTagRepositoryMock();
24 $this->entryRepository = $this->getEntryRepositoryMock();
25
26 $this->tagger = new RuleBasedTagger($this->rulerz, $this->tagRepository, $this->entryRepository, $this->getLogger());
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 {
40 $taggingRule = $this->getTaggingRule('rule as string', ['foo', 'bar']);
41 $user = $this->getUser([$taggingRule]);
42 $entry = new Entry($user);
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 {
57 $taggingRule = $this->getTaggingRule('rule as string', ['foo', 'bar']);
58 $user = $this->getUser([$taggingRule]);
59 $entry = new Entry($user);
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());
73 $this->assertSame('bar', $tags[1]->getLabel());
74 }
75
76 public function testTagWithAMixOfMatchingRules()
77 {
78 $taggingRule = $this->getTaggingRule('bla bla', ['hey']);
79 $otherTaggingRule = $this->getTaggingRule('rule as string', ['foo']);
80
81 $user = $this->getUser([$taggingRule, $otherTaggingRule]);
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());
94 }
95
96 public function testWhenTheTagExists()
97 {
98 $taggingRule = $this->getTaggingRule('rule as string', ['foo']);
99 $user = $this->getUser([$taggingRule]);
100 $entry = new Entry($user);
101 $tag = new Tag();
102
103 $this->rulerz
104 ->expects($this->once())
105 ->method('satisfies')
106 ->with($entry, 'rule as string')
107 ->willReturn(true);
108
109 $this->tagRepository
110 ->expects($this->once())
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')
114 ->willReturn($tag);
115
116 $this->tagger->tag($entry);
117
118 $this->assertFalse($entry->getTags()->isEmpty());
119
120 $tags = $entry->getTags();
121 $this->assertSame($tag, $tags[0]);
122 }
123
124 public function testSameTagWithDifferentfMatchingRules()
125 {
126 $taggingRule = $this->getTaggingRule('bla bla', ['hey']);
127 $otherTaggingRule = $this->getTaggingRule('rule as string', ['hey']);
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
144 public function testTagAllEntriesForAUser()
145 {
146 $taggingRule = $this->getTaggingRule('bla bla', ['hey']);
147
148 $user = $this->getUser([$taggingRule]);
149
150 $this->rulerz
151 ->method('satisfies')
152 ->willReturn(true);
153
154 $this->rulerz
155 ->method('filter')
156 ->willReturn([new Entry($user), new Entry($user)]);
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
170 private function getUser(array $taggingRules = [])
171 {
172 $user = new User();
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 }
206
207 private function getEntryRepositoryMock()
208 {
209 return $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
210 ->disableOriginalConstructor()
211 ->getMock();
212 }
213
214 private function getLogger()
215 {
216 return new NullLogger();
217 }
218 }