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

MapTest.php « DataTable « Core « PHPUnit « tests - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2ac19275a9a10c1d9f3a9017b95491f276eb3f6d (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
<?php
use Piwik\Config;
use Piwik\DataTable;
use Piwik\DataTable\Manager;
use Piwik\DataTable\Row;

class Test_DataTable_Map extends PHPUnit_Framework_TestCase
{
    public function setUp()
    {
        parent::setUp();
        Config::getInstance()->setTestEnvironment();
        Manager::getInstance()->deleteAll();
    }

    private function createTestDataTable()
    {
        $result = new DataTable();

        $result->addRowsFromArray(array(
                                       array(Row::COLUMNS => array('label' => 'row1', 'col1' => 1)),
                                       array(Row::COLUMNS => array('label' => 'row2', 'col1' => 2))
                                  ));

        return $result;
    }

    private function createInstanceWithDataTables()
    {
        $dataTable = new DataTable\Map();

        $subDataTable1 = $this->createTestDataTable();
        $dataTable->addTable($subDataTable1, 'subDataTable1');

        $subDataTable2 = $this->createTestDataTable();
        $dataTable->addTable($subDataTable2, 'subDataTable2');

        return $dataTable;
    }

    private function createInstanceWithDataTableArrays()
    {
        $dataTable = new DataTable\Map();

        $subDataTableArray1 = $this->createInstanceWithDataTables();
        $dataTable->addTable($subDataTableArray1, 'subArray1');

        $subDataTableArray2 = $this->createInstanceWithDataTables();
        $dataTable->addTable($subDataTableArray2, 'subArray2');

        return $dataTable;
    }

    /**
     * Tests that Set::mergeChildren works when the DataTable_Array contains DataTables.
     * @group Core
     * @group DataTable
     * @group DataTable_Array
     */
    public function test_MergeChildrenDataTable()
    {
        $dataTable = $this->createInstanceWithDataTables();

        $result = $dataTable->mergeChildren();

        // check that the result is a DataTable w/ 4 rows
        $this->assertInstanceOf('DataTable', $result);
        $this->assertEquals(4, $result->getRowsCount());

        // check that the first two rows have 'subDataTable1' as the label
        $this->mergeChildren_checkRow($result->getRowFromId(0), 'subDataTable1', 1);
        $this->mergeChildren_checkRow($result->getRowFromId(1), 'subDataTable1', 2);

        // check that the last two rows have 'subDataTable2' as the label
        $this->mergeChildren_checkRow($result->getRowFromId(2), 'subDataTable2', 1);
        $this->mergeChildren_checkRow($result->getRowFromId(3), 'subDataTable2', 2);
    }

    private function mergeChildren_checkRow($row, $expectedLabel, $expectedColumnValue)
    {
        $this->assertEquals($expectedLabel, $row->getColumn('label'));
        $this->assertEquals($expectedColumnValue, $row->getColumn('col1'));
    }

    /**
     * Tests that Set::mergeChildren works when the DataTable_Array contains DataTable_Arrays.
     * @group Core
     * @group DataTable
     * @group DataTable_Array
     */
    public function testMergeChildrenDataTableArray()
    {
        $dataTable = $this->createInstanceWithDataTableArrays();

        $result = $dataTable->mergeChildren();

        // check that the result is a DataTable_Array w/ two DataTable children
        $this->assertInstanceOf('Set', $result);
        $this->assertEquals(2, $result->getRowsCount());

        // check that the first sub-DataTable is a DataTable with 4 rows
        $subDataTable1 = $result->getTable('subDataTable1');
        $this->assertTrue($subDataTable1 instanceof DataTable);
        $this->assertEquals(4, $subDataTable1->getRowsCount());

        // check that the first two rows of the first sub-table have 'subArray1' as the label
        $this->mergeChildren_checkRow($subDataTable1->getRowFromId(0), 'subArray1', 1);
        $this->mergeChildren_checkRow($subDataTable1->getRowFromId(1), 'subArray1', 2);

        // check that the last two rows of the first sub-table have 'subArray2' as the label
        $this->mergeChildren_checkRow($subDataTable1->getRowFromId(2), 'subArray2', 1);
        $this->mergeChildren_checkRow($subDataTable1->getRowFromId(3), 'subArray2', 2);

        // check that the second sub-DataTable is a DataTable with 4 rows
        $subDataTable2 = $result->getTable('subDataTable2');
        $this->assertTrue($subDataTable2 instanceof DataTable);
        $this->assertEquals(4, $subDataTable2->getRowsCount());

        // check that the first two rows of the second sub-table have 'subArray1' as the label
        $this->mergeChildren_checkRow($subDataTable2->getRowFromId(0), 'subArray1', 1);
        $this->mergeChildren_checkRow($subDataTable2->getRowFromId(1), 'subArray1', 2);

        // check that the last two rows of the second sub-table have 'subArray2' as the label
        $this->mergeChildren_checkRow($subDataTable2->getRowFromId(2), 'subArray2', 1);
        $this->mergeChildren_checkRow($subDataTable2->getRowFromId(3), 'subArray2', 2);
    }
}