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
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($user, $tags[0]->getUser());
73 $this->assertSame('bar', $tags[1]->getLabel());
74 $this->assertSame($user, $tags[1]->getUser());
77 public function testTagWithAMixOfMatchingRules()
79 $taggingRule = $this->getTaggingRule('bla bla', array('hey'));
80 $otherTaggingRule = $this->getTaggingRule('rule as string', array('foo'));
82 $user = $this->getUser([$taggingRule, $otherTaggingRule]);
83 $entry = new Entry($user);
87 ->will($this->onConsecutiveCalls(false, true));
89 $this->tagger
->tag($entry);
91 $this->assertFalse($entry->getTags()->isEmpty());
93 $tags = $entry->getTags();
94 $this->assertSame('foo', $tags[0]->getLabel());
95 $this->assertSame($user, $tags[0]->getUser());
98 public function testWhenTheTagExists()
100 $taggingRule = $this->getTaggingRule('rule as string', array('foo'));
101 $user = $this->getUser([$taggingRule]);
102 $entry = new Entry($user);
103 $tag = new Tag($user);
106 ->expects($this->once())
107 ->method('satisfies')
108 ->with($entry, 'rule as string')
112 ->expects($this->once())
113 ->method('findOneByLabelAndUserId')
116 $this->tagger
->tag($entry);
118 $this->assertFalse($entry->getTags()->isEmpty());
120 $tags = $entry->getTags();
121 $this->assertSame($tag, $tags[0]);
124 private function getUser(array $taggingRules = [])
127 $config = new Config($user);
129 $user->setConfig($config);
131 foreach ($taggingRules as $rule) {
132 $config->addTaggingRule($rule);
138 private function getTaggingRule($rule, array $tags)
140 $taggingRule = new TaggingRule();
141 $taggingRule->setRule($rule);
142 $taggingRule->setTags($tags);
147 private function getRulerZMock()
149 return $this->getMockBuilder('RulerZ\RulerZ')
150 ->disableOriginalConstructor()
154 private function getTagRepositoryMock()
156 return $this->getMockBuilder('Wallabag\CoreBundle\Repository\TagRepository')
157 ->disableOriginalConstructor()
161 private function getEntryRepositoryMock()
163 return $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
164 ->disableOriginalConstructor()