Welcome to mirror list, hosted at ThFree Co, Russian Federation.

CategoryTest.php « Category « Unit « PHPUnit « tests - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: de4b93165760eeffc5026921361a1d1672e9de57 (plain)
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
/**
 * Piwik - free/libre analytics platform
 *
 * @link http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

namespace Piwik\Tests\Unit\Category;

use Piwik\Cache;
use Piwik\Category\Category;
use Piwik\Category\Subcategory;

/**
 * @group Category
 * @group CategoryTest
 */
class CategoryTest extends \PHPUnit_Framework_TestCase
{
    /**
     * @var Category
     */
    private $category;

    public function setUp()
    {
        parent::setUp();
        $this->category = new Category();
    }

    public function test_order_set_get()
    {
        $this->category->setOrder(99);
        $this->assertSame(99, $this->category->getOrder());

        $this->category->setOrder('98');
        $this->assertSame(98, $this->category->getOrder());
    }

    public function test_getOrder_shouldReturnADefaultValue()
    {
        $this->assertSame(99, $this->category->getOrder());
    }

    public function test_id_set_get()
    {
        $this->category->setId('myCustomId');
        $this->assertSame('myCustomId', $this->category->getId());
    }

    public function test_getId_shouldBeEmptyStringByDefault()
    {
        $this->assertSame('', $this->category->getId());
    }

    public function test_getSubcategories_ShouldReturnAnEmptyArray_ByDefault()
    {
        $this->assertSame(array(), $this->category->getSubcategories());
    }

    public function test_addSubcategory_ShouldActuallyAddAndReturnSubcategories()
    {
        $subcategory1 = $this->createSubcategory('id1', 'name1');
        $subcategory2 = $this->createSubcategory('id2', 'name2');

        $this->category->addSubcategory($subcategory1);
        $this->category->addSubcategory($subcategory2);

        $this->assertSame(array($subcategory1, $subcategory2), $this->category->getSubcategories());
    }

    /**
     * @expectedException \Exception
     * @expectedExceptionMessage Subcategory id1 already exists
     */
    public function test_addSubcategory_ShouldThrowException_WhenAddingSubcategoryWithSameIdTwice()
    {
        $subcategory1 = $this->createSubcategory('id1', 'name1');
        $subcategory2 = $this->createSubcategory('id1', 'name2');

        $this->category->addSubcategory($subcategory1);
        $this->category->addSubcategory($subcategory2);
    }

    public function test_hasSubcategories_ShouldDetectIfSubcategoriesArePresent()
    {
        $this->assertFalse($this->category->hasSubCategories());
        $this->addSubcategories(array('myid' => 'myname'));
        $this->assertTrue($this->category->hasSubCategories());
    }

    public function test_getSubcategory_ShouldNotFindASubCategoryById_IfSuchCategoryDoesNotExist()
    {
        $this->assertNull($this->category->getSubcategory('myid'));
    }

    public function test_getSubcategory_ShouldFindAnExistingSubCategoryById()
    {
        $this->addSubcategories(array('myid' => 'myname', 'myid2' => 'myname2'));

        $subcategory = $this->category->getSubcategory('myid2');
        $this->assertTrue($subcategory instanceof Subcategory);
        $this->assertSame('myname2', $subcategory->getName());
    }

    public function test_getSubcategory_ShouldNotFindASubcategoryByName()
    {
        $this->addSubcategories(array('myid' => 'myname'));

        $this->assertNull($this->category->getSubcategory('myname'));
    }

    public function test_hasSubcategory_ShouldActuallyAddTheConfig()
    {
        $this->assertFalse($this->category->hasSubcategory('myid2'));

        $this->addSubcategories(array('myid' => 'myname', 'myid2' => 'myname2'));

        $this->assertTrue($this->category->hasSubcategory('myid2'));
        $this->assertFalse($this->category->hasSubcategory('myname'));
        $this->assertFalse($this->category->hasSubcategory('myname2'));
        $this->assertFalse($this->category->hasSubcategory('mySomething'));
    }

    private function addSubcategories($subcategories)
    {
        foreach ($subcategories as $id => $name) {
            $this->category->addSubcategory($this->createSubcategory($id, $name));
        }
    }

    private function createSubcategory($subcategoryId, $subcategoryName)
    {
        $config = new Subcategory();
        $config->setId($subcategoryId);
        $config->setName($subcategoryName);

        return $config;
    }
}