]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Tests/Helper/RuleBasedTaggerTest.php
Merge pull request #2160 from wallabag/bin-cs-fixer
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Tests / Helper / RuleBasedTaggerTest.php
1 <?php
2
3 namespace Wallabag\CoreBundle\Tests\Helper;
4
5 use Wallabag\CoreBundle\Entity\Config;
6 use Wallabag\CoreBundle\Entity\Entry;
7 use Wallabag\CoreBundle\Entity\Tag;
8 use Wallabag\CoreBundle\Entity\TaggingRule;
9 use Wallabag\CoreBundle\Helper\RuleBasedTagger;
10 use Wallabag\UserBundle\Entity\User;
11
12 class RuleBasedTaggerTest extends \PHPUnit_Framework_TestCase
13 {
14 private $rulerz;
15 private $tagRepository;
16 private $entryRepository;
17 private $tagger;
18
19 public function setUp()
20 {
21 $this->rulerz = $this->getRulerZMock();
22 $this->tagRepository = $this->getTagRepositoryMock();
23 $this->entryRepository = $this->getEntryRepositoryMock();
24
25 $this->tagger = new RuleBasedTagger($this->rulerz, $this->tagRepository, $this->entryRepository);
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', ['foo', 'bar']);
40 $user = $this->getUser([$taggingRule]);
41 $entry = new Entry($user);
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', ['foo', 'bar']);
57 $user = $this->getUser([$taggingRule]);
58 $entry = new Entry($user);
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());
72 $this->assertSame('bar', $tags[1]->getLabel());
73 }
74
75 public function testTagWithAMixOfMatchingRules()
76 {
77 $taggingRule = $this->getTaggingRule('bla bla', ['hey']);
78 $otherTaggingRule = $this->getTaggingRule('rule as string', ['foo']);
79
80 $user = $this->getUser([$taggingRule, $otherTaggingRule]);
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());
93 }
94
95 public function testWhenTheTagExists()
96 {
97 $taggingRule = $this->getTaggingRule('rule as string', ['foo']);
98 $user = $this->getUser([$taggingRule]);
99 $entry = new Entry($user);
100 $tag = new Tag();
101
102 $this->rulerz
103 ->expects($this->once())
104 ->method('satisfies')
105 ->with($entry, 'rule as string')
106 ->willReturn(true);
107
108 $this->tagRepository
109 ->expects($this->once())
110 // the method `findOneByLabel` doesn't exist, EntityRepository will then call `_call` method
111 // to magically call the `findOneBy` with ['label' => 'foo']
112 ->method('__call')
113 ->willReturn($tag);
114
115 $this->tagger->tag($entry);
116
117 $this->assertFalse($entry->getTags()->isEmpty());
118
119 $tags = $entry->getTags();
120 $this->assertSame($tag, $tags[0]);
121 }
122
123 public function testSameTagWithDifferentfMatchingRules()
124 {
125 $taggingRule = $this->getTaggingRule('bla bla', ['hey']);
126 $otherTaggingRule = $this->getTaggingRule('rule as string', ['hey']);
127
128 $user = $this->getUser([$taggingRule, $otherTaggingRule]);
129 $entry = new Entry($user);
130
131 $this->rulerz
132 ->method('satisfies')
133 ->willReturn(true);
134
135 $this->tagger->tag($entry);
136
137 $this->assertFalse($entry->getTags()->isEmpty());
138
139 $tags = $entry->getTags();
140 $this->assertCount(1, $tags);
141 }
142
143 public function testTagAllEntriesForAUser()
144 {
145 $taggingRule = $this->getTaggingRule('bla bla', ['hey']);
146
147 $user = $this->getUser([$taggingRule]);
148
149 $this->rulerz
150 ->method('satisfies')
151 ->willReturn(true);
152
153 $this->rulerz
154 ->method('filter')
155 ->willReturn([new Entry($user), new Entry($user)]);
156
157 $entries = $this->tagger->tagAllForUser($user);
158
159 $this->assertCount(2, $entries);
160
161 foreach ($entries as $entry) {
162 $tags = $entry->getTags();
163
164 $this->assertCount(1, $tags);
165 $this->assertEquals('hey', $tags[0]->getLabel());
166 }
167 }
168
169 private function getUser(array $taggingRules = [])
170 {
171 $user = new User();
172 $config = new Config($user);
173
174 $user->setConfig($config);
175
176 foreach ($taggingRules as $rule) {
177 $config->addTaggingRule($rule);
178 }
179
180 return $user;
181 }
182
183 private function getTaggingRule($rule, array $tags)
184 {
185 $taggingRule = new TaggingRule();
186 $taggingRule->setRule($rule);
187 $taggingRule->setTags($tags);
188
189 return $taggingRule;
190 }
191
192 private function getRulerZMock()
193 {
194 return $this->getMockBuilder('RulerZ\RulerZ')
195 ->disableOriginalConstructor()
196 ->getMock();
197 }
198
199 private function getTagRepositoryMock()
200 {
201 return $this->getMockBuilder('Wallabag\CoreBundle\Repository\TagRepository')
202 ->disableOriginalConstructor()
203 ->getMock();
204 }
205
206 private function getEntryRepositoryMock()
207 {
208 return $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
209 ->disableOriginalConstructor()
210 ->getMock();
211 }
212 }