]> git.immae.eu Git - github/wallabag/wallabag.git/blame - tests/Wallabag/CoreBundle/Helper/TagsAssignerTest.php
Use namespaced PHPUnit classes
[github/wallabag/wallabag.git] / tests / Wallabag / CoreBundle / Helper / TagsAssignerTest.php
CommitLineData
6bc6fb1f
TC
1<?php
2
3namespace Tests\Wallabag\CoreBundle\Helper;
4
bd91bd5c 5use PHPUnit\Framework\TestCase;
6bc6fb1f
TC
6use Wallabag\CoreBundle\Entity\Entry;
7use Wallabag\CoreBundle\Entity\Tag;
8use Wallabag\CoreBundle\Helper\TagsAssigner;
6bc6fb1f 9use Wallabag\CoreBundle\Repository\TagRepository;
f808b016 10use Wallabag\UserBundle\Entity\User;
6bc6fb1f 11
bd91bd5c 12class TagsAssignerTest extends TestCase
6bc6fb1f 13{
6bc6fb1f
TC
14 public function testAssignTagsWithArrayAndExtraSpaces()
15 {
6bc6fb1f
TC
16 $tagRepo = $this->getTagRepositoryMock();
17 $tagsAssigner = new TagsAssigner($tagRepo);
18
19 $entry = new Entry(new User());
20
21 $tagsAssigner->assignTagsToEntry($entry, [' tag1', 'tag2 ']);
22
23 $this->assertCount(2, $entry->getTags());
f808b016
JB
24 $this->assertSame('tag1', $entry->getTags()[0]->getLabel());
25 $this->assertSame('tag2', $entry->getTags()[1]->getLabel());
6bc6fb1f
TC
26 }
27
28 public function testAssignTagsWithString()
29 {
30 $tagRepo = $this->getTagRepositoryMock();
31 $tagsAssigner = new TagsAssigner($tagRepo);
32
33 $entry = new Entry(new User());
34
35 $tagsAssigner->assignTagsToEntry($entry, 'tag1, tag2');
36
37 $this->assertCount(2, $entry->getTags());
f808b016
JB
38 $this->assertSame('tag1', $entry->getTags()[0]->getLabel());
39 $this->assertSame('tag2', $entry->getTags()[1]->getLabel());
6bc6fb1f
TC
40 }
41
42 public function testAssignTagsWithEmptyArray()
43 {
44 $tagRepo = $this->getTagRepositoryMock();
45 $tagsAssigner = new TagsAssigner($tagRepo);
46
47 $entry = new Entry(new User());
48
49 $tagsAssigner->assignTagsToEntry($entry, []);
50
51 $this->assertCount(0, $entry->getTags());
52 }
53
54 public function testAssignTagsWithEmptyString()
55 {
56 $tagRepo = $this->getTagRepositoryMock();
57 $tagsAssigner = new TagsAssigner($tagRepo);
58
59 $entry = new Entry(new User());
60
61 $tagsAssigner->assignTagsToEntry($entry, '');
62
63 $this->assertCount(0, $entry->getTags());
64 }
65
66 public function testAssignTagsAlreadyAssigned()
67 {
68 $tagRepo = $this->getTagRepositoryMock();
69 $tagsAssigner = new TagsAssigner($tagRepo);
70
71 $tagEntity = new Tag();
72 $tagEntity->setLabel('tag1');
73
74 $entry = new Entry(new User());
75 $entry->addTag($tagEntity);
76
77 $tagsAssigner->assignTagsToEntry($entry, 'tag1, tag2');
78
79 $this->assertCount(2, $entry->getTags());
f808b016
JB
80 $this->assertSame('tag1', $entry->getTags()[0]->getLabel());
81 $this->assertSame('tag2', $entry->getTags()[1]->getLabel());
6bc6fb1f
TC
82 }
83
84 public function testAssignTagsNotFlushed()
85 {
6bc6fb1f
TC
86 $tagRepo = $this->getTagRepositoryMock();
87 $tagRepo->expects($this->never())
88 ->method('__call');
89
90 $tagsAssigner = new TagsAssigner($tagRepo);
91
92 $tagEntity = new Tag();
93 $tagEntity->setLabel('tag1');
94
95 $entry = new Entry(new User());
96
97 $tagsAssigner->assignTagsToEntry($entry, 'tag1', [$tagEntity]);
98
99 $this->assertCount(1, $entry->getTags());
f808b016 100 $this->assertSame('tag1', $entry->getTags()[0]->getLabel());
6bc6fb1f
TC
101 }
102
103 private function getTagRepositoryMock()
104 {
105 return $this->getMockBuilder(TagRepository::class)
106 ->disableOriginalConstructor()
107 ->getMock();
108 }
109}