wallabag/tests/Helper/TagsAssignerTest.php

110 lines
3.1 KiB
PHP
Raw Normal View History

<?php
namespace Tests\Wallabag\CoreBundle\Helper;
2017-12-16 21:17:42 +00:00
use PHPUnit\Framework\TestCase;
use Wallabag\CoreBundle\Entity\Entry;
use Wallabag\CoreBundle\Entity\Tag;
2023-12-30 22:32:36 +00:00
use Wallabag\CoreBundle\Entity\User;
use Wallabag\CoreBundle\Helper\TagsAssigner;
use Wallabag\CoreBundle\Repository\TagRepository;
2017-12-16 21:17:42 +00:00
class TagsAssignerTest extends TestCase
{
public function testAssignTagsWithArrayAndExtraSpaces()
{
$tagRepo = $this->getTagRepositoryMock();
$tagsAssigner = new TagsAssigner($tagRepo);
$entry = new Entry(new User());
$tagsAssigner->assignTagsToEntry($entry, [' tag1', 'tag2 ']);
$this->assertCount(2, $entry->getTags());
2017-07-01 07:52:38 +00:00
$this->assertSame('tag1', $entry->getTags()[0]->getLabel());
$this->assertSame('tag2', $entry->getTags()[1]->getLabel());
}
public function testAssignTagsWithString()
{
$tagRepo = $this->getTagRepositoryMock();
$tagsAssigner = new TagsAssigner($tagRepo);
$entry = new Entry(new User());
$tagsAssigner->assignTagsToEntry($entry, 'tag1, tag2');
$this->assertCount(2, $entry->getTags());
2017-07-01 07:52:38 +00:00
$this->assertSame('tag1', $entry->getTags()[0]->getLabel());
$this->assertSame('tag2', $entry->getTags()[1]->getLabel());
}
public function testAssignTagsWithEmptyArray()
{
$tagRepo = $this->getTagRepositoryMock();
$tagsAssigner = new TagsAssigner($tagRepo);
$entry = new Entry(new User());
$tagsAssigner->assignTagsToEntry($entry, []);
$this->assertCount(0, $entry->getTags());
}
public function testAssignTagsWithEmptyString()
{
$tagRepo = $this->getTagRepositoryMock();
$tagsAssigner = new TagsAssigner($tagRepo);
$entry = new Entry(new User());
$tagsAssigner->assignTagsToEntry($entry, '');
$this->assertCount(0, $entry->getTags());
}
public function testAssignTagsAlreadyAssigned()
{
$tagRepo = $this->getTagRepositoryMock();
$tagsAssigner = new TagsAssigner($tagRepo);
$tagEntity = new Tag();
$tagEntity->setLabel('tag1');
$entry = new Entry(new User());
$entry->addTag($tagEntity);
$tagsAssigner->assignTagsToEntry($entry, 'tag1, tag2');
$this->assertCount(2, $entry->getTags());
2017-07-01 07:52:38 +00:00
$this->assertSame('tag1', $entry->getTags()[0]->getLabel());
$this->assertSame('tag2', $entry->getTags()[1]->getLabel());
}
public function testAssignTagsNotFlushed()
{
$tagRepo = $this->getTagRepositoryMock();
$tagRepo->expects($this->never())
->method('__call');
$tagsAssigner = new TagsAssigner($tagRepo);
$tagEntity = new Tag();
$tagEntity->setLabel('tag1');
$entry = new Entry(new User());
$tagsAssigner->assignTagsToEntry($entry, 'tag1', [$tagEntity]);
$this->assertCount(1, $entry->getTags());
2017-07-01 07:52:38 +00:00
$this->assertSame('tag1', $entry->getTags()[0]->getLabel());
}
private function getTagRepositoryMock()
{
return $this->getMockBuilder(TagRepository::class)
->disableOriginalConstructor()
->getMock();
}
}