summaryrefslogtreecommitdiffstats
path: root/tests/PlanetTest.php
blob: d4b87b89906dabd791a573f4b852e1b894f8141f (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
<?php

use PHPUnit\Framework\TestCase;

class FoolCategory
{

    protected $name;

    public function __construct($name)
    {
        $this->name = $name;
    }

    public function get_label()
    {
        return $this->name;
    }
}

class FoolItem
{
    protected $categories;

    public function __construct($categories)
    {
        foreach ($categories as $c) {
            $this->categories[] = new FoolCategory($c);
        }
    }

    public function get_categories()
    {
        return $this->categories;
    }
}

class PlanetTest extends TestCase
{

    protected $planet;
    protected $items;

    public function setUp() : void
    {
        $this->planet = new Planet();

        $this->items = array(
            new FoolItem(array('catA', 'catB', 'catC')),
            new FoolItem(array('catB')),
            new FoolItem(array('catA')),
            new FoolItem(array('catC'))
        );
    }

    protected function _after()
    {
        unset($this->planet);
    }

    public function testFilterItemsByCategoryWithInvalidCategory()
    {
        $this->assertEquals(count($this->planet->_filterItemsByCategory($this->items, null)), count($this->items));
        $this->assertEquals(count($this->planet->_filterItemsByCategory($this->items, ' ')), count($this->items));
    }

    public function testFilterItemsByCategoryWithNonUsedCategory()
    {
        $this->assertEquals(count($this->planet->_filterItemsByCategory($this->items, 'catD')), 0);
    }

    public function testFilterItemsByCategoryWithValidCategory()
    {
        $this->assertEquals(count($this->planet->_filterItemsByCategory($this->items, 'catA')), 2);
        $this->assertEquals(count($this->planet->_filterItemsByCategory($this->items, 'catB')), 2);
        $this->assertEquals(count($this->planet->_filterItemsByCategory($this->items, 'catC')), 2);
        $this->assertEquals(count($this->planet->_filterItemsByCategory($this->items, 'CATA')), 2);
    }

    public function testFilterItemsByCategoryWithMultipleCategory()
    {
        $this->assertEquals(count($this->planet->_filterItemsByCategory($this->items, 'catA,catB')), 3);
        $this->assertEquals(count($this->planet->_filterItemsByCategory($this->items, 'catA,catB,catC')), 4);
        $this->assertEquals(count($this->planet->_filterItemsByCategory($this->items, 'catA, catB')), 3);
    }
}