3 namespace Wallabag\CoreBundle\Tests\Helper
;
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
;
12 class RuleBasedTaggerTest
extends \PHPUnit_Framework_TestCase
15 private $tagRepository;
16 private $entryRepository;
19 public function setUp()
21 $this->rulerz
= $this->getRulerZMock();
22 $this->tagRepository
= $this->getTagRepositoryMock();
23 $this->entryRepository
= $this->getEntryRepositoryMock();
25 $this->tagger
= new RuleBasedTagger($this->rulerz
, $this->tagRepository
, $this->entryRepository
);
28 public function testTagWithNoRule()
30 $entry = new Entry($this->getUser());
32 $this->tagger
->tag($entry);
34 $this->assertTrue($entry->getTags()->isEmpty());
37 public function testTagWithNoMatchingRule()
39 $taggingRule = $this->getTaggingRule('rule as string', array('foo', 'bar'));
40 $user = $this->getUser([$taggingRule]);
41 $entry = new Entry($user);
44 ->expects($this->once())
46 ->with($entry, 'rule as string')
49 $this->tagger
->tag($entry);
51 $this->assertTrue($entry->getTags()->isEmpty());
54 public function testTagWithAMatchingRule()
56 $taggingRule = $this->getTaggingRule('rule as string', array('foo', 'bar'));
57 $user = $this->getUser([$taggingRule]);
58 $entry = new Entry($user);
61 ->expects($this->once())
63 ->with($entry, 'rule as string')
66 $this->tagger
->tag($entry);
68 $this->assertFalse($entry->getTags()->isEmpty());
70 $tags = $entry->getTags();
71 $this->assertSame('foo', $tags[0]->getLabel());
72 $this->assertSame('bar', $tags[1]->getLabel());
75 public function testTagWithAMixOfMatchingRules()
77 $taggingRule = $this->getTaggingRule('bla bla', array('hey'));
78 $otherTaggingRule = $this->getTaggingRule('rule as string', array('foo'));
80 $user = $this->getUser([$taggingRule, $otherTaggingRule]);
81 $entry = new Entry($user);
85 ->will($this->onConsecutiveCalls(false, true));
87 $this->tagger
->tag($entry);
89 $this->assertFalse($entry->getTags()->isEmpty());
91 $tags = $entry->getTags();
92 $this->assertSame('foo', $tags[0]->getLabel());
95 public function testWhenTheTagExists()
97 $taggingRule = $this->getTaggingRule('rule as string', array('foo'));
98 $user = $this->getUser([$taggingRule]);
99 $entry = new Entry($user);
103 ->expects($this->once())
104 ->method('satisfies')
105 ->with($entry, 'rule as string')
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']
115 $this->tagger
->tag($entry);
117 $this->assertFalse($entry->getTags()->isEmpty());
119 $tags = $entry->getTags();
120 $this->assertSame($tag, $tags[0]);
123 public function testSameTagWithDifferentfMatchingRules()
125 $taggingRule = $this->getTaggingRule('bla bla', array('hey'));
126 $otherTaggingRule = $this->getTaggingRule('rule as string', array('hey'));
128 $user = $this->getUser([$taggingRule, $otherTaggingRule]);
129 $entry = new Entry($user);
132 ->method('satisfies')
135 $this->tagger
->tag($entry);
137 $this->assertFalse($entry->getTags()->isEmpty());
139 $tags = $entry->getTags();
140 $this->assertCount(1, $tags);
143 public function testTagAllEntriesForAUser()
145 $taggingRule = $this->getTaggingRule('bla bla', array('hey'));
147 $user = $this->getUser([$taggingRule]);
150 ->method('satisfies')
155 ->willReturn(array(new Entry($user), new Entry($user)));
157 $entries = $this->tagger
->tagAllForUser($user);
159 $this->assertCount(2, $entries);
161 foreach ($entries as $entry) {
162 $tags = $entry->getTags();
164 $this->assertCount(1, $tags);
165 $this->assertEquals('hey', $tags[0]->getLabel());
169 private function getUser(array $taggingRules = [])
172 $config = new Config($user);
174 $user->setConfig($config);
176 foreach ($taggingRules as $rule) {
177 $config->addTaggingRule($rule);
183 private function getTaggingRule($rule, array $tags)
185 $taggingRule = new TaggingRule();
186 $taggingRule->setRule($rule);
187 $taggingRule->setTags($tags);
192 private function getRulerZMock()
194 return $this->getMockBuilder('RulerZ\RulerZ')
195 ->disableOriginalConstructor()
199 private function getTagRepositoryMock()
201 return $this->getMockBuilder('Wallabag\CoreBundle\Repository\TagRepository')
202 ->disableOriginalConstructor()
206 private function getEntryRepositoryMock()
208 return $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
209 ->disableOriginalConstructor()