3 namespace Tests\Wallabag\CoreBundle\Helper
;
5 use PHPUnit\Framework\TestCase
;
6 use Wallabag\CoreBundle\Entity\Entry
;
7 use Wallabag\CoreBundle\Entity\Tag
;
8 use Wallabag\CoreBundle\Helper\TagsAssigner
;
9 use Wallabag\CoreBundle\Repository\TagRepository
;
10 use Wallabag\UserBundle\Entity\User
;
12 class TagsAssignerTest
extends TestCase
14 public function testAssignTagsWithArrayAndExtraSpaces()
16 $tagRepo = $this->getTagRepositoryMock();
17 $tagsAssigner = new TagsAssigner($tagRepo);
19 $entry = new Entry(new User());
21 $tagsAssigner->assignTagsToEntry($entry, [' tag1', 'tag2 ']);
23 $this->assertCount(2, $entry->getTags());
24 $this->assertSame('tag1', $entry->getTags()[0]->getLabel());
25 $this->assertSame('tag2', $entry->getTags()[1]->getLabel());
28 public function testAssignTagsWithString()
30 $tagRepo = $this->getTagRepositoryMock();
31 $tagsAssigner = new TagsAssigner($tagRepo);
33 $entry = new Entry(new User());
35 $tagsAssigner->assignTagsToEntry($entry, 'tag1, tag2');
37 $this->assertCount(2, $entry->getTags());
38 $this->assertSame('tag1', $entry->getTags()[0]->getLabel());
39 $this->assertSame('tag2', $entry->getTags()[1]->getLabel());
42 public function testAssignTagsWithEmptyArray()
44 $tagRepo = $this->getTagRepositoryMock();
45 $tagsAssigner = new TagsAssigner($tagRepo);
47 $entry = new Entry(new User());
49 $tagsAssigner->assignTagsToEntry($entry, []);
51 $this->assertCount(0, $entry->getTags());
54 public function testAssignTagsWithEmptyString()
56 $tagRepo = $this->getTagRepositoryMock();
57 $tagsAssigner = new TagsAssigner($tagRepo);
59 $entry = new Entry(new User());
61 $tagsAssigner->assignTagsToEntry($entry, '');
63 $this->assertCount(0, $entry->getTags());
66 public function testAssignTagsAlreadyAssigned()
68 $tagRepo = $this->getTagRepositoryMock();
69 $tagsAssigner = new TagsAssigner($tagRepo);
71 $tagEntity = new Tag();
72 $tagEntity->setLabel('tag1');
74 $entry = new Entry(new User());
75 $entry->addTag($tagEntity);
77 $tagsAssigner->assignTagsToEntry($entry, 'tag1, tag2');
79 $this->assertCount(2, $entry->getTags());
80 $this->assertSame('tag1', $entry->getTags()[0]->getLabel());
81 $this->assertSame('tag2', $entry->getTags()[1]->getLabel());
84 public function testAssignTagsNotFlushed()
86 $tagRepo = $this->getTagRepositoryMock();
87 $tagRepo->expects($this->never())
90 $tagsAssigner = new TagsAssigner($tagRepo);
92 $tagEntity = new Tag();
93 $tagEntity->setLabel('tag1');
95 $entry = new Entry(new User());
97 $tagsAssigner->assignTagsToEntry($entry, 'tag1', [$tagEntity]);
99 $this->assertCount(1, $entry->getTags());
100 $this->assertSame('tag1', $entry->getTags()[0]->getLabel());
103 private function getTagRepositoryMock()
105 return $this->getMockBuilder(TagRepository
::class)
106 ->disableOriginalConstructor()