Commit 31aa0738 authored by Tino Goratsch's avatar Tino Goratsch

- added unit tests for the ReplaceValueColumnRenderer

parent 57384a79
......@@ -85,7 +85,7 @@ abstract class AbstractColumnRenderer implements ColumnRendererInterface
* @param array $dbResultRow
* @param string $field
*
* @return null
* @return string|null
*/
protected function getDbValueIfExists(array $dbResultRow, $field)
{
......
......@@ -7,18 +7,32 @@ namespace ACP3\Core\Helpers\DataGrid\ColumnRenderer;
*/
class ReplaceValueColumnRenderer extends AbstractColumnRenderer
{
/**
* @var array
*/
protected $search = [];
/**
* @var array
*/
protected $replace = [];
/**
* @inheritdoc
*/
public function fetchDataAndRenderColumn(array $column, array $dbResultRow, $identifier, $primaryKey)
{
$value = $this->getValue($column, $dbResultRow);
$this->search = $column['custom']['search'];
$this->replace = $column['custom']['replace'];
$search = $column['custom']['search'];
$replace = $column['custom']['replace'];
return $this->render($column, $this->getValue($column, $dbResultRow));
}
return $this->render($column, str_replace($search, $replace, $value));
/**
* @inheritdoc
*/
protected function getDbValueIfExists(array $dbResultRow, $field)
{
return isset($dbResultRow[$field]) ? str_replace($this->search, $this->replace, $dbResultRow[$field]) : null;
}
/**
......
......@@ -6,10 +6,18 @@ abstract class AbstractColumnRendererTest extends PHPUnit_Framework_TestCase
* @var \ACP3\Core\Helpers\DataGrid\ColumnRenderer\AbstractColumnRenderer
*/
protected $columnRenderer;
/**
* @var array
*/
protected $columnData = [];
/**
* @var array
*/
protected $dbData = [];
protected function setUp()
{
$this->columnData = $this->getColumnDefaults();
}
/**
......@@ -34,19 +42,19 @@ abstract class AbstractColumnRendererTest extends PHPUnit_Framework_TestCase
public function testSingleCustomHtmlAttribute()
{
$column = array_merge($this->getColumnDefaults(), [
$this->columnData = array_merge($this->columnData, [
'attribute' => [
'data-foo' => 'bar'
]
]);
$expected = '<td data-foo="bar"></td>';
$this->compareResults($expected, $column);
$this->compareResults($expected, $this->columnData);
}
public function testMultipleCustomHtmlAttributes()
{
$column = array_merge($this->getColumnDefaults(), [
$this->columnData = array_merge($this->columnData, [
'attribute' => [
'data-foo' => 'bar',
'data-lorem' => 'ipsum',
......@@ -54,85 +62,85 @@ abstract class AbstractColumnRendererTest extends PHPUnit_Framework_TestCase
]);
$expected = '<td data-foo="bar" data-lorem="ipsum"></td>';
$this->compareResults($expected, $column);
$this->compareResults($expected, $this->columnData);
}
public function testAddStyle()
{
$column = array_merge($this->getColumnDefaults(), [
$this->columnData = array_merge($this->columnData, [
'style' => 'width:50%'
]);
$expected = '<td style="width:50%"></td>';
$this->compareResults($expected, $column);
$this->compareResults($expected, $this->columnData);
}
public function testAddCssClass()
{
$column = array_merge($this->getColumnDefaults(), [
$this->columnData = array_merge($this->columnData, [
'class' => 'foobar'
]);
$expected = '<td class="foobar"></td>';
$this->compareResults($expected, $column);
$this->compareResults($expected, $this->columnData);
}
public function testInvalidField()
{
$column = array_merge($this->getColumnDefaults(), [
$this->columnData = array_merge($this->columnData, [
'fields' => ['test']
]);
$data = [
$this->dbData = [
'text' => 'Lorem Ipsum'
];
$expected = '<td></td>';
$this->compareResults($expected, $column, $data);
$this->compareResults($expected, $this->columnData, $this->dbData);
}
public function testValidField()
{
$column = array_merge($this->getColumnDefaults(), [
$this->columnData = array_merge($this->columnData, [
'fields' => ['text']
]);
$data = [
$this->dbData = [
'text' => 'Lorem Ipsum'
];
$expected = '<td>Lorem Ipsum</td>';
$this->compareResults($expected, $column, $data);
$this->compareResults($expected, $this->columnData, $this->dbData);
}
public function testDefaultValueIfNull()
{
$column = array_merge($this->getColumnDefaults(), [
$this->columnData = array_merge($this->columnData, [
'fields' => ['text'],
'custom' => [
'default_value' => 'Foo Bar'
]
]);
$data = [
$this->dbData = [
'text' => null
];
$expected = '<td>Foo Bar</td>';
$this->compareResults($expected, $column, $data);
$this->compareResults($expected, $this->columnData, $this->dbData);
}
public function testDefaultValueIfNotFound()
{
$column = array_merge($this->getColumnDefaults(), [
$this->columnData = array_merge($this->columnData, [
'fields' => ['test'],
'custom' => [
'default_value' => 'Foo Bar'
]
]);
$data = [
$this->dbData = [
'text' => 'Lorem Ipsum'
];
$expected = '<td>Foo Bar</td>';
$this->compareResults($expected, $column, $data);
$this->compareResults($expected, $this->columnData, $this->dbData);
}
/**
......
......@@ -5,31 +5,33 @@ class IntegerColumnRendererTest extends AbstractColumnRendererTest
protected function setUp()
{
$this->columnRenderer = new \ACP3\Core\Helpers\DataGrid\ColumnRenderer\IntegerColumnRenderer();
parent::setUp();
}
public function testValidField()
{
$column = array_merge($this->getColumnDefaults(), [
$this->columnData = array_merge($this->columnData, [
'fields' => ['text']
]);
$data = [
$this->dbData = [
'text' => '1'
];
$expected = '<td>1</td>';
$this->compareResults($expected, $column, $data);
$this->compareResults($expected, $this->columnData, $this->dbData);
}
public function testValidFieldStringToIntegerConversion()
{
$column = array_merge($this->getColumnDefaults(), [
$this->columnData = array_merge($this->columnData, [
'fields' => ['text']
]);
$data = [
$this->dbData = [
'text' => 'Test'
];
$expected = '<td>0</td>';
$this->compareResults($expected, $column, $data);
$this->compareResults($expected, $this->columnData, $this->dbData);
}
}
\ No newline at end of file
<?php
class ReplaceValueColumnRendererTest extends AbstractColumnRendererTest
{
protected function setUp()
{
$this->columnRenderer = new \ACP3\Core\Helpers\DataGrid\ColumnRenderer\ReplaceValueColumnRenderer();
$this->columnData = array_merge(
$this->getColumnDefaults(),
[
'custom' => [
'search' => [],
'replace' => []
]
]
);
}
public function testDefaultValueIfNull()
{
$this->columnData = array_merge($this->columnData, [
'fields' => ['text'],
'custom' => [
'default_value' => 'Foo Bar',
'search' => [],
'replace' => []
]
]);
$this->dbData = [
'text' => null
];
$expected = '<td>Foo Bar</td>';
$this->compareResults($expected, $this->columnData, $this->dbData);
}
public function testDefaultValueIfNotFound()
{
$this->columnData = array_merge($this->columnData, [
'fields' => ['test'],
'custom' => [
'default_value' => 'Foo Bar',
'search' => [],
'replace' => []
]
]);
$this->dbData = [
'text' => 'Lorem Ipsum'
];
$expected = '<td>Foo Bar</td>';
$this->compareResults($expected, $this->columnData, $this->dbData);
}
}
\ No newline at end of file
......@@ -5,5 +5,7 @@ class TextColumnRendererTest extends AbstractColumnRendererTest
protected function setUp()
{
$this->columnRenderer = new \ACP3\Core\Helpers\DataGrid\ColumnRenderer\TextColumnRenderer();
parent::setUp();
}
}
\ No newline at end of file
......@@ -7,7 +7,6 @@ class TranslateColumnRendererTest extends AbstractColumnRendererTest
*/
protected $langMock;
protected function setUp()
{
$this->langMock = $this->getMockBuilder(\ACP3\Core\Lang::class)
......@@ -16,6 +15,8 @@ class TranslateColumnRendererTest extends AbstractColumnRendererTest
->getMock();
$this->columnRenderer = new \ACP3\Core\Helpers\DataGrid\ColumnRenderer\TranslateColumnRenderer($this->langMock);
parent::setUp();
}
/**
......@@ -24,7 +25,6 @@ class TranslateColumnRendererTest extends AbstractColumnRendererTest
*/
private function setUpLangMockExpectation($langKey, $langValue)
{
$this->langMock->expects($this->once())
->method('t')
->with($langKey, $langKey)
......@@ -36,7 +36,7 @@ class TranslateColumnRendererTest extends AbstractColumnRendererTest
{
$this->setUpLangMockExpectation('news', '{NEWS_NEWS}');
$column = array_merge($this->getColumnDefaults(), [
$this->columnData = array_merge($this->columnData, [
'fields' => ['text']
]);
$data = [
......@@ -44,6 +44,6 @@ class TranslateColumnRendererTest extends AbstractColumnRendererTest
];
$expected = '<td>{NEWS_NEWS}</td>';
$this->compareResults($expected, $column, $data);
$this->compareResults($expected, $this->columnData, $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