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
|
<?php
use Piwik\Config;
use Piwik\DataTable\Manager;
use Piwik\DataTable;
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 createInstanceWithDataTableMaps()
{
$dataTable = new DataTable\Map();
$subDataTableMap1 = $this->createInstanceWithDataTables();
$dataTable->addTable($subDataTableMap1, 'subArray1');
$subDataTableMap2 = $this->createInstanceWithDataTables();
$dataTable->addTable($subDataTableMap2, 'subArray2');
return $dataTable;
}
/**
* Tests that Set::mergeChildren works when the DataTable\Map contains DataTables.
* @group Core
*/
public function test_MergeChildrenDataTable()
{
$dataTable = $this->createInstanceWithDataTables();
$result = $dataTable->mergeChildren();
// check that the result is a DataTable w/ 4 rows
$this->assertInstanceOf('\Piwik\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\Map contains DataTable\Maps.
* @group Core
*/
public function testMergeChildrenDataTableMap()
{
$dataTable = $this->createInstanceWithDataTableMaps();
$result = $dataTable->mergeChildren();
// check that the result is a DataTable\Map w/ two DataTable children
$this->assertInstanceOf('\Piwik\DataTable\Map', $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);
}
}
|