]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Tests/Helper/RuleBasedTaggerTest.php
Add a command to automatically tag all entries for a user
[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 private function getUser(array $taggingRules = [])
125 {
126 $user = new User();
127 $config = new Config($user);
128
129 $user->setConfig($config);
130
131 foreach ($taggingRules as $rule) {
132 $config->addTaggingRule($rule);
133 }
134
135 return $user;
136 }
137
138 private function getTaggingRule($rule, array $tags)
139 {
140 $taggingRule = new TaggingRule();
141 $taggingRule->setRule($rule);
142 $taggingRule->setTags($tags);
143
144 return $taggingRule;
145 }
146
147 private function getRulerZMock()
148 {
149 return $this->getMockBuilder('RulerZ\RulerZ')
150 ->disableOriginalConstructor()
151 ->getMock();
152 }
153
154 private function getTagRepositoryMock()
155 {
156 return $this->getMockBuilder('Wallabag\CoreBundle\Repository\TagRepository')
157 ->disableOriginalConstructor()
158 ->getMock();
159 }
160
161 private function getEntryRepositoryMock()
162 {
163 return $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
164 ->disableOriginalConstructor()
165 ->getMock();
166 }
167 }