]> git.immae.eu Git - github/wallabag/wallabag.git/blame - tests/Wallabag/CoreBundle/Helper/RuleBasedTaggerTest.php
Jump to Symfony 3.1
[github/wallabag/wallabag.git] / tests / Wallabag / CoreBundle / Helper / RuleBasedTaggerTest.php
CommitLineData
5a166c5c
KG
1<?php
2
23634d5d 3namespace Tests\Wallabag\CoreBundle\Helper;
5a166c5c
KG
4
5use Wallabag\CoreBundle\Entity\Config;
6use Wallabag\CoreBundle\Entity\Entry;
7use Wallabag\CoreBundle\Entity\Tag;
8use Wallabag\CoreBundle\Entity\TaggingRule;
5a166c5c 9use Wallabag\CoreBundle\Helper\RuleBasedTagger;
619cc453 10use Wallabag\UserBundle\Entity\User;
5a166c5c
KG
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 {
4094ea47 39 $taggingRule = $this->getTaggingRule('rule as string', ['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 {
4094ea47 56 $taggingRule = $this->getTaggingRule('rule as string', ['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 {
4094ea47
JB
77 $taggingRule = $this->getTaggingRule('bla bla', ['hey']);
78 $otherTaggingRule = $this->getTaggingRule('rule as string', ['foo']);
5a166c5c 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 {
4094ea47 97 $taggingRule = $this->getTaggingRule('rule as string', ['foo']);
347fa6be
NL
98 $user = $this->getUser([$taggingRule]);
99 $entry = new Entry($user);
fc732227 100 $tag = new Tag();
5a166c5c
KG
101
102 $this->rulerz
103 ->expects($this->once())
104 ->method('satisfies')
105 ->with($entry, 'rule as string')
106 ->willReturn(true);
107
625acf33 108 $this->tagRepository
5a166c5c 109 ->expects($this->once())
fc732227
JB
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')
5a166c5c
KG
113 ->willReturn($tag);
114
115 $this->tagger->tag($entry);
116
117 $this->assertFalse($entry->getTags()->isEmpty());
118
119 $tags = $entry->getTags();
1bb1939a 120 $this->assertSame($tag, $tags[0]);
5a166c5c
KG
121 }
122
fc031e57
JB
123 public function testSameTagWithDifferentfMatchingRules()
124 {
4094ea47
JB
125 $taggingRule = $this->getTaggingRule('bla bla', ['hey']);
126 $otherTaggingRule = $this->getTaggingRule('rule as string', ['hey']);
fc031e57
JB
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
e9fa8c40
JB
143 public function testTagAllEntriesForAUser()
144 {
4094ea47 145 $taggingRule = $this->getTaggingRule('bla bla', ['hey']);
e9fa8c40
JB
146
147 $user = $this->getUser([$taggingRule]);
148
149 $this->rulerz
150 ->method('satisfies')
151 ->willReturn(true);
152
153 $this->rulerz
154 ->method('filter')
4094ea47 155 ->willReturn([new Entry($user), new Entry($user)]);
e9fa8c40
JB
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
5a166c5c
KG
169 private function getUser(array $taggingRules = [])
170 {
347fa6be 171 $user = new User();
5a166c5c
KG
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 }
625acf33
KG
205
206 private function getEntryRepositoryMock()
207 {
208 return $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
209 ->disableOriginalConstructor()
210 ->getMock();
211 }
5a166c5c 212}