]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | ||
3 | namespace Tests\Wallabag\CoreBundle\Helper; | |
4 | ||
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; | |
11 | ||
12 | class TagsAssignerTest extends TestCase | |
13 | { | |
14 | public function testAssignTagsWithArrayAndExtraSpaces() | |
15 | { | |
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()); | |
24 | $this->assertSame('tag1', $entry->getTags()[0]->getLabel()); | |
25 | $this->assertSame('tag2', $entry->getTags()[1]->getLabel()); | |
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()); | |
38 | $this->assertSame('tag1', $entry->getTags()[0]->getLabel()); | |
39 | $this->assertSame('tag2', $entry->getTags()[1]->getLabel()); | |
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()); | |
80 | $this->assertSame('tag1', $entry->getTags()[0]->getLabel()); | |
81 | $this->assertSame('tag2', $entry->getTags()[1]->getLabel()); | |
82 | } | |
83 | ||
84 | public function testAssignTagsNotFlushed() | |
85 | { | |
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()); | |
100 | $this->assertSame('tag1', $entry->getTags()[0]->getLabel()); | |
101 | } | |
102 | ||
103 | private function getTagRepositoryMock() | |
104 | { | |
105 | return $this->getMockBuilder(TagRepository::class) | |
106 | ->disableOriginalConstructor() | |
107 | ->getMock(); | |
108 | } | |
109 | } |