]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Tests/Helper/RuleBasedTaggerTest.php
Fix parameters regarding documentation
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Tests / Helper / RuleBasedTaggerTest.php
CommitLineData
5a166c5c
KG
1<?php
2
3namespace Wallabag\CoreBundle\Tests\Helper;
4
5use Wallabag\CoreBundle\Entity\Config;
6use Wallabag\CoreBundle\Entity\Entry;
7use Wallabag\CoreBundle\Entity\Tag;
8use Wallabag\CoreBundle\Entity\TaggingRule;
9use Wallabag\UserBundle\Entity\User;
10use Wallabag\CoreBundle\Helper\RuleBasedTagger;
11
12class RuleBasedTaggerTest extends \PHPUnit_Framework_TestCase
13{
14 private $rulerz;
625acf33
KG
15 private $tagRepository;
16 private $entryRepository;
5a166c5c
KG
17 private $tagger;
18
19 public function setUp()
20 {
347fa6be
NL
21 $this->rulerz = $this->getRulerZMock();
22 $this->tagRepository = $this->getTagRepositoryMock();
625acf33 23 $this->entryRepository = $this->getEntryRepositoryMock();
5a166c5c 24
625acf33 25 $this->tagger = new RuleBasedTagger($this->rulerz, $this->tagRepository, $this->entryRepository);
5a166c5c
KG
26 }
27
28 public function testTagWithNoRule()
29 {
30 $entry = new Entry($this->getUser());
31
32 $this->tagger->tag($entry);
33
34 $this->assertTrue($entry->getTags()->isEmpty());
35 }
36
37 public function testTagWithNoMatchingRule()
38 {
39 $taggingRule = $this->getTaggingRule('rule as string', array('foo', 'bar'));
347fa6be
NL
40 $user = $this->getUser([$taggingRule]);
41 $entry = new Entry($user);
5a166c5c
KG
42
43 $this->rulerz
44 ->expects($this->once())
45 ->method('satisfies')
46 ->with($entry, 'rule as string')
47 ->willReturn(false);
48
49 $this->tagger->tag($entry);
50
51 $this->assertTrue($entry->getTags()->isEmpty());
52 }
53
54 public function testTagWithAMatchingRule()
55 {
56 $taggingRule = $this->getTaggingRule('rule as string', array('foo', 'bar'));
347fa6be
NL
57 $user = $this->getUser([$taggingRule]);
58 $entry = new Entry($user);
5a166c5c
KG
59
60 $this->rulerz
61 ->expects($this->once())
62 ->method('satisfies')
63 ->with($entry, 'rule as string')
64 ->willReturn(true);
65
66 $this->tagger->tag($entry);
67
68 $this->assertFalse($entry->getTags()->isEmpty());
69
70 $tags = $entry->getTags();
71 $this->assertSame('foo', $tags[0]->getLabel());
5a166c5c 72 $this->assertSame('bar', $tags[1]->getLabel());
5a166c5c
KG
73 }
74
75 public function testTagWithAMixOfMatchingRules()
76 {
347fa6be 77 $taggingRule = $this->getTaggingRule('bla bla', array('hey'));
5a166c5c
KG
78 $otherTaggingRule = $this->getTaggingRule('rule as string', array('foo'));
79
347fa6be 80 $user = $this->getUser([$taggingRule, $otherTaggingRule]);
5a166c5c
KG
81 $entry = new Entry($user);
82
83 $this->rulerz
84 ->method('satisfies')
85 ->will($this->onConsecutiveCalls(false, true));
86
87 $this->tagger->tag($entry);
88
89 $this->assertFalse($entry->getTags()->isEmpty());
90
91 $tags = $entry->getTags();
92 $this->assertSame('foo', $tags[0]->getLabel());
5a166c5c
KG
93 }
94
95 public function testWhenTheTagExists()
96 {
97 $taggingRule = $this->getTaggingRule('rule as string', array('foo'));
347fa6be
NL
98 $user = $this->getUser([$taggingRule]);
99 $entry = new Entry($user);
fc732227
JB
100 $tag = new Tag();
101 $tag->setLabel('foo');
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();
fc732227 121 $this->assertSame($tag->getLabel(), $tags[0]->getLabel());
5a166c5c
KG
122 }
123
fc031e57
JB
124 public function testSameTagWithDifferentfMatchingRules()
125 {
126 $taggingRule = $this->getTaggingRule('bla bla', array('hey'));
127 $otherTaggingRule = $this->getTaggingRule('rule as string', array('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
e9fa8c40
JB
144 public function testTagAllEntriesForAUser()
145 {
146 $taggingRule = $this->getTaggingRule('bla bla', array('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(array(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
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 }
5a166c5c 213}