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\UserBundle\Entity\User
;
10 use Wallabag\CoreBundle\Helper\RuleBasedTagger
;
12 class RuleBasedTaggerTest
extends \PHPUnit_Framework_TestCase
18 public function setUp()
20 $this->rulerz
= $this->getRulerZMock();
21 $this->repository
= $this->getTagRepositoryMock();
23 $this->tagger
= new RuleBasedTagger($this->rulerz
, $this->repository
);
26 public function testTagWithNoRule()
28 $entry = new Entry($this->getUser());
30 $this->tagger
->tag($entry);
32 $this->assertTrue($entry->getTags()->isEmpty());
35 public function testTagWithNoMatchingRule()
37 $taggingRule = $this->getTaggingRule('rule as string', array('foo', 'bar'));
38 $user = $this->getUser([$taggingRule]);
39 $entry = new Entry($user);
42 ->expects($this->once())
44 ->with($entry, 'rule as string')
47 $this->tagger
->tag($entry);
49 $this->assertTrue($entry->getTags()->isEmpty());
52 public function testTagWithAMatchingRule()
54 $taggingRule = $this->getTaggingRule('rule as string', array('foo', 'bar'));
55 $user = $this->getUser([$taggingRule]);
56 $entry = new Entry($user);
59 ->expects($this->once())
61 ->with($entry, 'rule as string')
64 $this->tagger
->tag($entry);
66 $this->assertFalse($entry->getTags()->isEmpty());
68 $tags = $entry->getTags();
69 $this->assertSame('foo', $tags[0]->getLabel());
70 $this->assertSame($user, $tags[0]->getUser());
71 $this->assertSame('bar', $tags[1]->getLabel());
72 $this->assertSame($user, $tags[1]->getUser());
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());
93 $this->assertSame($user, $tags[0]->getUser());
96 public function testWhenTheTagExists()
98 $taggingRule = $this->getTaggingRule('rule as string', array('foo'));
99 $user = $this->getUser([$taggingRule]);
100 $entry = new Entry($user);
101 $tag = new Tag($user);
104 ->expects($this->once())
105 ->method('satisfies')
106 ->with($entry, 'rule as string')
110 ->expects($this->once())
111 ->method('findOneByLabelAndUserId')
114 $this->tagger
->tag($entry);
116 $this->assertFalse($entry->getTags()->isEmpty());
118 $tags = $entry->getTags();
119 $this->assertSame($tag, $tags[0]);
122 private function getUser(array $taggingRules = [])
125 $config = new Config($user);
127 $user->setConfig($config);
129 foreach ($taggingRules as $rule) {
130 $config->addTaggingRule($rule);
136 private function getTaggingRule($rule, array $tags)
138 $taggingRule = new TaggingRule();
139 $taggingRule->setRule($rule);
140 $taggingRule->setTags($tags);
145 private function getRulerZMock()
147 return $this->getMockBuilder('RulerZ\RulerZ')
148 ->disableOriginalConstructor()
152 private function getTagRepositoryMock()
154 return $this->getMockBuilder('Wallabag\CoreBundle\Repository\TagRepository')
155 ->disableOriginalConstructor()