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