« README.md » n'existait pas sur « cf80aa4010f2585fce7d668b85e1314bef24d24f »
Newer
Older
* 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;
use Piwik\DataTable;
use Piwik\DataTable\Row;
class RowTest extends \PHPUnit_Framework_TestCase
private $row;
public function setUp()
{
$this->row = new Row();
}
Thomas Steur
a validé
public function test_isSubtableLoaded_ReturnsTrue_IfDataTableAssociatedIsLoaded()
{
$testRow = $this->getTestRowWithSubDataTableLoaded();
Thomas Steur
a validé
$this->assertTrue($testRow->isSubtableLoaded());
$this->assertGreaterThanOrEqual(1, $testRow->getIdSubDataTable());
Thomas Steur
a validé
public function test_isSubtableLoaded_ReturnsTrue_WhenSubDataTableSetted()
{
$testRow = $this->getTestRowWithSubDataTableNotLoaded();
Thomas Steur
a validé
$this->assertFalse($testRow->isSubtableLoaded()); // verify not already loaded
$this->assertEquals(50, $testRow->getIdSubDataTable());
$testRow->setSubtable($this->getTestSubDataTable());
Thomas Steur
a validé
$this->assertTrue($testRow->isSubtableLoaded());
$this->assertGreaterThanOrEqual(1, $testRow->getIdSubDataTable());
}
public function test_getIdSubDataTable_ShouldBeNullIfNoSubtableIsSet()
{
$testRow = $this->getTestRowWithNoSubDataTable();
$this->assertEquals(null, $testRow->getIdSubDataTable());
Thomas Steur
a validé
public function test_removeSubtable_ShouldRemoveASetSubtable()
{
$testRow = $this->getTestRowWithSubDataTableLoaded();
Thomas Steur
a validé
$this->assertTrue($testRow->isSubtableLoaded());
$testRow->removeSubtable();
$this->assertFalse($testRow->isSubtableLoaded());
$this->assertEquals(null, $testRow->getIdSubDataTable());
public function test_destruct_ShouldRemoveASetSubtable()
{
$testRow = $this->getTestRowWithSubDataTableLoaded();
Thomas Steur
a validé
$this->assertTrue($testRow->isSubtableLoaded());
Thomas Steur
a validé
$testRow->__destruct();
Thomas Steur
a validé
$this->assertFalse($testRow->isSubtableLoaded());
$this->assertEquals(null, $testRow->getIdSubDataTable());
}
public function test_canBeCloned_ShouldRemoveASetSubtable()
{
$testRow = $this->getTestRowWithNoSubDataTable();
$testRow->setColumn('label', 'test');
Thomas Steur
a validé
$testRow2 = clone $testRow;
$this->assertNotSame($testRow2, $testRow);
$this->assertEquals('test', $testRow2->getColumn('label'));
$this->assertEquals('test', $testRow->getColumn('label'));
$testRow->setColumn('label', 'different');
// only row 2 changes
$this->assertEquals('test', $testRow2->getColumn('label'));
$this->assertEquals('different', $testRow->getColumn('label'));
Thomas Steur
a validé
public function test_export_shouldExportColumnsMetadataAndSubtableId()
Thomas Steur
a validé
$columns = array('label' => 'test', 'nb_visits' => 5);
$testRow = $this->getTestRowWithSubDataTableLoaded();
Thomas Steur
a validé
$testRow->setColumns($columns);
$testRow->setMetadata('test1', 'val1');
$testRow->setMetadata('url', 'http://piwik.org');
$export = $testRow->export();
Thomas Steur
a validé
$expected = array(
Row::COLUMNS => $columns,
Row::METADATA => array('test1' => 'val1', 'url' => 'http://piwik.org')
);
Thomas Steur
a validé
// we cannot really test for exact match since the subtableId might change when other tests are changed
$this->assertGreaterThan(1, $export[Row::DATATABLE_ASSOCIATED]);
unset($export[Row::DATATABLE_ASSOCIATED]);
Thomas Steur
a validé
$this->assertSame($expected, $export);
}
public function test_isSubtableLoaded_ShouldReturnFalse_WhenRestoringAnExportedRow()
{
$testRow = $this->getTestRowWithSubDataTableLoaded();
// serialize and unserialize is not needed for this test case, the export is the important part.
// we still do it, to have it more "realistic"
$serializedTestRow = serialize($testRow->export());
$unserializedTestRow = unserialize($serializedTestRow);
/** @var Row $unserializedTestRow */
$row = new Row($unserializedTestRow);
$this->assertTrue($row->getIdSubDataTable() > 0);
$this->assertFalse($row->isSubtableLoaded());
}
public function testIsSubDataTableLoadedIsTrueWhenSubDataTableInMemory()
{
$testRow = $this->getTestRowWithSubDataTableLoaded();
$this->assertTrue($testRow->isSubtableLoaded());
}
public function testIsSubDataTableLoadedIsFalseWhenSubDataTableNotInMemory()
{
$testRow = $this->getTestRowWithSubDataTableNotLoaded();
$this->assertFalse($testRow->isSubtableLoaded());
}
Thomas Steur
a validé
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
public function test_getMetadata_setMetadata_shouldReturnRawScalarValue()
{
$this->assertMetadataSavesValue(5, 'testInteger', 5);
$this->assertMetadataSavesValue(5.444, 'testFloat', 5.444);
$this->assertMetadataSavesValue('MyString', 'testString', 'MyString');
$this->assertMetadataSavesValue(array(array(1 => '5')), 'testArray', array(array(1 => '5')));
}
public function test_getMetadata_shouldReturnFalse_IfMetadataKeyDoesNotExists()
{
$this->assertFalse($this->row->getMetadata('anyKey_thatDoesNotExist'));
}
public function test_getMetadata_shouldReturnEmptyArray_IfNoParticularOneIsRequestedAndNoneAreSet()
{
$this->assertEquals(array(), $this->row->getMetadata());
}
public function test_getMetadata_shouldReturnAllMetadataValues_IfNoParticularOneIsRequested()
{
$this->row->setMetadata('url', 'http://piwik.org');
$this->row->setMetadata('segmentValue', 'test==piwik');
$this->assertEquals(array(
'url' => 'http://piwik.org',
'segmentValue' => 'test==piwik'
), $this->row->getMetadata());
}
public function test_deleteMetadata_shouldReturnDeleteAllValues_WhenNoSpecificOneIsRequestedToBeDeleted()
{
$this->row->setMetadata('url', 'http://piwik.org');
$this->row->setMetadata('segmentValue', 'test==piwik');
$this->assertNotEmpty($this->row->getMetadata()); // make sure it is actually set
$this->row->deleteMetadata();
$this->assertSame(array(), $this->row->getMetadata());
}
public function test_deleteMetadata_shouldOnlyDeleteARequestedMetadataEntry_WhileKeepingOthersUntouched()
{
$this->row->setMetadata('url', 'http://piwik.org');
$this->row->setMetadata('segmentValue', 'test==piwik');
$this->assertTrue($this->row->deleteMetadata('url'));
$this->assertFalse($this->row->getMetadata('url'));
$this->assertEquals('test==piwik', $this->row->getMetadata('segmentValue'));
}
public function test_deleteMetadata_shouldReturnFalseAndKeepOtherEntriesUntouched_IfMetadataNameDidNotExist()
{
$this->row->setMetadata('segmentValue', 'test==piwik');
$this->assertFalse($this->row->deleteMetadata('url'));
$this->assertEquals('test==piwik', $this->row->getMetadata('segmentValue'));
}
public function test_getColumn_shouldReturnRawScalarValue()
{
$this->assertColumnSavesValue(5, 'testInteger', 5);
$this->assertColumnSavesValue(5.444, 'testFloat', 5.444);
$this->assertColumnSavesValue('MyString', 'testString', 'MyString');
$this->assertColumnSavesValue(array(array(1 => '5')), 'testArray', array(array(1 => '5')));
}
public function test_getColumn_shouldReturnFalseIfValueIsNull()
{
$this->assertColumnSavesValue(false, 'testScalar', null);
}
public function test_getColumns_shouldNotCallAnyCallableForSecurity()
{
$this->assertColumnSavesValue('print_r', 'testScalar', 'print_r');
$this->assertColumnSavesValue(array('print_r'), 'testScalar', array('print_r'));
$this->assertColumnSavesValue(array(null, 'print_r'), 'testScalar', array(null, 'print_r'));
$this->assertColumnSavesValue('phpinfo', 'testScalar', 'phpinfo');
$this->assertColumnSavesValue(array('phpinfo'), 'testScalar', array('phpinfo'));
$this->assertColumnSavesValue(array(null, 'phpinfo'), 'testScalar', array(null, 'phpinfo'));
}
Thomas Steur
a validé
public function test_getColumns_setColumns_shouldReturnAllColumns()
{
$this->row->setColumns(array(
'nb_visits' => 4,
'label' => 'Test',
'goals' => array(1 => array())
));
$expected = array(
'nb_visits' => 4,
'label' => 'Test',
'goals' => array(1 => array())
);
$this->assertEquals($expected, $this->row->getColumns());
Thomas Steur
a validé
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
$this->assertEquals('Test', $this->row->getColumn('label'));
$this->assertEquals(4, $this->row->getColumn('nb_visits'));
}
public function test_deleteColumn_shouldOnlyDeleteARequestedColumnEntry_WhileKeepingOthersUntouched()
{
$this->row->setColumn('label', 'http://piwik.org');
$this->row->setColumn('nb_visits', '1');
$this->assertTrue($this->row->deleteColumn('nb_visits'));
$this->assertFalse($this->row->hasColumn('nb_visits')); // verify
$this->assertFalse($this->row->getMetadata('nb_visits')); // verify
$this->assertEquals('http://piwik.org', $this->row->getColumn('label')); // make sure not deleted
}
public function test_deleteColumn_shouldReturnFalseAndKeepOtherEntriesUntouched_IfColumnNameDidNotExist()
{
$this->row->setColumn('label', 'http://piwik.org');
$this->assertFalse($this->row->deleteColumn('nb_visits'));
$this->assertFalse($this->row->hasColumn('nb_visits'));
$this->assertEquals('http://piwik.org', $this->row->getColumn('label'));
}
public function test_deleteColumn_shouldReturnAColumnValueThatIsNull()
{
$this->row->setColumn('label', null);
$this->assertTrue($this->row->hasColumn('label'));
$this->assertTrue($this->row->deleteColumn('label'));
$this->assertFalse($this->row->hasColumn('label'));
}
public function test_renameColumn_shouldReturnAColumnOnly_IfAValueIsSetForThatColumn()
{
$this->row->setColumn('nb_visits', 10);
$this->row->renameColumn('nb_visits', 'nb_hits');
$this->assertFalse($this->row->hasColumn('nb_visits'));
$this->assertTrue($this->row->hasColumn('nb_hits'));
$this->assertEquals(10, $this->row->getColumn('nb_hits'));
}
public function test_renameColumn_shouldNotReturnAColumn_IfValueIsNotSetButRemoveColumn()
{
$this->row->setColumn('nb_visits', null);
$this->row->renameColumn('nb_visits', 'nb_hits');
$this->assertFalse($this->row->hasColumn('nb_visits'));
$this->assertFalse($this->row->hasColumn('nb_hits'));
}
public function test_renameColumn_shouldDoNothing_IfGivenColumnDoesNotExist()
{
$this->row->setColumn('nb_visits', 11);
$this->row->renameColumn('nb_hits', 'nb_pageviews');
$this->assertFalse($this->row->hasColumn('nb_hits'));
$this->assertFalse($this->row->hasColumn('nb_pageviews'));
$this->assertEquals(11, $this->row->getColumn('nb_visits'));
}
public function test_getSubtable_shouldReturnSubtable_IfLoaded()
{
$testRow = $this->getTestRowWithSubDataTableNotLoaded();
$subTable = $this->getTestSubDataTable();
$testRow->setSubtable($subTable);
$this->assertSame($subTable, $testRow->getSubtable());
}
public function test_getSubtable_shouldReturnFalse_IfSubtableExistsButIsNotLoaded()
{
$testRow = $this->getTestRowWithSubDataTableNotLoaded();
$this->assertFalse($testRow->getSubtable());
}
public function test_getSubtable_shouldReturnFalse_IfHasNoSubtableAtAll()
{
$testRow = $this->getTestRowWithNoSubDataTable();
$this->assertFalse($testRow->getSubtable());
public function test_sumSubTable_whenSubTableAlreadyExists_overwriteExistingSubtable()
{
$testRow = $this->getTestRowWithSubDataTableNotLoaded();
Thomas Steur
a validé
$this->assertFalse($testRow->isSubtableLoaded());
$subTable = $this->getTestSubDataTable();
$testRow->setSubtable($subTable);
Thomas Steur
a validé
$this->assertTrue($testRow->isSubtableLoaded());
$testRow->sumSubtable($subTable);
Thomas Steur
a validé
$this->assertTrue(DataTable::isEqual($testRow->getSubtable(), $subTable));
}
public function test_hasColumn()
{
$this->row->setColumns(array('test1' => 'yes', 'test2' => false, 'test3' => 5, 'test4' => array()));
$this->assertFalse($this->row->hasColumn('test')); // does not exist
$this->assertTrue($this->row->hasColumn('test1'));
$this->assertTrue($this->row->hasColumn('test2')); // even if value is false it still exists
$this->assertTrue($this->row->hasColumn('test3'));
$this->assertTrue($this->row->hasColumn('test4'));
}
public function test_hasColumn_shouldReturnTrueEvenIfColumnValueIsNull()
{
$this->assertFalse($this->row->hasColumn('test'));
$this->row->setColumn('test', null);
$this->assertTrue($this->row->hasColumn('test'));
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
public function test_sumRowMetadata_shouldSumMetadataAccordingToAggregationOperations()
{
$this->row->setColumn('nb_visits', 10);
$this->row->setMetadata('my_sum', 5);
$this->row->setMetadata('my_max', 4);
$this->row->setMetadata('my_array', array(array('test' => 1, 'value' => 1), array('test' => 2, 'value' => 2)));
$row = $this->getTestRowWithNoSubDataTable();
$row->setColumn('nb_visits', 15);
$row->setMetadata('my_sum', 7);
$row->setMetadata('my_max', 2);
$row->setMetadata('my_array', array(array('test' => 3, 'value' => 3), array('test' => 2, 'value' => 2)));
$aggregations = array(
'nosuchcolumn' => 'max', // this metadata name does not exist and should be ignored
'my_sum' => 'sum',
'my_max' => 'max',
'my_array' => 'uniquearraymerge'
);
$this->row->sumRowMetadata($row, $aggregations);
$metadata = $this->row->getMetadata();
$expected = array(
'my_sum' => 12,
'my_max' => 4,
'my_array' => array(array('test' => 1, 'value' => 1), array('test' => 2, 'value' => 2), array('test' => 3, 'value' => 3))
);
$this->assertSame($expected, $metadata);
}
public function test_sumRowMetadata_uniquearraymergeShouldUseArrayFromOtherRow_IfNoMetadataForThisRowSpecified()
{
$row = $this->getTestRowWithNoSubDataTable();
$arrayValue = array(array('test' => 3, 'value' => 3), array('test' => 2, 'value' => 2));
$row->setMetadata('my_array', $arrayValue);
$aggregations = array('my_array' => 'uniquearraymerge');
$this->row->sumRowMetadata($row, $aggregations);
$this->assertSame(array('my_array' => $arrayValue), $this->row->getMetadata());
}
public function test_sumRowMetadata_uniquearraymergeShouldUseArrayFromThisRow_IfNoMetadataForOtherRowSpecified()
{
$row = $this->getTestRowWithNoSubDataTable();
$arrayValue = array(array('test' => 3, 'value' => 3), array('test' => 2, 'value' => 2));
$this->row->setMetadata('my_array', $arrayValue);
$aggregations = array('my_array' => 'uniquearraymerge');
$this->row->sumRowMetadata($row, $aggregations);
$this->assertSame(array('my_array' => $arrayValue), $this->row->getMetadata());
}
private function assertColumnSavesValue($expectedValue, $columnName, $valueToSet)
{
$this->row->setColumn($columnName, $valueToSet);
$this->assertSame($expectedValue, $this->row->getColumn($columnName));
}
Thomas Steur
a validé
private function assertMetadataSavesValue($expectedValue, $metadataName, $valueToSet)
{
$this->row->setMetadata($metadataName, $valueToSet);
$this->assertSame($expectedValue, $this->row->getMetadata($metadataName));
}
protected function getTestRowWithSubDataTableLoaded()
{
$testSubDataTable = $this->getTestSubDataTable();
Thomas Steur
a validé
$testRow = new Row(array(
Row::DATATABLE_ASSOCIATED => $testSubDataTable
));
return $testRow;
}
Thomas Steur
a validé
protected function getTestRowWithNoSubDataTable()
{
return new Row(array());
}
protected function getTestSubDataTable()
{
return new DataTable();
}
protected function getTestRowWithSubDataTableNotLoaded()
{
Thomas Steur
a validé
$testRow = new Row(array(
Row::DATATABLE_ASSOCIATED => 50
));
return $testRow;
}