Commit 08e2d56c authored by Tino Goratsch's avatar Tino Goratsch

- added unit tests for the IntegerColumnRenderer + fixed behavior

- added some more basic test cases to the AbstractColumnRendererTest class
parent 5329760b
......@@ -8,14 +8,24 @@ namespace ACP3\Core\Helpers\DataGrid\ColumnRenderer;
class IntegerColumnRenderer extends AbstractColumnRenderer
{
/**
* @inheritdoc
* @param array $column
* @param array $dbResultRow
*
* @return string
*/
public function fetchDataAndRenderColumn(array $column, array $dbResultRow, $identifier, $primaryKey)
protected function getDbFieldValueIfExists(array $column, array $dbResultRow)
{
return parent::render(
$column,
(int)$this->getDbFieldValueIfExists($column, $dbResultRow)
);
$field = $this->getFirstDbField($column);
if (isset($dbResultRow[$field])) {
return (int)$dbResultRow[$field];
}
if (isset($column['custom']['default_value'])) {
return $column['custom']['default_value'];
}
return '';
}
/**
......
......@@ -41,9 +41,7 @@ abstract class AbstractColumnRendererTest extends PHPUnit_Framework_TestCase
]);
$expected = '<td data-foo="bar"></td>';
$actual = $this->columnRenderer->fetchDataAndRenderColumn($column, [], '', '');
$this->assertEquals($expected, $actual);
$this->compareResults($expected, $column);
}
public function testMultipleCustomHtmlAttributes()
......@@ -56,9 +54,7 @@ abstract class AbstractColumnRendererTest extends PHPUnit_Framework_TestCase
]);
$expected = '<td data-foo="bar" data-lorem="ipsum"></td>';
$actual = $this->columnRenderer->fetchDataAndRenderColumn($column, [], '', '');
$this->assertEquals($expected, $actual);
$this->compareResults($expected, $column);
}
public function testAddStyle()
......@@ -68,9 +64,7 @@ abstract class AbstractColumnRendererTest extends PHPUnit_Framework_TestCase
]);
$expected = '<td style="width:50%"></td>';
$actual = $this->columnRenderer->fetchDataAndRenderColumn($column, [], '', '');
$this->assertEquals($expected, $actual);
$this->compareResults($expected, $column);
}
public function testAddCssClass()
......@@ -80,7 +74,75 @@ abstract class AbstractColumnRendererTest extends PHPUnit_Framework_TestCase
]);
$expected = '<td class="foobar"></td>';
$actual = $this->columnRenderer->fetchDataAndRenderColumn($column, [], '', '');
$this->compareResults($expected, $column);
}
public function testInvalidField()
{
$column = array_merge($this->getColumnDefaults(), [
'fields' => ['test']
]);
$data = [
'text' => 'Lorem Ipsum'
];
$expected = '<td></td>';
$this->compareResults($expected, $column, $data);
}
public function testValidField()
{
$column = array_merge($this->getColumnDefaults(), [
'fields' => ['text']
]);
$data = [
'text' => 'Lorem Ipsum'
];
$expected = '<td>Lorem Ipsum</td>';
$this->compareResults($expected, $column, $data);
}
public function testDefaultValueIfNull()
{
$column = array_merge($this->getColumnDefaults(), [
'fields' => ['text'],
'custom' => [
'default_value' => 'Foo Bar'
]
]);
$data = [
'text' => null
];
$expected = '<td>Foo Bar</td>';
$this->compareResults($expected, $column, $data);
}
public function testDefaultValueIfNotFound()
{
$column = array_merge($this->getColumnDefaults(), [
'fields' => ['test'],
'custom' => [
'default_value' => 'Foo Bar'
]
]);
$data = [
'text' => 'Lorem Ipsum'
];
$expected = '<td>Foo Bar</td>';
$this->compareResults($expected, $column, $data);
}
/**
* @param $expected
* @param array $column
* @param array $data
*/
protected function compareResults($expected, array $column, array $data = [])
{
$actual = $this->columnRenderer->fetchDataAndRenderColumn($column, $data, '', '');
$this->assertEquals($expected, $actual);
}
......
<?php
class IntegerColumnRendererTest extends AbstractColumnRendererTest
{
protected function setUp()
{
$this->columnRenderer = new \ACP3\Core\Helpers\DataGrid\ColumnRenderer\IntegerColumnRenderer();
}
public function testValidField()
{
$column = array_merge($this->getColumnDefaults(), [
'fields' => ['text']
]);
$data = [
'text' => '1'
];
$expected = '<td>1</td>';
$this->compareResults($expected, $column, $data);
}
public function testValidFieldStringToIntegerConversion()
{
$column = array_merge($this->getColumnDefaults(), [
'fields' => ['text']
]);
$data = [
'text' => 'Test'
];
$expected = '<td>0</td>';
$this->compareResults($expected, $column, $data);
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment