2015-10-17 16:34:18 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Wallabag\CoreBundle\Tests\Helper;
|
|
|
|
|
|
|
|
use Wallabag\CoreBundle\Entity\Config;
|
|
|
|
use Wallabag\CoreBundle\Entity\Entry;
|
|
|
|
use Wallabag\CoreBundle\Entity\Tag;
|
|
|
|
use Wallabag\CoreBundle\Entity\TaggingRule;
|
|
|
|
use Wallabag\UserBundle\Entity\User;
|
|
|
|
use Wallabag\CoreBundle\Helper\RuleBasedTagger;
|
|
|
|
|
|
|
|
class RuleBasedTaggerTest extends \PHPUnit_Framework_TestCase
|
|
|
|
{
|
|
|
|
private $rulerz;
|
2015-10-24 13:11:06 +00:00
|
|
|
private $tagRepository;
|
|
|
|
private $entryRepository;
|
2015-10-17 16:34:18 +00:00
|
|
|
private $tagger;
|
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
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();
|
2015-10-17 16:34:18 +00:00
|
|
|
|
2015-10-24 13:11:06 +00:00
|
|
|
$this->tagger = new RuleBasedTagger($this->rulerz, $this->tagRepository, $this->entryRepository);
|
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());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testTagWithNoMatchingRule()
|
|
|
|
{
|
|
|
|
$taggingRule = $this->getTaggingRule('rule as string', array('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());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testTagWithAMatchingRule()
|
|
|
|
{
|
|
|
|
$taggingRule = $this->getTaggingRule('rule as string', array('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($user, $tags[0]->getUser());
|
|
|
|
$this->assertSame('bar', $tags[1]->getLabel());
|
|
|
|
$this->assertSame($user, $tags[1]->getUser());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testTagWithAMixOfMatchingRules()
|
|
|
|
{
|
2015-12-08 08:20:03 +00:00
|
|
|
$taggingRule = $this->getTaggingRule('bla bla', array('hey'));
|
2015-10-17 16:34:18 +00:00
|
|
|
$otherTaggingRule = $this->getTaggingRule('rule as string', array('foo'));
|
|
|
|
|
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());
|
|
|
|
$this->assertSame($user, $tags[0]->getUser());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testWhenTheTagExists()
|
|
|
|
{
|
|
|
|
$taggingRule = $this->getTaggingRule('rule as string', array('foo'));
|
2015-12-08 08:20:03 +00:00
|
|
|
$user = $this->getUser([$taggingRule]);
|
|
|
|
$entry = new Entry($user);
|
|
|
|
$tag = new Tag($user);
|
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())
|
|
|
|
->method('findOneByLabelAndUserId')
|
|
|
|
->willReturn($tag);
|
|
|
|
|
|
|
|
$this->tagger->tag($entry);
|
|
|
|
|
|
|
|
$this->assertFalse($entry->getTags()->isEmpty());
|
|
|
|
|
|
|
|
$tags = $entry->getTags();
|
|
|
|
$this->assertSame($tag, $tags[0]);
|
|
|
|
}
|
|
|
|
|
2015-12-27 21:26:49 +00:00
|
|
|
public function testSameTagWithDifferentfMatchingRules()
|
|
|
|
{
|
|
|
|
$taggingRule = $this->getTaggingRule('bla bla', array('hey'));
|
|
|
|
$otherTaggingRule = $this->getTaggingRule('rule as string', array('hey'));
|
|
|
|
|
|
|
|
$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);
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
}
|
2015-10-17 16:34:18 +00:00
|
|
|
}
|