Skip to content

Commit

Permalink
Make sure simple datatable metadata is serialized + some test case ch…
Browse files Browse the repository at this point in the history
…anges (#13296)

* Make DataTable metadata protected so it will be serialized in derived classes.

* Allow SystemTestCase::runAnyApiTest to test any format and use a testSuffix.

* Add test for metadata serialization & note about it in field docs.
  • Loading branch information
diosmosis committed Sep 6, 2018
1 parent c9d4cfc commit f74db0e
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 3 deletions.
4 changes: 3 additions & 1 deletion core/DataTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,9 +306,11 @@ class DataTable implements DataTableInterface, \IteratorAggregate, \ArrayAccess
*
* Any data that describes the data held in the table's rows should go here.
*
* Note: this field is protected so derived classes will serialize it.
*
* @var array
*/
private $metadata = array();
protected $metadata = array();

/**
* Maximum number of rows allowed in this datatable (including the summary row).
Expand Down
10 changes: 8 additions & 2 deletions tests/PHPUnit/Framework/TestCase/SystemTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -297,12 +297,18 @@ protected static function getApiForTestingScheduledReports($dateTime, $period)
protected function runAnyApiTest($apiMethod, $apiId, $requestParams, $options = array())
{
$requestParams['module'] = 'API';
$requestParams['format'] = 'XML';
if (empty($requestParams['format'])) {
$requestParams['format'] = 'XML';
}
$requestParams['method'] = $apiMethod;

$apiId = $apiMethod . '_' . $apiId . '.xml';
$apiId = $apiMethod . '_' . $apiId . '.' . strtolower($requestParams['format']);
$testName = 'test_' . static::getOutputPrefix();

if (!empty($options['testSuffix'])) {
$testName .= '_' . $options['testSuffix'];
}

list($processedFilePath, $expectedFilePath) =
$this->getProcessedAndExpectedPaths($testName, $apiId, $format = null, $compareAgainst = false);

Expand Down
52 changes: 52 additions & 0 deletions tests/PHPUnit/Unit/DataTable/SimpleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?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;


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

class SimpleTest extends \PHPUnit_Framework_TestCase
{
public function test_serialize_includesAllRequiredData()
{
$dataTable = new Simple();
$dataTable->addRowFromSimpleArray([
'column1' => 'value1',
'column2' => 'value2',
]);
$dataTable->addSummaryRow(new Row([
Row::COLUMNS => ['column1' => 'total1', 'column2' => 'total2']
]));
$dataTable->setAllTableMetadata([
'metadataKey1' => 10,
'metadataKey2' => ['a', 'b', 'c'],
]);

$serialized = serialize($dataTable);

/** @var Simple $unserialized */
$unserialized = unserialize($serialized);

$this->assertEquals(1, $unserialized->getRowsCountWithoutSummaryRow());
$this->assertEquals([
'column1' => 'value1',
'column2' => 'value2',
], $unserialized->getRows()[0]->getColumns());

$this->assertEquals(2, $unserialized->getRowsCount());
$this->assertEquals(['column1' => 'total1', 'column2' => 'total2'], $unserialized->getRows()[DataTable::ID_SUMMARY_ROW]->getColumns());

$this->assertEquals([
'metadataKey1' => 10,
'metadataKey2' => ['a', 'b', 'c'],
], $unserialized->getAllTableMetadata());
}
}

0 comments on commit f74db0e

Please sign in to comment.