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

github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Steur <thomas.steur@googlemail.com>2014-05-13 09:50:38 +0400
committerThomas Steur <thomas.steur@googlemail.com>2014-05-13 09:50:38 +0400
commit112748511588723652879b06fd0355fe3680689e (patch)
tree63ef62318a96de83dd93aa4d18ef7aaad6140a19
parent56aedb8f50898a772b0b8d08be91aa69bbc011e5 (diff)
refs #4706 added some tests for Row class
-rw-r--r--tests/PHPUnit/Core/DataTable/RowTest.php153
1 files changed, 130 insertions, 23 deletions
diff --git a/tests/PHPUnit/Core/DataTable/RowTest.php b/tests/PHPUnit/Core/DataTable/RowTest.php
index b021e1bfe4..af32f3e9b0 100644
--- a/tests/PHPUnit/Core/DataTable/RowTest.php
+++ b/tests/PHPUnit/Core/DataTable/RowTest.php
@@ -8,20 +8,27 @@
use Piwik\DataTable;
use Piwik\DataTable\Row;
+/**
+ * @group Core
+ */
class RowTest extends PHPUnit_Framework_TestCase
{
/**
- * @group Core
+ * @var Row
*/
+ private $row;
+
+ public function setUp()
+ {
+ $this->row = new Row();
+ }
+
public function testDataTableAssociatedIsNegativeWhenSubDataTableInMemory()
{
$testRow = $this->getTestRowWithSubDataTableLoaded();
$this->assertTrue($testRow->c[Row::DATATABLE_ASSOCIATED] < 0);
}
- /**
- * @group Core
- */
public function testDataTableAssociatedIsNegativeWhenSubDataTableAdded()
{
$testRow = $this->getTestRowWithSubDataTableNotLoaded();
@@ -29,9 +36,6 @@ class RowTest extends PHPUnit_Framework_TestCase
$this->assertTrue($testRow->c[Row::DATATABLE_ASSOCIATED] < 0);
}
- /**
- * @group Core
- */
public function testDataTableAssociatedIsNegativeWhenSubDataTableSetted()
{
$testRow = $this->getTestRowWithSubDataTableNotLoaded();
@@ -39,19 +43,12 @@ class RowTest extends PHPUnit_Framework_TestCase
$this->assertTrue($testRow->c[Row::DATATABLE_ASSOCIATED] < 0);
}
- /**
- * @group Core
- */
public function testIdSubDataTabledIsPositiveWhenSubDataTableInMemory()
{
$testRow = $this->getTestRowWithSubDataTableLoaded();
$this->assertTrue($testRow->getIdSubDataTable() > 0);
}
-
- /**
- * @group Core
- */
public function testDataTableAssociatedIsPositiveOnSerializedRow()
{
$testRow = $this->getTestRowWithSubDataTableLoaded();
@@ -65,9 +62,6 @@ class RowTest extends PHPUnit_Framework_TestCase
$this->assertTrue($unserializedTestRow->c[Row::DATATABLE_ASSOCIATED] > 0);
}
- /**
- * @group Core
- */
public function testDataTableAssociatedIsNegativeAfterSerialize()
{
$testRow = $this->getTestRowWithSubDataTableLoaded();
@@ -79,24 +73,137 @@ class RowTest extends PHPUnit_Framework_TestCase
$this->assertTrue($testRow->c[Row::DATATABLE_ASSOCIATED] < 0);
}
- /**
- * @group Core
- */
public function testIsSubDataTableLoadedIsTrueWhenSubDataTableInMemory()
{
$testRow = $this->getTestRowWithSubDataTableLoaded();
$this->assertTrue($testRow->isSubtableLoaded());
}
- /**
- * @group Core
- */
public function testIsSubDataTableLoadedIsFalseWhenSubDataTableNotInMemory()
{
$testRow = $this->getTestRowWithSubDataTableNotLoaded();
$this->assertFalse($testRow->isSubtableLoaded());
}
+ 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_shouldResolveACallable()
+ {
+ $this->assertColumnSavesValue(6, 'testClosure', function () {
+ return 6;
+ });
+
+ $this->assertColumnSavesValue(7, 'testCallable', array($this, 'callbackReturnScalar'));
+ }
+
+ public function test_getColumn_shouldPassRowToCallable()
+ {
+ $callbackRow = null;
+
+ $this->row->addColumn('testClosure', function (Row $row) use (&$callbackRow) {
+ $callbackRow = $row;
+ return $row;
+ });
+
+ $returnedRow = $this->row->getColumn('testClosure');
+ $this->assertNotEmpty($callbackRow);
+ $this->assertSame($returnedRow, $callbackRow);
+ }
+
+ public function test_getColumn_shouldReturnFalseIfValueIsNull()
+ {
+ $this->assertColumnSavesValue(false, 'testScalar', null);
+ $this->assertColumnSavesValue(false, 'testClosure', function () {
+ return 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'));
+ }
+
+ public function test_getColumns_shouldReturnAllColumns()
+ {
+ $this->row->setColumns(array(
+ 'nb_visits' => 4,
+ 'label' => 'Test',
+ 'closure' => function () { return 5; },
+ 'callable' => array($this, 'callbackReturnScalar'),
+ 'goals' => array(1 => array())
+ ));
+
+ $expected = array(
+ 'nb_visits' => 4,
+ 'label' => 'Test',
+ 'closure' => 5,
+ 'callable' => 7,
+ 'goals' => array(1 => array())
+ );
+
+ $this->assertEquals($expected, $this->row->getColumns());
+ }
+
+ public function test_getColumns_shouldNotConvertNullValuesToFalse()
+ {
+ $this->row->setColumns(array(
+ 'nb_visits' => null,
+ 'label' => 'Test',
+ 'closure' => function () { return null; },
+ 'boolean' => false
+ ));
+
+ $expected = array(
+ 'nb_visits' => null,
+ 'label' => 'Test',
+ 'closure' => null,
+ 'boolean' => false
+ );
+
+ $this->assertSame($expected, $this->row->getColumns());
+ }
+
+ public function callbackReturnScalar(Row $row)
+ {
+ return 7;
+ }
+
+ public function test_SumRow_shouldIgnoreCallableValues_AndNotRaiseAnyException()
+ {
+ $columns = array(
+ 'nb_visits' => 5,
+ 'label' => 'Test',
+ 'closure' => function () { return 7; },
+ );
+
+ $this->row->setColumns($columns);
+
+ $secondRow = new Row(array(Row::COLUMNS => $columns));
+
+ $this->row->sumRow($secondRow);
+
+ $this->assertEquals(10, $this->row->getColumn('nb_visits'));
+ $this->assertEquals(7, $this->row->getColumn('closure'));
+ }
+
+ private function assertColumnSavesValue($expectedValue, $columnName, $valueToSet)
+ {
+ $this->row->setColumn($columnName, $valueToSet);
+ $this->assertSame($expectedValue, $this->row->getColumn($columnName));
+ }
+
protected function getTestRowWithSubDataTableLoaded()
{
$testSubDataTable = $this->getTestSubDataTable();