Commit 3c121809 authored by Tino Goratsch's avatar Tino Goratsch

covered the PictureColumnRenderer with unit tests

parent acbe6fc5
<?php
namespace ACP3\Core\Helpers\DataGrid\ColumnRenderer;
use ACP3\Core\Router;
/**
......@@ -28,30 +29,31 @@ class PictureColumnRenderer extends AbstractColumnRenderer
*/
public function fetchDataAndRenderColumn(array $column, array $dbResultRow)
{
if (isset($column['custom']['pattern'])) {
$dbValue = $this->getValue($column, $dbResultRow);
$value = '<img src="' . $this->getUrl($column['custom'], $dbValue) . '" alt="">';
} else {
$value = '';
$value = $this->getValue($column, $dbResultRow);
if (isset($column['custom']['pattern']) &&
$value !== null && $value !== $this->getDefaultValue($column)
) {
$value = '<img src="' . $this->getUrl($column['custom'], $value) . '" alt="">';
}
return $this->render($column, $value);
}
/**
* @param array $custom
* @param array $data
* @param string $value
*
* @return string
*/
protected function getUrl(array $custom, $value)
protected function getUrl(array $data, $value)
{
$value = sprintf($custom['pattern'], $value);
if (isset($custom['isRoute'])) {
return $this->router->route($value);
$url = sprintf($data['pattern'], $value);
if (isset($data['isRoute'])) {
return $this->router->route($url);
}
return $value;
return $url;
}
/**
......
<?php
class PictureColumnRendererTest extends AbstractColumnRendererTest
{
/**
* @var \ACP3\Core\Router|PHPUnit_Framework_MockObject_MockObject
*/
protected $routerMock;
protected function setUp()
{
$this->routerMock = $this->getMockBuilder(\ACP3\Core\Router::class)
->disableOriginalConstructor()
->setMethods(['route'])
->getMock();
$this->columnRenderer = new \ACP3\Core\Helpers\DataGrid\ColumnRenderer\PictureColumnRenderer(
$this->routerMock
);
parent::setUp();
}
public function testValidField()
{
$this->columnData = array_merge($this->columnData, [
'fields' => ['picture'],
'custom' => [
'pattern' => 'gallery/index/pic/id_%s',
'isRoute' => true
]
]);
$this->dbData = [
'picture' => 1
];
$this->routerMock->expects($this->once())
->method('route')
->with('gallery/index/pic/id_1')
->willReturn('/gallery/index/pic/id_1/');
$expected = '<td><img src="/gallery/index/pic/id_1/" alt=""></td>';
$this->compareResults($expected);
}
public function testValidFieldWithNoInternalRoute()
{
$this->columnData = array_merge($this->columnData, [
'fields' => ['picture'],
'custom' => [
'pattern' => 'gallery/index/pic/id_%s'
]
]);
$this->dbData = [
'picture' => 1
];
$this->routerMock->expects($this->never())
->method('route');
$expected = '<td><img src="gallery/index/pic/id_1" alt=""></td>';
$this->compareResults($expected);
}
}
\ 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