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

ColumnDeleteTest.php « Filter « DataTable « Unit « PHPUnit « tests - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 79fe1619ccab2a1ae69db9f613ddae700a933870 (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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?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\DataTable\Filter;

use Piwik\DataTable;
use Piwik\DataTable\Row;

class ColumnDeleteTest extends \PHPUnit_Framework_TestCase
{
    private $filter = 'ColumnDelete';

    protected function makeDataTable($appendRowWithSubtable = true)
    {
        $table = new DataTable();
        $table->addRowFromArray(array(Row::COLUMNS => array('label' => 'row1', 'visits' => 0, 'arrayColumn' => array('visits' => 0, 'columnWithin' => 10))));
        $table->addRowFromArray(array(Row::COLUMNS => array('label' => 'row2', 'visits' => 1, 'arrayColumn' => array('visits' => 1, 'columnWithin' => 11))));
        $table->addRowFromArray(array(Row::COLUMNS => array('label' => 'row3', 'visits' => 2, 'arrayColumn' => array('visits' => 2, 'columnWithin' => 12))));

        if($appendRowWithSubtable) {
            $subTable = $this->makeDataTable( $appendRowWithSubtable = false );
            $table->addRowFromArray(array(
                Row::COLUMNS => array('label' => 'row4', 'visits' => 3, 'arrayColumn' => array('visits' => 3, 'columnWithin' => 13)),
                Row::DATATABLE_ASSOCIATED => $subTable
            ));
        }
        return $table;
    }

    protected function makeDataTable_withoutVisitsColumn($appendRowWithSubtable = true)
    {
        $table = new DataTable();
        $table->addRowFromArray(array(Row::COLUMNS => array('label' => 'row1', 'arrayColumn' => array('columnWithin' => 10))));
        $table->addRowFromArray(array(Row::COLUMNS => array('label' => 'row2', 'arrayColumn' => array('columnWithin' => 11))));
        $table->addRowFromArray(array(Row::COLUMNS => array('label' => 'row3', 'arrayColumn' => array('columnWithin' => 12))));

        if($appendRowWithSubtable) {
            $subTable = $this->makeDataTable_withoutVisitsColumn( $appendRowWithSubtable = false );
            $table->addRowFromArray(array(
                Row::COLUMNS => array('label' => 'row4', 'arrayColumn' => array('columnWithin' => 13)),
                Row::DATATABLE_ASSOCIATED => $subTable
            ));
        }
        return $table;
    }

    protected function makeDataTable_showOnlyVisitColumn($appendRowWithSubtable = true)
    {
        $table = new DataTable();
        $table->addRowFromArray(array(Row::COLUMNS => array( 'label' => 'row1', 'visits' => 0)));
        $table->addRowFromArray(array(Row::COLUMNS => array( 'label' => 'row2', 'visits' => 1)));
        $table->addRowFromArray(array(Row::COLUMNS => array( 'label' => 'row3', 'visits' => 2)));

        if($appendRowWithSubtable) {
            $subTable = $this->makeDataTable_showOnlyVisitColumn( $appendRowWithSubtable = false );
            $table->addRowFromArray(array(
                Row::COLUMNS => array('label' => 'row4', 'visits' => 3),
                Row::DATATABLE_ASSOCIATED => $subTable
            ));
        }
        return $table;
    }

    protected function assertSameDataTable(DataTable $table1, DataTable $table2)
    {
        $this->assertTrue(DataTable::isEqual($table1, $table2), var_export($table1->getRows(), true) .' different from ' . var_export($table2, true));
    }

    public function test_filter_DataTable_removeNonExistingColumn()
    {
        $table = $this->makeDataTable();
        $table->filter($this->filter, array('does-not-exist'));

        $this->assertSameDataTable($this->makeDataTable(), $table);
    }

    public function test_filter_DataTable_removeExistingColumn()
    {
        $table = $this->makeDataTable();
        $table->filter($this->filter, array('visits'));

        $this->assertSameDataTable($this->makeDataTable_withoutVisitsColumn(), $table);
    }

    public function test_filter_DataTable_keepColumn()
    {
        $table = $this->makeDataTable();
        $table->filter($this->filter, array($hide = '', $show = 'visits'));

        $this->assertSameDataTable($this->makeDataTable_showOnlyVisitColumn(), $table);
    }

    public function test_filter_array_removeNonExistingColumn()
    {
        $array = $this->makeArray();

        $columnDelete = new DataTable\Filter\ColumnDelete(new DataTable(), $hideColumns = 'non-existing-column', $showColumns = array());
        $filteredArray = $columnDelete->filter($array);

        $this->assertSame($array, $filteredArray);
    }

    public function test_filter_array_removeExistingColumn()
    {
        $columnDelete = new DataTable\Filter\ColumnDelete(new DataTable(), $hideColumns = 'visits', $showColumns = array());
        $filteredArray = $columnDelete->filter($this->makeArray());

        $this->assertSame($this->makeArray_withoutVisitsColumns(), $filteredArray);
    }

    public function test_filter_array_keepColumn()
    {
        $columnDelete = new DataTable\Filter\ColumnDelete(new DataTable(), $hideColumns = '', $showColumns = 'visits');
        $filteredArray = $columnDelete->filter($this->makeArray());

        $this->assertSame($this->makeArray_showVisitsColumns(), $filteredArray);
    }

    /**
     * @return array
     */
    protected function makeArray()
    {
        $array = array(
            array('label' => 'row1', 'visits' => 1, 'arrayColumn' => array('visits' => 0, 'columnWithin' => 10)),
            array('label' => 'row2', 'visits' => 2, 'arrayColumn' => array('visits' => 1, 'columnWithin' => 11)),
            array('label' => 'row3', 'visits' => 3, 'arrayColumn' => array('visits' => 2, 'columnWithin' => 12)),
        );
        return $array;
    }

    /**
     * @return array
     */
    protected function makeArray_withoutVisitsColumns()
    {
        $array = array(
            array('label' => 'row1', 'arrayColumn' => array( 'columnWithin' => 10)),
            array('label' => 'row2', 'arrayColumn' => array( 'columnWithin' => 11)),
            array('label' => 'row3', 'arrayColumn' => array( 'columnWithin' => 12)),
        );
        return $array;
    }

    /**
     * @return array
     */
    protected function makeArray_showVisitsColumns()
    {
        $array = array(
            array('label' => 'row1', 'visits' => 1),
            array('label' => 'row2', 'visits' => 2),
            array('label' => 'row3', 'visits' => 3),
        );
        return $array;
    }

}