]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Tests/Helper/RuleBasedTaggerTest.php
Avoid multiple tag creation
[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\UserBundle\Entity\User;
10 use Wallabag\CoreBundle\Helper\RuleBasedTagger;
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', array('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', array('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($user, $tags[0]->getUser());
73 $this->assertSame('bar', $tags[1]->getLabel());
74 $this->assertSame($user, $tags[1]->getUser());
75 }
76
77 public function testTagWithAMixOfMatchingRules()
78 {
79 $taggingRule = $this->getTaggingRule('bla bla', array('hey'));
80 $otherTaggingRule = $this->getTaggingRule('rule as string', array('foo'));
81
82 $user = $this->getUser([$taggingRule, $otherTaggingRule]);
83 $entry = new Entry($user);
84
85 $this->rulerz
86 ->method('satisfies')
87 ->will($this->onConsecutiveCalls(false, true));
88
89 $this->tagger->tag($entry);
90
91 $this->assertFalse($entry->getTags()->isEmpty());
92
93 $tags = $entry->getTags();
94 $this->assertSame('foo', $tags[0]->getLabel());
95 $this->assertSame($user, $tags[0]->getUser());
96 }
97
98 public function testWhenTheTagExists()
99 {
100 $taggingRule = $this->getTaggingRule('rule as string', array('foo'));
101 $user = $this->getUser([$taggingRule]);
102 $entry = new Entry($user);
103 $tag = new Tag($user);
104
105 $this->rulerz
106 ->expects($this->once())
107 ->method('satisfies')
108 ->with($entry, 'rule as string')
109 ->willReturn(true);
110
111 $this->tagRepository
112 ->expects($this->once())
113 ->method('findOneByLabelAndUserId')
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', 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
144 private function getUser(array $taggingRules = [])
145 {
146 $user = new User();
147 $config = new Config($user);
148
149 $user->setConfig($config);
150
151 foreach ($taggingRules as $rule) {
152 $config->addTaggingRule($rule);
153 }
154
155 return $user;
156 }
157
158 private function getTaggingRule($rule, array $tags)
159 {
160 $taggingRule = new TaggingRule();
161 $taggingRule->setRule($rule);
162 $taggingRule->setTags($tags);
163
164 return $taggingRule;
165 }
166
167 private function getRulerZMock()
168 {
169 return $this->getMockBuilder('RulerZ\RulerZ')
170 ->disableOriginalConstructor()
171 ->getMock();
172 }
173
174 private function getTagRepositoryMock()
175 {
176 return $this->getMockBuilder('Wallabag\CoreBundle\Repository\TagRepository')
177 ->disableOriginalConstructor()
178 ->getMock();
179 }
180
181 private function getEntryRepositoryMock()
182 {
183 return $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
184 ->disableOriginalConstructor()
185 ->getMock();
186 }
187 }