2015-10-17 16:34:18 +00:00
|
|
|
<?php
|
|
|
|
|
2016-06-01 19:27:35 +00:00
|
|
|
namespace Tests\Wallabag\CoreBundle\Helper;
|
2015-10-17 16:34:18 +00:00
|
|
|
|
2017-05-12 13:01:18 +00:00
|
|
|
use Monolog\Handler\TestHandler;
|
|
|
|
use Monolog\Logger;
|
2017-12-16 21:17:42 +00:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2015-10-17 16:34:18 +00:00
|
|
|
use Wallabag\CoreBundle\Entity\Config;
|
|
|
|
use Wallabag\CoreBundle\Entity\Entry;
|
|
|
|
use Wallabag\CoreBundle\Entity\Tag;
|
|
|
|
use Wallabag\CoreBundle\Entity\TaggingRule;
|
|
|
|
use Wallabag\CoreBundle\Helper\RuleBasedTagger;
|
2015-12-22 09:16:34 +00:00
|
|
|
use Wallabag\UserBundle\Entity\User;
|
2015-10-17 16:34:18 +00:00
|
|
|
|
2017-12-16 21:17:42 +00:00
|
|
|
class RuleBasedTaggerTest extends TestCase
|
2015-10-17 16:34:18 +00:00
|
|
|
{
|
|
|
|
private $rulerz;
|
2015-10-24 13:11:06 +00:00
|
|
|
private $tagRepository;
|
|
|
|
private $entryRepository;
|
2015-10-17 16:34:18 +00:00
|
|
|
private $tagger;
|
2017-05-12 13:01:18 +00:00
|
|
|
private $logger;
|
|
|
|
private $handler;
|
2015-10-17 16:34:18 +00:00
|
|
|
|
2020-12-08 08:17:10 +00:00
|
|
|
protected function setUp(): void
|
2015-10-17 16:34:18 +00:00
|
|
|
{
|
2015-12-08 08:20:03 +00:00
|
|
|
$this->rulerz = $this->getRulerZMock();
|
|
|
|
$this->tagRepository = $this->getTagRepositoryMock();
|
2015-10-24 13:11:06 +00:00
|
|
|
$this->entryRepository = $this->getEntryRepositoryMock();
|
2017-05-12 13:01:18 +00:00
|
|
|
$this->logger = $this->getLogger();
|
|
|
|
$this->handler = new TestHandler();
|
|
|
|
$this->logger->pushHandler($this->handler);
|
2015-10-17 16:34:18 +00:00
|
|
|
|
2017-05-12 13:01:18 +00:00
|
|
|
$this->tagger = new RuleBasedTagger($this->rulerz, $this->tagRepository, $this->entryRepository, $this->logger);
|
2015-10-17 16:34:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testTagWithNoRule()
|
|
|
|
{
|
|
|
|
$entry = new Entry($this->getUser());
|
|
|
|
|
|
|
|
$this->tagger->tag($entry);
|
|
|
|
|
|
|
|
$this->assertTrue($entry->getTags()->isEmpty());
|
2017-05-12 13:01:18 +00:00
|
|
|
$records = $this->handler->getRecords();
|
|
|
|
$this->assertCount(0, $records);
|
2015-10-17 16:34:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testTagWithNoMatchingRule()
|
|
|
|
{
|
2016-04-12 09:36:01 +00:00
|
|
|
$taggingRule = $this->getTaggingRule('rule as string', ['foo', 'bar']);
|
2015-12-08 08:20:03 +00:00
|
|
|
$user = $this->getUser([$taggingRule]);
|
|
|
|
$entry = new Entry($user);
|
2015-10-17 16:34:18 +00:00
|
|
|
|
|
|
|
$this->rulerz
|
|
|
|
->expects($this->once())
|
|
|
|
->method('satisfies')
|
|
|
|
->with($entry, 'rule as string')
|
|
|
|
->willReturn(false);
|
|
|
|
|
|
|
|
$this->tagger->tag($entry);
|
|
|
|
|
|
|
|
$this->assertTrue($entry->getTags()->isEmpty());
|
2017-05-12 13:01:18 +00:00
|
|
|
$records = $this->handler->getRecords();
|
|
|
|
$this->assertCount(0, $records);
|
2015-10-17 16:34:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testTagWithAMatchingRule()
|
|
|
|
{
|
2016-04-12 09:36:01 +00:00
|
|
|
$taggingRule = $this->getTaggingRule('rule as string', ['foo', 'bar']);
|
2015-12-08 08:20:03 +00:00
|
|
|
$user = $this->getUser([$taggingRule]);
|
|
|
|
$entry = new Entry($user);
|
2015-10-17 16:34:18 +00:00
|
|
|
|
|
|
|
$this->rulerz
|
|
|
|
->expects($this->once())
|
|
|
|
->method('satisfies')
|
|
|
|
->with($entry, 'rule as string')
|
|
|
|
->willReturn(true);
|
|
|
|
|
|
|
|
$this->tagger->tag($entry);
|
|
|
|
|
|
|
|
$this->assertFalse($entry->getTags()->isEmpty());
|
|
|
|
|
|
|
|
$tags = $entry->getTags();
|
|
|
|
$this->assertSame('foo', $tags[0]->getLabel());
|
|
|
|
$this->assertSame('bar', $tags[1]->getLabel());
|
2017-05-12 13:01:18 +00:00
|
|
|
|
|
|
|
$records = $this->handler->getRecords();
|
|
|
|
$this->assertCount(1, $records);
|
2015-10-17 16:34:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testTagWithAMixOfMatchingRules()
|
|
|
|
{
|
2016-04-12 09:36:01 +00:00
|
|
|
$taggingRule = $this->getTaggingRule('bla bla', ['hey']);
|
|
|
|
$otherTaggingRule = $this->getTaggingRule('rule as string', ['foo']);
|
2015-10-17 16:34:18 +00:00
|
|
|
|
2015-12-08 08:20:03 +00:00
|
|
|
$user = $this->getUser([$taggingRule, $otherTaggingRule]);
|
2015-10-17 16:34:18 +00:00
|
|
|
$entry = new Entry($user);
|
|
|
|
|
|
|
|
$this->rulerz
|
|
|
|
->method('satisfies')
|
|
|
|
->will($this->onConsecutiveCalls(false, true));
|
|
|
|
|
|
|
|
$this->tagger->tag($entry);
|
|
|
|
|
|
|
|
$this->assertFalse($entry->getTags()->isEmpty());
|
|
|
|
|
|
|
|
$tags = $entry->getTags();
|
|
|
|
$this->assertSame('foo', $tags[0]->getLabel());
|
2017-05-12 13:01:18 +00:00
|
|
|
$records = $this->handler->getRecords();
|
|
|
|
$this->assertCount(1, $records);
|
2015-10-17 16:34:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testWhenTheTagExists()
|
|
|
|
{
|
2016-04-12 09:36:01 +00:00
|
|
|
$taggingRule = $this->getTaggingRule('rule as string', ['foo']);
|
2015-12-08 08:20:03 +00:00
|
|
|
$user = $this->getUser([$taggingRule]);
|
|
|
|
$entry = new Entry($user);
|
2015-12-29 13:50:52 +00:00
|
|
|
$tag = new Tag();
|
2015-10-17 16:34:18 +00:00
|
|
|
|
|
|
|
$this->rulerz
|
|
|
|
->expects($this->once())
|
|
|
|
->method('satisfies')
|
|
|
|
->with($entry, 'rule as string')
|
|
|
|
->willReturn(true);
|
|
|
|
|
2015-10-24 13:11:06 +00:00
|
|
|
$this->tagRepository
|
2015-10-17 16:34:18 +00:00
|
|
|
->expects($this->once())
|
2015-12-29 13:50:52 +00:00
|
|
|
// the method `findOneByLabel` doesn't exist, EntityRepository will then call `_call` method
|
|
|
|
// to magically call the `findOneBy` with ['label' => 'foo']
|
|
|
|
->method('__call')
|
2015-10-17 16:34:18 +00:00
|
|
|
->willReturn($tag);
|
|
|
|
|
|
|
|
$this->tagger->tag($entry);
|
|
|
|
|
|
|
|
$this->assertFalse($entry->getTags()->isEmpty());
|
|
|
|
|
|
|
|
$tags = $entry->getTags();
|
2015-12-29 14:04:46 +00:00
|
|
|
$this->assertSame($tag, $tags[0]);
|
2017-05-12 13:01:18 +00:00
|
|
|
$records = $this->handler->getRecords();
|
|
|
|
$this->assertCount(1, $records);
|
2015-10-17 16:34:18 +00:00
|
|
|
}
|
|
|
|
|
2015-12-27 21:26:49 +00:00
|
|
|
public function testSameTagWithDifferentfMatchingRules()
|
|
|
|
{
|
2016-04-12 09:36:01 +00:00
|
|
|
$taggingRule = $this->getTaggingRule('bla bla', ['hey']);
|
|
|
|
$otherTaggingRule = $this->getTaggingRule('rule as string', ['hey']);
|
2015-12-27 21:26:49 +00:00
|
|
|
|
|
|
|
$user = $this->getUser([$taggingRule, $otherTaggingRule]);
|
|
|
|
$entry = new Entry($user);
|
|
|
|
|
|
|
|
$this->rulerz
|
|
|
|
->method('satisfies')
|
|
|
|
->willReturn(true);
|
|
|
|
|
|
|
|
$this->tagger->tag($entry);
|
|
|
|
|
|
|
|
$this->assertFalse($entry->getTags()->isEmpty());
|
|
|
|
|
|
|
|
$tags = $entry->getTags();
|
|
|
|
$this->assertCount(1, $tags);
|
2017-05-12 13:01:18 +00:00
|
|
|
$records = $this->handler->getRecords();
|
|
|
|
$this->assertCount(2, $records);
|
2015-12-27 21:26:49 +00:00
|
|
|
}
|
|
|
|
|
2015-12-27 22:48:57 +00:00
|
|
|
public function testTagAllEntriesForAUser()
|
|
|
|
{
|
2016-04-12 09:36:01 +00:00
|
|
|
$taggingRule = $this->getTaggingRule('bla bla', ['hey']);
|
2015-12-27 22:48:57 +00:00
|
|
|
|
|
|
|
$user = $this->getUser([$taggingRule]);
|
|
|
|
|
|
|
|
$this->rulerz
|
|
|
|
->method('satisfies')
|
|
|
|
->willReturn(true);
|
|
|
|
|
|
|
|
$this->rulerz
|
|
|
|
->method('filter')
|
2016-04-12 09:36:01 +00:00
|
|
|
->willReturn([new Entry($user), new Entry($user)]);
|
2015-12-27 22:48:57 +00:00
|
|
|
|
|
|
|
$entries = $this->tagger->tagAllForUser($user);
|
|
|
|
|
|
|
|
$this->assertCount(2, $entries);
|
|
|
|
|
|
|
|
foreach ($entries as $entry) {
|
|
|
|
$tags = $entry->getTags();
|
|
|
|
|
|
|
|
$this->assertCount(1, $tags);
|
2017-07-01 07:52:38 +00:00
|
|
|
$this->assertSame('hey', $tags[0]->getLabel());
|
2015-12-27 22:48:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-17 16:34:18 +00:00
|
|
|
private function getUser(array $taggingRules = [])
|
|
|
|
{
|
2015-12-08 08:20:03 +00:00
|
|
|
$user = new User();
|
2015-10-17 16:34:18 +00:00
|
|
|
$config = new Config($user);
|
|
|
|
|
|
|
|
$user->setConfig($config);
|
|
|
|
|
|
|
|
foreach ($taggingRules as $rule) {
|
|
|
|
$config->addTaggingRule($rule);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getTaggingRule($rule, array $tags)
|
|
|
|
{
|
|
|
|
$taggingRule = new TaggingRule();
|
|
|
|
$taggingRule->setRule($rule);
|
|
|
|
$taggingRule->setTags($tags);
|
|
|
|
|
|
|
|
return $taggingRule;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getRulerZMock()
|
|
|
|
{
|
|
|
|
return $this->getMockBuilder('RulerZ\RulerZ')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getTagRepositoryMock()
|
|
|
|
{
|
|
|
|
return $this->getMockBuilder('Wallabag\CoreBundle\Repository\TagRepository')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
}
|
2015-10-24 13:11:06 +00:00
|
|
|
|
|
|
|
private function getEntryRepositoryMock()
|
|
|
|
{
|
|
|
|
return $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
}
|
2017-05-12 11:47:53 +00:00
|
|
|
|
|
|
|
private function getLogger()
|
|
|
|
{
|
2017-05-12 13:01:18 +00:00
|
|
|
return new Logger('foo');
|
2017-05-12 11:47:53 +00:00
|
|
|
}
|
2015-10-17 16:34:18 +00:00
|
|
|
}
|