Commit 14ee8acf authored by Tino Goratsch's avatar Tino Goratsch

made the DataGrid class a little bit more testable and added uni tests for it

parent 811052e8
......@@ -21,30 +21,26 @@ class DataGrid
* @var \ACP3\Core\Lang
*/
protected $lang;
/**
* @var \ACP3\Core\View
*/
protected $view;
/**
* @var array
*/
protected $results;
protected $results = [];
/**
* @var string
*/
protected $resourcePathEdit;
protected $resourcePathEdit = '';
/**
* @var string
*/
protected $resourcePathDelete;
protected $resourcePathDelete = '';
/**
* @var string
*/
protected $identifier;
protected $identifier = '';
/**
* @var int
*/
protected $recordsPerPage;
protected $recordsPerPage = 10;
/**
* @var bool
*/
......@@ -69,17 +65,14 @@ class DataGrid
/**
* @param \ACP3\Core\ACL $acl
* @param \ACP3\Core\Lang $lang
* @param \ACP3\Core\View $view
*/
public function __construct(
ACL $acl,
Lang $lang,
View $view
Lang $lang
)
{
$this->acl = $acl;
$this->lang = $lang;
$this->view = $view;
$this->columns = new ColumnPriorityQueue();
}
......@@ -209,7 +202,7 @@ class DataGrid
}
/**
* @return string
* @return array
*/
public function render()
{
......@@ -220,7 +213,7 @@ class DataGrid
$this->findPrimaryKey();
$dataTable = [
return [
'can_edit' => $canEdit,
'can_delete' => $canDelete,
'identifier' => substr($this->identifier, 1),
......@@ -228,9 +221,6 @@ class DataGrid
'config' => $this->generateDataTableConfig(),
'results' => $this->mapTableColumnsToDbFields()
];
$this->view->assign('dataTable', $dataTable);
return $this->view->fetchTemplate('system/datagrid.tpl');
}
/**
......@@ -260,7 +250,7 @@ class DataGrid
{
$results = '';
foreach ($this->results as $result) {
$results .= "<tr>\n";
$results .= '<tr>';
foreach (clone $this->columns as $column) {
if (array_key_exists($column['type'], $this->columnRenderer) && !empty($column['label'])) {
$results .= $this->columnRenderer[$column['type']]
......
services:
core.helpers.data_grid:
class: ACP3\Core\Helpers\DataGrid
arguments: [@core.acl, @core.lang, @core.view]
arguments: [@core.acl, @core.lang]
scope: prototype
core.helpers.data_grid.column_renderer.date_column_renderer:
......
......@@ -21,6 +21,6 @@
</div>
</nav>
{redirect_message}
{$grid}
{include file="asset:system/datagrid.tpl" dataTable=$grid}
</form>
{/block}
\ No newline at end of file
......@@ -22,6 +22,6 @@
</div>
</nav>
{redirect_message}
{$grid}
{include file="asset:system/datagrid.tpl" dataTable=$grid}
</form>
{/block}
\ No newline at end of file
......@@ -20,6 +20,6 @@
{/if}
</nav>
{redirect_message}
{$grid}
{include file="asset:system/datagrid.tpl" dataTable=$grid}
</form>
{/block}
\ No newline at end of file
......@@ -21,6 +21,6 @@
</div>
</nav>
{redirect_message}
{$grid}
{include file="asset:system/datagrid.tpl" dataTable=$grid}
</form>
{/block}
\ No newline at end of file
......@@ -22,6 +22,6 @@
</div>
</nav>
{redirect_message}
{$grid}
{include file="asset:system/datagrid.tpl" dataTable=$grid}
</form>
{/block}
\ No newline at end of file
......@@ -22,6 +22,6 @@
</div>
</nav>
{redirect_message}
{$grid}
{include file="asset:system/datagrid.tpl" dataTable=$grid}
</form>
{/block}
\ No newline at end of file
......@@ -21,7 +21,7 @@
</div>
</nav>
{redirect_message}
{$grid}
{include file="asset:system/datagrid.tpl" dataTable=$grid}
</form>
{/block}
......
......@@ -22,6 +22,6 @@
</div>
</nav>
{redirect_message}
{$grid}
{include file="asset:system/datagrid.tpl" dataTable=$grid}
</form>
{/block}
\ No newline at end of file
......@@ -21,6 +21,6 @@
</div>
</nav>
{redirect_message}
{$grid}
{include file="asset:system/datagrid.tpl" dataTable=$grid}
</form>
{/block}
\ No newline at end of file
......@@ -22,6 +22,6 @@
</div>
</nav>
{redirect_message}
{$grid}
{include file="asset:system/datagrid.tpl" dataTable=$grid}
</form>
{/block}
\ No newline at end of file
......@@ -20,6 +20,6 @@
{/if}
</nav>
{redirect_message}
{$grid}
{include file="asset:system/datagrid.tpl" dataTable=$grid}
</form>
{/block}
\ No newline at end of file
......@@ -23,7 +23,7 @@
</div>
</nav>
{redirect_message}
{$grid}
{include file="asset:system/datagrid.tpl" dataTable=$grid}
{javascripts}
{include_js module="system" file="forms"}
{/javascripts}
......
......@@ -21,6 +21,6 @@
</div>
</nav>
{redirect_message}
{$grid}
{include file="asset:system/datagrid.tpl" dataTable=$grid}
</form>
{/block}
\ No newline at end of file
......@@ -22,6 +22,6 @@
</div>
</nav>
{redirect_message}
{$grid}
{include file="asset:system/datagrid.tpl" dataTable=$grid}
</form>
{/block}
\ No newline at end of file
......@@ -22,6 +22,6 @@
</div>
</nav>
{redirect_message}
{$grid}
{include file="asset:system/datagrid.tpl" dataTable=$grid}
</form>
{/block}
\ No newline at end of file
......@@ -2,8 +2,159 @@
class DataGridTest extends PHPUnit_Framework_TestCase
{
public function testRender()
/**
* @var \ACP3\Core\Helpers\DataGrid|PHPUnit_Framework_MockObject_MockObject
*/
protected $dataGrid;
/**
* @var \ACP3\Core\ACL|PHPUnit_Framework_MockObject_MockObject
*/
protected $aclMock;
/**
* @var \ACP3\Core\Lang|PHPUnit_Framework_MockObject_MockObject
*/
protected $langMock;
protected function setUp()
{
$this->aclMock = $this->getMockBuilder(\ACP3\Core\ACL::class)
->disableOriginalConstructor()
->setMethods(['hasPermission'])
->getMock();
$this->langMock = $this->getMockBuilder(\ACP3\Core\Lang::class)
->disableOriginalConstructor()
->setMethods(['t'])
->getMock();
$this->dataGrid = new \ACP3\Core\Helpers\DataGrid(
$this->aclMock,
$this->langMock
);
parent::setUp();
}
/**
* @return array
*/
protected function getDefaultExpected()
{
return [
'can_edit' => false,
'can_delete' => false,
'identifier' => '',
'header' => '',
'config' => [
'element' => null,
'records_per_page' => 10,
'hide_col_sort' => "0",
'sort_col' => null,
'sort_dir' => null
],
'results' => ''
];
}
public function testRenderWithDefaults()
{
$this->aclMock
->expects($this->exactly(2))
->method('hasPermission')
->willReturn(false);
$expected = $this->getDefaultExpected();
$this->assertEquals($expected, $this->dataGrid->render());
}
public function testRenderWithOneTextColumn()
{
$this->markTestIncomplete('tba');
$this->aclMock
->expects($this->exactly(2))
->method('hasPermission')
->willReturn(false);
/** @var \ACP3\Core\Helpers\Formatter\MarkEntries|PHPUnit_Framework_MockObject_MockObject $markEntriesMock */
$markEntriesMock = $this->getMockBuilder(\ACP3\Core\Helpers\Formatter\MarkEntries::class)
->disableOriginalConstructor()
->getMock();
$this->dataGrid->registerColumnRenderer(new \ACP3\Core\Helpers\DataGrid\ColumnRenderer\HeaderColumnRenderer($markEntriesMock));
$this->dataGrid->registerColumnRenderer(new \ACP3\Core\Helpers\DataGrid\ColumnRenderer\TextColumnRenderer());
$this->dataGrid->addColumn([
'label' => 'Foo',
'fields' => ['title'],
'type' => 'text'
], 10);
$expected = array_merge(
$this->getDefaultExpected(),
[
'header' => '<th>Foo</th>',
'config' => [
'element' => null,
'records_per_page' => 10,
'hide_col_sort' => "1",
'sort_col' => null,
'sort_dir' => null
],
]
);
$this->assertEquals($expected, $this->dataGrid->render());
}
public function testRenderWithOneTextColumnAndData()
{
$data = [
[
'title' => 'Lorem Ipsum'
],
[
'title' => 'Lorem Ipsum Dolor'
]
];
$this->aclMock
->expects($this->exactly(2))
->method('hasPermission')
->willReturn(false);
/** @var \ACP3\Core\Helpers\Formatter\MarkEntries|PHPUnit_Framework_MockObject_MockObject $markEntriesMock */
$markEntriesMock = $this->getMockBuilder(\ACP3\Core\Helpers\Formatter\MarkEntries::class)
->disableOriginalConstructor()
->getMock();
$this->dataGrid->registerColumnRenderer(new \ACP3\Core\Helpers\DataGrid\ColumnRenderer\HeaderColumnRenderer($markEntriesMock));
$this->dataGrid->registerColumnRenderer(new \ACP3\Core\Helpers\DataGrid\ColumnRenderer\TextColumnRenderer());
$this->dataGrid->addColumn([
'label' => 'Foo',
'fields' => ['title'],
'type' => 'text'
], 10);
$this->dataGrid->setResults($data);
$expected = array_merge(
$this->getDefaultExpected(),
[
'header' => '<th>Foo</th>',
'config' => [
'element' => null,
'records_per_page' => 10,
'hide_col_sort' => "1",
'sort_col' => null,
'sort_dir' => null
],
'results' => <<<HTML
<tr><td>Lorem Ipsum</td></tr>
<tr><td>Lorem Ipsum Dolor</td></tr>
HTML
]
);
$this->assertEquals($expected, $this->dataGrid->render());
}
}
\ 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