Commit bda2f85c authored by Tino Goratsch's avatar Tino Goratsch

covered the DataProcessor and the ColumnTypeStrategyFactory with unit tests

parent f6a767fe
......@@ -26,16 +26,16 @@ class DataProcessor
}
/**
* @param array $formData
* @param array $allowedColumns
* @param array $columnData
* @param array $columnConstraints
* @return array
*/
public function processColumnData(array $formData, array $allowedColumns)
public function processColumnData(array $columnData, array $columnConstraints)
{
$data = [];
foreach ($formData as $column => $value) {
if (array_key_exists($column, $allowedColumns)) {
$data[$column] = $this->factory->getStrategy($allowedColumns[$column])->doEscape($value);
foreach ($columnData as $column => $value) {
if (array_key_exists($column, $columnConstraints)) {
$data[$column] = $this->factory->getStrategy($columnConstraints[$column])->doEscape($value);
}
}
......
......@@ -15,7 +15,7 @@ class ColumnTypeStrategyFactory
/**
* @var ContainerInterface
*/
private $container;
protected $container;
/**
* ColumnTypeStrategyFactory constructor.
......
<?php
/**
* Copyright (c) 2016 by the ACP3 Developers.
* See the LICENCE file at the top-level module directory for licencing details.
*/
namespace ACP3\Core\Test\Model;
use ACP3\Core\Model\DataProcessor\ColumnType\ColumnTypeStrategyInterface;
trait CreateRawColumnTypeMockTrait
{
public function getRawColumnTypeInstance(\PHPUnit_Framework_TestCase $testCase)
{
return $testCase->getMockBuilder(ColumnTypeStrategyInterface::class)
->setMethods(['doEscape'])
->getMock();
}
}
<?php
/**
* Copyright (c) 2016 by the ACP3 Developers.
* See the LICENCE file at the top-level module directory for licencing details.
*/
namespace ACP3\Core\Test\Model\DataProcessor;
use ACP3\Core\Model\DataProcessor\ColumnType\ColumnTypeStrategyInterface;
use ACP3\Core\Model\DataProcessor\ColumnTypes;
use ACP3\Core\Model\DataProcessor\ColumnTypeStrategyFactory;
use ACP3\Core\Test\Model\CreateRawColumnTypeMockTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ColumnTypeStrategyFactoryTest extends \PHPUnit_Framework_TestCase
{
use CreateRawColumnTypeMockTrait;
/**
* @var ColumnTypeStrategyFactory
*/
private $columnTypeStrategyFactory;
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $containerMock;
protected function setUp()
{
$this->containerMock = $this->getMockBuilder(ContainerInterface::class)
->setMethods(['set', 'get', 'has', 'initialized', 'setParameter', 'getParameter', 'hasParameter'])
->getMock();
$this->columnTypeStrategyFactory = new ColumnTypeStrategyFactory($this->containerMock);
}
public function testGetStrategyWithValidColumnType()
{
$this->setUpContainerMockExpectations();
$this->assertInstanceOf(ColumnTypeStrategyInterface::class, $this->columnTypeStrategyFactory->getStrategy(ColumnTypes::COLUMN_TYPE_RAW));
}
private function setUpContainerMockExpectations()
{
$serviceId = 'core.model.column_type.' . ColumnTypes::COLUMN_TYPE_RAW . '_column_type_strategy';
$this->containerMock
->expects($this->once())
->method('get')
->with($serviceId)
->willReturn($this->getRawColumnTypeInstance($this));
}
}
<?php
/**
* Copyright (c) 2016 by the ACP3 Developers.
* See the LICENCE file at the top-level module directory for licencing details.
*/
namespace ACP3\Core\Test\Model;
use ACP3\Core\Model\DataProcessor;
class DataProcessorTest extends \PHPUnit_Framework_TestCase
{
use CreateRawColumnTypeMockTrait;
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $columnTypeStrategyFactoryMock;
/**
* @var DataProcessor
*/
private $dataProcessor;
protected function setUp()
{
$this->columnTypeStrategyFactoryMock = $this->getMockBuilder(DataProcessor\ColumnTypeStrategyFactory::class)
->disableOriginalConstructor()
->setMethods(['getStrategy'])
->getMock();
$this->dataProcessor = new DataProcessor($this->columnTypeStrategyFactoryMock);
}
public function testProcessColumnData()
{
$columnData = [
'foo' => 'Lorem',
'bar' => 'Ipsum',
'baz' => 'Dolor'
];
$columnConstraints = [
'foo' => DataProcessor\ColumnTypes::COLUMN_TYPE_RAW,
'bar' => DataProcessor\ColumnTypes::COLUMN_TYPE_RAW
];
$this->setUpColumnTypeStrategyFactoryExpectations();
$expected = [
'foo' => 'Lorem',
'bar' => 'Ipsum',
];
$this->assertEquals($expected, $this->dataProcessor->processColumnData($columnData, $columnConstraints));
}
private function setUpColumnTypeStrategyFactoryExpectations()
{
$columnTypeMock = $this->getRawColumnTypeInstance($this);
$columnTypeMock
->expects($this->exactly(2))
->method('doEscape')
->with($this->logicalOr('Lorem', 'Ipsum'))
->willReturnOnConsecutiveCalls('Lorem', 'Ipsum');
$this->columnTypeStrategyFactoryMock
->expects($this->exactly(2))
->method('getStrategy')
->with(DataProcessor\ColumnTypes::COLUMN_TYPE_RAW)
->willReturn($columnTypeMock);
}
}
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