3 namespace Tests\Wallabag\CoreBundle\Helper
;
5 use Monolog\Handler\TestHandler
;
7 use Wallabag\CoreBundle\Entity\Config
;
8 use Wallabag\CoreBundle\Entity\Entry
;
9 use Wallabag\CoreBundle\Entity\Tag
;
10 use Wallabag\CoreBundle\Entity\TaggingRule
;
11 use Wallabag\CoreBundle\Helper\RuleBasedTagger
;
12 use Wallabag\UserBundle\Entity\User
;
14 class RuleBasedTaggerTest
extends \PHPUnit_Framework_TestCase
17 private $tagRepository;
18 private $entryRepository;
23 public function setUp()
25 $this->rulerz
= $this->getRulerZMock();
26 $this->tagRepository
= $this->getTagRepositoryMock();
27 $this->entryRepository
= $this->getEntryRepositoryMock();
28 $this->logger
= $this->getLogger();
29 $this->handler
= new TestHandler();
30 $this->logger
->pushHandler($this->handler
);
32 $this->tagger
= new RuleBasedTagger($this->rulerz
, $this->tagRepository
, $this->entryRepository
, $this->logger
);
35 public function testTagWithNoRule()
37 $entry = new Entry($this->getUser());
39 $this->tagger
->tag($entry);
41 $this->assertTrue($entry->getTags()->isEmpty());
42 $records = $this->handler
->getRecords();
43 $this->assertCount(0, $records);
46 public function testTagWithNoMatchingRule()
48 $taggingRule = $this->getTaggingRule('rule as string', ['foo', 'bar']);
49 $user = $this->getUser([$taggingRule]);
50 $entry = new Entry($user);
53 ->expects($this->once())
55 ->with($entry, 'rule as string')
58 $this->tagger
->tag($entry);
60 $this->assertTrue($entry->getTags()->isEmpty());
61 $records = $this->handler
->getRecords();
62 $this->assertCount(0, $records);
65 public function testTagWithAMatchingRule()
67 $taggingRule = $this->getTaggingRule('rule as string', ['foo', 'bar']);
68 $user = $this->getUser([$taggingRule]);
69 $entry = new Entry($user);
72 ->expects($this->once())
74 ->with($entry, 'rule as string')
77 $this->tagger
->tag($entry);
79 $this->assertFalse($entry->getTags()->isEmpty());
81 $tags = $entry->getTags();
82 $this->assertSame('foo', $tags[0]->getLabel());
83 $this->assertSame('bar', $tags[1]->getLabel());
85 $records = $this->handler
->getRecords();
86 $this->assertCount(1, $records);
89 public function testTagWithAMixOfMatchingRules()
91 $taggingRule = $this->getTaggingRule('bla bla', ['hey']);
92 $otherTaggingRule = $this->getTaggingRule('rule as string', ['foo']);
94 $user = $this->getUser([$taggingRule, $otherTaggingRule]);
95 $entry = new Entry($user);
99 ->will($this->onConsecutiveCalls(false, true));
101 $this->tagger
->tag($entry);
103 $this->assertFalse($entry->getTags()->isEmpty());
105 $tags = $entry->getTags();
106 $this->assertSame('foo', $tags[0]->getLabel());
107 $records = $this->handler
->getRecords();
108 $this->assertCount(1, $records);
111 public function testWhenTheTagExists()
113 $taggingRule = $this->getTaggingRule('rule as string', ['foo']);
114 $user = $this->getUser([$taggingRule]);
115 $entry = new Entry($user);
119 ->expects($this->once())
120 ->method('satisfies')
121 ->with($entry, 'rule as string')
125 ->expects($this->once())
126 // the method `findOneByLabel` doesn't exist, EntityRepository will then call `_call` method
127 // to magically call the `findOneBy` with ['label' => 'foo']
131 $this->tagger
->tag($entry);
133 $this->assertFalse($entry->getTags()->isEmpty());
135 $tags = $entry->getTags();
136 $this->assertSame($tag, $tags[0]);
137 $records = $this->handler
->getRecords();
138 $this->assertCount(1, $records);
141 public function testSameTagWithDifferentfMatchingRules()
143 $taggingRule = $this->getTaggingRule('bla bla', ['hey']);
144 $otherTaggingRule = $this->getTaggingRule('rule as string', ['hey']);
146 $user = $this->getUser([$taggingRule, $otherTaggingRule]);
147 $entry = new Entry($user);
150 ->method('satisfies')
153 $this->tagger
->tag($entry);
155 $this->assertFalse($entry->getTags()->isEmpty());
157 $tags = $entry->getTags();
158 $this->assertCount(1, $tags);
159 $records = $this->handler
->getRecords();
160 $this->assertCount(2, $records);
163 public function testTagAllEntriesForAUser()
165 $taggingRule = $this->getTaggingRule('bla bla', ['hey']);
167 $user = $this->getUser([$taggingRule]);
170 ->method('satisfies')
175 ->willReturn([new Entry($user), new Entry($user)]);
177 $entries = $this->tagger
->tagAllForUser($user);
179 $this->assertCount(2, $entries);
181 foreach ($entries as $entry) {
182 $tags = $entry->getTags();
184 $this->assertCount(1, $tags);
185 $this->assertSame('hey', $tags[0]->getLabel());
189 private function getUser(array $taggingRules = [])
192 $config = new Config($user);
194 $user->setConfig($config);
196 foreach ($taggingRules as $rule) {
197 $config->addTaggingRule($rule);
203 private function getTaggingRule($rule, array $tags)
205 $taggingRule = new TaggingRule();
206 $taggingRule->setRule($rule);
207 $taggingRule->setTags($tags);
212 private function getRulerZMock()
214 return $this->getMockBuilder('RulerZ\RulerZ')
215 ->disableOriginalConstructor()
219 private function getTagRepositoryMock()
221 return $this->getMockBuilder('Wallabag\CoreBundle\Repository\TagRepository')
222 ->disableOriginalConstructor()
226 private function getEntryRepositoryMock()
228 return $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
229 ->disableOriginalConstructor()
233 private function getLogger()
235 return new Logger('foo');