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