aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/Wallabag/CoreBundle/Helper/TagsAssignerTest.php
blob: 475cd3497322e2ada3c04e46c9657b05d0ce412e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php

namespace Tests\Wallabag\CoreBundle\Helper;

use Wallabag\CoreBundle\Entity\Entry;
use Wallabag\CoreBundle\Entity\Tag;
use Wallabag\CoreBundle\Helper\TagsAssigner;
use Wallabag\CoreBundle\Repository\TagRepository;
use Wallabag\UserBundle\Entity\User;

class TagsAssignerTest extends \PHPUnit_Framework_TestCase
{
    public function testAssignTagsWithArrayAndExtraSpaces()
    {
        $tagRepo = $this->getTagRepositoryMock();
        $tagsAssigner = new TagsAssigner($tagRepo);

        $entry = new Entry(new User());

        $tagsAssigner->assignTagsToEntry($entry, ['   tag1', 'tag2   ']);

        $this->assertCount(2, $entry->getTags());
        $this->assertSame('tag1', $entry->getTags()[0]->getLabel());
        $this->assertSame('tag2', $entry->getTags()[1]->getLabel());
    }

    public function testAssignTagsWithString()
    {
        $tagRepo = $this->getTagRepositoryMock();
        $tagsAssigner = new TagsAssigner($tagRepo);

        $entry = new Entry(new User());

        $tagsAssigner->assignTagsToEntry($entry, 'tag1, tag2');

        $this->assertCount(2, $entry->getTags());
        $this->assertSame('tag1', $entry->getTags()[0]->getLabel());
        $this->assertSame('tag2', $entry->getTags()[1]->getLabel());
    }

    public function testAssignTagsWithEmptyArray()
    {
        $tagRepo = $this->getTagRepositoryMock();
        $tagsAssigner = new TagsAssigner($tagRepo);

        $entry = new Entry(new User());

        $tagsAssigner->assignTagsToEntry($entry, []);

        $this->assertCount(0, $entry->getTags());
    }

    public function testAssignTagsWithEmptyString()
    {
        $tagRepo = $this->getTagRepositoryMock();
        $tagsAssigner = new TagsAssigner($tagRepo);

        $entry = new Entry(new User());

        $tagsAssigner->assignTagsToEntry($entry, '');

        $this->assertCount(0, $entry->getTags());
    }

    public function testAssignTagsAlreadyAssigned()
    {
        $tagRepo = $this->getTagRepositoryMock();
        $tagsAssigner = new TagsAssigner($tagRepo);

        $tagEntity = new Tag();
        $tagEntity->setLabel('tag1');

        $entry = new Entry(new User());
        $entry->addTag($tagEntity);

        $tagsAssigner->assignTagsToEntry($entry, 'tag1, tag2');

        $this->assertCount(2, $entry->getTags());
        $this->assertSame('tag1', $entry->getTags()[0]->getLabel());
        $this->assertSame('tag2', $entry->getTags()[1]->getLabel());
    }

    public function testAssignTagsNotFlushed()
    {
        $tagRepo = $this->getTagRepositoryMock();
        $tagRepo->expects($this->never())
            ->method('__call');

        $tagsAssigner = new TagsAssigner($tagRepo);

        $tagEntity = new Tag();
        $tagEntity->setLabel('tag1');

        $entry = new Entry(new User());

        $tagsAssigner->assignTagsToEntry($entry, 'tag1', [$tagEntity]);

        $this->assertCount(1, $entry->getTags());
        $this->assertSame('tag1', $entry->getTags()[0]->getLabel());
    }

    private function getTagRepositoryMock()
    {
        return $this->getMockBuilder(TagRepository::class)
            ->disableOriginalConstructor()
            ->getMock();
    }
}