From b1946c4aaeb42f260406341fb55b7654f69d1456 Mon Sep 17 00:00:00 2001 From: Daniel Seifert Date: Fri, 26 Nov 2021 23:16:32 +0100 Subject: [PATCH] improve tests for EE --- .../Controller/Admin/d3ActionWizard.php | 11 +- .../Controller/Admin/d3ExportWizard.php | 11 +- Application/Model/ActionBase.php | 15 +- Application/Model/ExportRenderer/Csv.php | 13 +- tests/.gitignore | 1 + tests/tools/d3TestAction.php | 2 +- .../Controller/Admin/d3ActionWizardTest.php | 54 +++- .../Admin/d3AdminControllerTest.php | 27 +- .../Controller/Admin/d3ExportWizardTest.php | 58 ++-- .../unit/Application/Model/ActionBaseTest.php | 250 ++++++++++++++++++ .../Model/ExportRenderer/CsvTest.php | 69 +++++ .../Model/ExportRenderer/JsonTest.php | 20 +- 12 files changed, 484 insertions(+), 47 deletions(-) create mode 100644 tests/unit/Application/Model/ActionBaseTest.php diff --git a/Application/Controller/Admin/d3ActionWizard.php b/Application/Controller/Admin/d3ActionWizard.php index 3c4e5a4..c15ec84 100644 --- a/Application/Controller/Admin/d3ActionWizard.php +++ b/Application/Controller/Admin/d3ActionWizard.php @@ -23,6 +23,7 @@ use D3\ModCfg\Application\Model\Exception\d3_cfg_mod_exception; use D3\ModCfg\Application\Model\Exception\d3ShopCompatibilityAdapterException; use Doctrine\DBAL\DBALException; use OxidEsales\Eshop\Application\Controller\Admin\AdminDetailsController; +use OxidEsales\Eshop\Core\Config; use OxidEsales\Eshop\Core\Exception\DatabaseConnectionException; use OxidEsales\Eshop\Core\Exception\DatabaseErrorException; use OxidEsales\Eshop\Core\Exception\StandardException; @@ -79,7 +80,7 @@ class d3ActionWizard extends AdminDetailsController [ $queryString, $parameters ] = $action->getQuery(); - if (Registry::getConfig()->getConfigParam('d3datawizard_debug')) { + if ($this->d3GetConfig()->getConfigParam('d3datawizard_debug')) { throw oxNew( DebugException::class, d3database::getInstance()->getPreparedStatementQuery($queryString, $parameters) @@ -89,6 +90,14 @@ class d3ActionWizard extends AdminDetailsController $action->run(); } + /** + * @return Config + */ + public function d3GetConfig() + { + return Registry::getConfig(); + } + public function getUserMessages() { return null; diff --git a/Application/Controller/Admin/d3ExportWizard.php b/Application/Controller/Admin/d3ExportWizard.php index d38e678..049a24e 100644 --- a/Application/Controller/Admin/d3ExportWizard.php +++ b/Application/Controller/Admin/d3ExportWizard.php @@ -25,6 +25,7 @@ use D3\ModCfg\Application\Model\Exception\d3_cfg_mod_exception; use D3\ModCfg\Application\Model\Exception\d3ShopCompatibilityAdapterException; use Doctrine\DBAL\DBALException; use OxidEsales\Eshop\Application\Controller\Admin\AdminDetailsController; +use OxidEsales\Eshop\Core\Config; use OxidEsales\Eshop\Core\Exception\DatabaseConnectionException; use OxidEsales\Eshop\Core\Exception\DatabaseErrorException; use OxidEsales\Eshop\Core\Exception\StandardException; @@ -87,7 +88,7 @@ class d3ExportWizard extends AdminDetailsController [ $queryString, $parameters ] = $export->getQuery(); - if (Registry::getConfig()->getConfigParam('d3datawizard_debug')) { + if ($this->d3GetConfig()->getConfigParam('d3datawizard_debug')) { throw oxNew( DebugException::class, d3database::getInstance()->getPreparedStatementQuery($queryString, $parameters) @@ -97,6 +98,14 @@ class d3ExportWizard extends AdminDetailsController $export->run(Registry::getRequest()->getRequestEscapedParameter('format')); } + /** + * @return Config + */ + public function d3GetConfig() + { + return Registry::getConfig(); + } + public function getUserMessages() { return null; diff --git a/Application/Model/ActionBase.php b/Application/Model/ActionBase.php index 1285f34..e0f87a6 100644 --- a/Application/Model/ActionBase.php +++ b/Application/Model/ActionBase.php @@ -103,15 +103,12 @@ abstract class ActionBase implements QueryBase */ public function registerFormElement(Input $input) { - switch (get_class($input)) { - case Radio::class: - case Checkbox::class: - $input->setTemplate('

{{ input }} {{ label }}

'); - $input->setAttribute('class', 'form-check-input'); - break; - default: - $input->setTemplate('

{{ label }} {{ input }}

'); - $input->setAttribute('class', 'form-control'); + if ($input instanceof Radio || $input instanceof Checkbox) { + $input->setTemplate('

{{ input }} {{ label }}

'); + $input->setAttribute('class', 'form-check-input'); + } else { + $input->setTemplate('

{{ label }} {{ input }}

'); + $input->setAttribute('class', 'form-control'); } $this->formElements[] = $input; } diff --git a/Application/Model/ExportRenderer/Csv.php b/Application/Model/ExportRenderer/Csv.php index 549f6d8..38f38b6 100644 --- a/Application/Model/ExportRenderer/Csv.php +++ b/Application/Model/ExportRenderer/Csv.php @@ -19,6 +19,7 @@ use D3\DataWizard\Application\Model\Exceptions\RenderException; use League\Csv\EncloseField; use League\Csv\Exception; use League\Csv\Writer; +use OxidEsales\Eshop\Core\Config; use OxidEsales\Eshop\Core\Registry; class Csv implements RendererInterface @@ -59,13 +60,13 @@ class Csv implements RendererInterface EncloseField::addTo($csv, "\t\x1f"); - $sEncloser = Registry::getConfig()->getConfigParam('sGiCsvFieldEncloser'); + $sEncloser = $this->d3GetConfig()->getConfigParam('sGiCsvFieldEncloser'); if (false == $sEncloser) { $sEncloser = '"'; } $csv->setEnclosure($sEncloser); - $sDelimiter = Registry::getConfig()->getConfigParam('sCSVSign'); + $sDelimiter = $this->d3GetConfig()->getConfigParam('sCSVSign'); if (false == $sDelimiter) { $sDelimiter = ';'; } @@ -81,4 +82,12 @@ class Csv implements RendererInterface { return 'D3_DATAWIZARD_EXPORT_FORMAT_CSV'; } + + /** + * @return Config + */ + public function d3GetConfig() + { + return Registry::getConfig(); + } } \ No newline at end of file diff --git a/tests/.gitignore b/tests/.gitignore index 165765a..89bffeb 100644 --- a/tests/.gitignore +++ b/tests/.gitignore @@ -1 +1,2 @@ +reports .phpunit.result.cache diff --git a/tests/tools/d3TestAction.php b/tests/tools/d3TestAction.php index 18fd56f..746624e 100644 --- a/tests/tools/d3TestAction.php +++ b/tests/tools/d3TestAction.php @@ -13,6 +13,6 @@ class d3TestAction extends ActionBase public function getQuery(): array { - return "SELECT 1"; + return ["UPDATE 1"]; } } \ No newline at end of file diff --git a/tests/unit/Application/Controller/Admin/d3ActionWizardTest.php b/tests/unit/Application/Controller/Admin/d3ActionWizardTest.php index 6532184..652cd26 100644 --- a/tests/unit/Application/Controller/Admin/d3ActionWizardTest.php +++ b/tests/unit/Application/Controller/Admin/d3ActionWizardTest.php @@ -18,6 +18,7 @@ namespace D3\DataWizard\tests\unit\Application\Controller\Admin; use D3\DataWizard\Application\Controller\Admin\d3ActionWizard; use D3\DataWizard\Application\Model\Configuration; use D3\DataWizard\Application\Model\Exceptions\DebugException; +use D3\DataWizard\Application\Model\ExportRenderer\RendererBridge; use D3\DataWizard\tests\tools\d3TestAction; use OxidEsales\Eshop\Core\Config; use OxidEsales\Eshop\Core\Registry; @@ -30,15 +31,17 @@ class d3ActionWizardTest extends d3AdminControllerTest /** @var d3ActionWizard */ protected $_oController; + protected $testClassName = d3ActionWizard::class; + public function setUp() : void { parent::setUp(); - $this->_oController = oxNew(d3ActionWizard::class); + $this->_oController = oxNew($this->testClassName); } /** - * @covers d3ActionWizard::getGroups() + * @covers \D3\DataWizard\Application\Controller\Admin\d3ActionWizard::getGroups() * @test * @throws ReflectionException */ @@ -63,7 +66,7 @@ class d3ActionWizardTest extends d3AdminControllerTest } /** - * @covers d3ActionWizard::getGroupTasks() + * @covers \D3\DataWizard\Application\Controller\Admin\d3ActionWizard::getGroupTasks() * @test * @throws ReflectionException * @dataProvider canGetGroupTasksDataProvider @@ -101,7 +104,7 @@ class d3ActionWizardTest extends d3AdminControllerTest } /** - * @covers d3ActionWizard::execute() + * @covers \D3\DataWizard\Application\Controller\Admin\d3ActionWizard::execute() * @test * @throws ReflectionException * @dataProvider executePassDataProvider @@ -112,9 +115,31 @@ class d3ActionWizardTest extends d3AdminControllerTest $requestMock = $this->getMockBuilder(get_class(Registry::getRequest())) ->onlyMethods(['getRequestEscapedParameter']) ->getMock(); - $requestMock->expects($this->atLeastOnce())->method('getRequestEscapedParameter')->with('taskid')->willReturn('testTaskId'); + $requestMock->expects($this->any())->method('getRequestEscapedParameter')->willReturnCallback([$this, 'executePassRequestCallback']); Registry::set(Request::class, $requestMock); + /** @var Config|MockObject $configMock */ + $configMock = $this->getMockBuilder(Config::class) + ->onlyMethods(['getConfigParam']) + ->getMock(); + $configMock->expects($this->atLeastOnce())->method('getConfigParam')->willReturnCallback( + function ($argName) use ($blDebug) { + switch ($argName) { + case 'd3datawizard_debug': + return $blDebug; + default: + return Registry::getConfig()->getConfigParam($argName); + } + } + ); + + /** @var d3ActionWizard|MockObject $controllerMock */ + $controllerMock = $this->getMockBuilder(d3ActionWizard::class) + ->onlyMethods(['d3GetConfig']) + ->getMock(); + $controllerMock->method('d3GetConfig')->willReturn($configMock); + $this->_oController = $controllerMock; + /** @var d3TestAction|MockObject $actionMock */ $actionMock = $this->getMockBuilder(d3TestAction::class) ->onlyMethods([ @@ -133,13 +158,6 @@ class d3ActionWizardTest extends d3AdminControllerTest $configurationMock->expects($this->atLeastOnce())->method('getActionById')->with('testTaskId')->willReturn($actionMock); $this->setValue($this->_oController, 'configuration', $configurationMock); - /** @var Config|MockObject $configMock */ - $configMock = $this->getMockBuilder(Config::class) - ->onlyMethods(['getConfigParam']) - ->getMock(); - $configMock->expects($this->atLeastOnce())->method('getConfigParam')->willReturn($blDebug); - Registry::set(Config::class, $configMock); - if ($blDebug) { $this->expectException(DebugException::class); } @@ -150,6 +168,18 @@ class d3ActionWizardTest extends d3AdminControllerTest ); } + public function executePassRequestCallback($varName) + { + switch ($varName) { + case 'taskid': + return 'testTaskId'; + case 'format': + return RendererBridge::FORMAT_CSV; + default: + return oxNew(Request::class)->getRequestEscapedParameter($varName); + } + } + /** * @return array */ diff --git a/tests/unit/Application/Controller/Admin/d3AdminControllerTest.php b/tests/unit/Application/Controller/Admin/d3AdminControllerTest.php index c8130a6..29aa527 100644 --- a/tests/unit/Application/Controller/Admin/d3AdminControllerTest.php +++ b/tests/unit/Application/Controller/Admin/d3AdminControllerTest.php @@ -37,6 +37,8 @@ abstract class d3AdminControllerTest extends d3ModCfgUnitTestCase /** @var d3ActionWizard|d3ExportWizard */ protected $_oController; + protected $testClassName; + public function tearDown() : void { parent::tearDown(); @@ -76,8 +78,8 @@ abstract class d3AdminControllerTest extends d3ModCfgUnitTestCase */ public function runTaskPass() { - /** @var d3ActionWizard|MockObject $controllerMock */ - $controllerMock = $this->getMockBuilder(d3ActionWizard::class) + /** @var d3ActionWizard|d3ExportWizard|MockObject $controllerMock */ + $controllerMock = $this->getMockBuilder($this->testClassName) ->onlyMethods(['execute']) ->getMock(); $controllerMock->expects($this->once())->method('execute')->willReturn(true); @@ -106,8 +108,8 @@ abstract class d3AdminControllerTest extends d3ModCfgUnitTestCase ->getMock(); $this->setValue($exceptionMock, 'message', 'exc_msg'); - /** @var d3ActionWizard|MockObject $controllerMock */ - $controllerMock = $this->getMockBuilder(d3ActionWizard::class) + /** @var d3ActionWizard|d3ExportWizard|MockObject $controllerMock */ + $controllerMock = $this->getMockBuilder($this->testClassName) ->onlyMethods(['execute']) ->getMock(); $controllerMock->expects($this->once())->method('execute')->willThrowException($exceptionMock); @@ -240,4 +242,21 @@ abstract class d3AdminControllerTest extends d3ModCfgUnitTestCase ) ); } + + /** + * @covers \D3\DataWizard\Application\Controller\Admin\d3ExportWizard::d3GetConfig + * @covers \D3\DataWizard\Application\Controller\Admin\d3ActionWizard::d3GetConfig + * @test + * @throws ReflectionException + */ + public function canGetConfig() + { + $this->assertInstanceOf( + Config::class, + $this->callMethod( + $this->_oController, + 'd3GetConfig' + ) + ); + } } \ No newline at end of file diff --git a/tests/unit/Application/Controller/Admin/d3ExportWizardTest.php b/tests/unit/Application/Controller/Admin/d3ExportWizardTest.php index 678ef39..907634a 100644 --- a/tests/unit/Application/Controller/Admin/d3ExportWizardTest.php +++ b/tests/unit/Application/Controller/Admin/d3ExportWizardTest.php @@ -19,6 +19,7 @@ use D3\DataWizard\Application\Controller\Admin\d3ActionWizard; use D3\DataWizard\Application\Controller\Admin\d3ExportWizard; use D3\DataWizard\Application\Model\Configuration; use D3\DataWizard\Application\Model\Exceptions\DebugException; +use D3\DataWizard\Application\Model\ExportRenderer\RendererBridge; use D3\DataWizard\tests\tools\d3TestAction; use D3\DataWizard\tests\tools\d3TestExport; use OxidEsales\Eshop\Core\Config; @@ -32,15 +33,17 @@ class d3ExportWizardTest extends d3AdminControllerTest /** @var d3ExportWizard */ protected $_oController; + protected $testClassName = d3ExportWizard::class; + public function setUp() : void { parent::setUp(); - $this->_oController = oxNew(d3ExportWizard::class); + $this->_oController = oxNew($this->testClassName); } /** - * @covers d3ActionWizard::getGroups() + * @covers \D3\DataWizard\Application\Controller\Admin\d3ExportWizard::getGroups() * @test * @throws ReflectionException */ @@ -65,7 +68,7 @@ class d3ExportWizardTest extends d3AdminControllerTest } /** - * @covers d3ActionWizard::getGroupTasks() + * @covers \D3\DataWizard\Application\Controller\Admin\d3ExportWizard::getGroupTasks() * @test * @throws ReflectionException * @dataProvider canGetGroupTasksDataProvider @@ -103,7 +106,7 @@ class d3ExportWizardTest extends d3AdminControllerTest } /** - * @covers d3ActionWizard::execute() + * @covers \D3\DataWizard\Application\Controller\Admin\d3ExportWizard::execute() * @test * @throws ReflectionException * @dataProvider executePassDataProvider @@ -114,12 +117,32 @@ class d3ExportWizardTest extends d3AdminControllerTest $requestMock = $this->getMockBuilder(get_class(Registry::getRequest())) ->onlyMethods(['getRequestEscapedParameter']) ->getMock(); - - $requestMock->expects($this->exactly($blDebug ? 1 : 2))->method('getRequestEscapedParameter')->withConsecutive( - ['taskid'], ['format'] - )->willReturnOnConsecutiveCalls('testTaskId', 'CSV'); + $requestMock->expects($this->any())->method('getRequestEscapedParameter')->willReturnCallback([$this, 'executePassRequestCallback']); + //OnConsecutiveCalls('testTaskId', 'CSV'); Registry::set(Request::class, $requestMock); + /** @var Config|MockObject $configMock */ + $configMock = $this->getMockBuilder(Config::class) + ->onlyMethods(['getConfigParam']) + ->getMock(); + $configMock->expects($this->atLeastOnce())->method('getConfigParam')->willReturnCallback( + function ($argName) use ($blDebug) { + switch ($argName) { + case 'd3datawizard_debug': + return $blDebug; + default: + return Registry::getConfig()->getConfigParam($argName); + } + } + ); + + /** @var d3ExportWizard|MockObject $controllerMock */ + $controllerMock = $this->getMockBuilder(d3ExportWizard::class) + ->onlyMethods(['d3GetConfig']) + ->getMock(); + $controllerMock->method('d3GetConfig')->willReturn($configMock); + $this->_oController = $controllerMock; + /** @var d3TestAction|MockObject $exportMock */ $exportMock = $this->getMockBuilder(d3TestExport::class) ->onlyMethods([ @@ -138,13 +161,6 @@ class d3ExportWizardTest extends d3AdminControllerTest $configurationMock->expects($this->atLeastOnce())->method('getExportById')->with('testTaskId')->willReturn($exportMock); $this->setValue($this->_oController, 'configuration', $configurationMock); - /** @var Config|MockObject $configMock */ - $configMock = $this->getMockBuilder(Config::class) - ->onlyMethods(['getConfigParam']) - ->getMock(); - $configMock->expects($this->atLeastOnce())->method('getConfigParam')->willReturn($blDebug); - Registry::set(Config::class, $configMock); - if ($blDebug) { $this->expectException(DebugException::class); } @@ -155,6 +171,18 @@ class d3ExportWizardTest extends d3AdminControllerTest ); } + public function executePassRequestCallback($varName) + { + switch ($varName) { + case 'taskid': + return 'testTaskId'; + case 'format': + return RendererBridge::FORMAT_CSV; + default: + return oxNew(Request::class)->getRequestEscapedParameter($varName); + } + } + /** * @return array */ diff --git a/tests/unit/Application/Model/ActionBaseTest.php b/tests/unit/Application/Model/ActionBaseTest.php new file mode 100644 index 0000000..5564405 --- /dev/null +++ b/tests/unit/Application/Model/ActionBaseTest.php @@ -0,0 +1,250 @@ + + * @link https://www.oxidmodule.com + */ + +declare(strict_types=1); + +namespace D3\DataWizard\tests\unit\Application\Model; + +use D3\DataWizard\tests\tools\d3TestAction; +use D3\ModCfg\Tests\unit\d3ModCfgUnitTestCase; +use FormManager\Inputs\Hidden; +use FormManager\Inputs\Number; +use FormManager\Inputs\Radio; +use OxidEsales\Eshop\Core\Exception\StandardException; +use PHPUnit\Framework\MockObject\MockObject; +use ReflectionException; + +class ActionBaseTest extends d3ModCfgUnitTestCase +{ + /** @var d3TestAction */ + protected $_oModel; + + public function setUp() : void + { + parent::setUp(); + + $this->_oModel = oxNew(d3TestAction::class); + } + + public function tearDown() : void + { + parent::tearDown(); + + unset($this->_oModel); + } + + /** + * @covers \D3\DataWizard\Application\Model\ActionBase::getDescription + * @test + * @throws ReflectionException + */ + public function canGetDescription() + { + $this->assertIsString( + $this->callMethod( + $this->_oModel, + 'getDescription' + ) + ); + } + + /** + * @covers \D3\DataWizard\Application\Model\ActionBase::getButtonText + * @test + * @throws ReflectionException + */ + public function canGetButtonText() + { + $this->assertIsString( + $this->callMethod( + $this->_oModel, + 'getButtonText' + ) + ); + } + + /** + * @covers \D3\DataWizard\Application\Model\ActionBase::hasFormElements + * @test + * @throws ReflectionException + * @dataProvider canGetHasFormElementsDataProvider + */ + public function canGetHasFormElements($formElements, $expected) + { + $this->setValue($this->_oModel, 'formElements', $formElements); + + $this->assertSame( + $expected, + $this->callMethod( + $this->_oModel, + 'hasFormElements' + ) + ); + } + + public function canGetHasFormElementsDataProvider() + { + return [ + 'hasFormElements' => [['abc', 'def'], true], + 'hasNoFormElements' => [[], false], + ]; + } + + /** + * @covers \D3\DataWizard\Application\Model\ActionBase::getFormElements + * @test + * @throws ReflectionException + * @dataProvider canGetHasFormElementsDataProvider + */ + public function canGetFormElements($formElements) + { + $this->setValue($this->_oModel, 'formElements', $formElements); + + $this->assertSame( + $formElements, + $this->callMethod( + $this->_oModel, + 'getFormElements' + ) + ); + } + + /** + * @covers \D3\DataWizard\Application\Model\ActionBase::registerFormElement + * @test + * @throws ReflectionException + * @dataProvider canRegisterFormElementDataProvider + */ + public function canRegisterFormElement($inputClass) + { + $oldCount = count($this->getValue($this->_oModel, 'formElements')); + + /** @var Radio|MockObject $inputMock */ + $inputMock = $this->getMockBuilder($inputClass) + ->onlyMethods([ + 'setTemplate', + 'setAttribute' + ]) + ->getMock(); + $inputMock->expects($this->atLeastOnce())->method('setTemplate'); + $inputMock->expects($this->atLeastOnce())->method('setAttribute'); + + $this->callMethod( + $this->_oModel, + 'registerFormElement', + [$inputMock] + ); + + $newCount = count($this->getValue($this->_oModel, 'formElements')); + + $this->assertGreaterThan($oldCount, $newCount); + } + + /** + * @return \string[][] + */ + public function canRegisterFormElementDataProvider(): array + { + return [ + 'Radio' => [Radio::class], + 'Checkbox' => [Radio::class], + 'Hidden' => [Hidden::class] + ]; + } + + /** + * @covers \D3\DataWizard\Application\Model\ActionBase::run + * @test + * @throws ReflectionException + */ + public function canRunWithoutFormElements() + { + $modelMock = $this->getMockBuilder(d3TestAction::class) + ->onlyMethods([ + 'hasFormElements', + 'executeAction', + 'getQuery' + ]) + ->getMock(); + $modelMock->expects($this->atLeastOnce())->method('hasFormElements')->willReturn(false); + $modelMock->expects($this->atLeastOnce())->method('executeAction')->willReturn(1); + $modelMock->expects($this->atLeastOnce())->method('getQuery')->willReturn([]); + $this->_oModel = $modelMock; + + $this->callMethod( + $this->_oModel, + 'run' + ); + } + + /** + * @covers \D3\DataWizard\Application\Model\ActionBase::run + * @test + * @throws ReflectionException + * @dataProvider canRunWithFormElementsDataProvider + */ + public function canRunWithFormElements($elements, $blThrowException) + { + $expectedException = oxNew(StandardException::class); + + $modelMock = $this->getMockBuilder(d3TestAction::class) + ->onlyMethods([ + 'hasFormElements', + 'executeAction', + 'getQuery', + 'getFormElements' + ]) + ->getMock(); + $modelMock->expects($this->atLeastOnce())->method('hasFormElements')->willReturn(true); + $modelMock->expects($this->exactly((int) !$blThrowException))->method('executeAction')->willReturn(1); + $modelMock->expects($this->exactly((int) !$blThrowException))->method('getQuery')->willReturn([]); + $modelMock->expects($this->atLeastOnce())->method('getFormElements')->willReturn($elements); + $this->_oModel = $modelMock; + + if ($blThrowException) { + $this->expectException(get_class($expectedException)); + } + + $this->callMethod( + $this->_oModel, + 'run' + ); + } + + /** + * @return array[] + */ + public function canRunWithFormElementsDataProvider(): array + { + /** @var Radio|MockObject $validMock */ + $validMock = $this->getMockBuilder(Radio::class) + ->onlyMethods(['isValid']) + ->getMock(); + $validMock->expects($this->atLeastOnce())->method('isValid')->willReturn(true); + + $invalidField = new Number(null, [ + 'required' => true, + 'min' => 1, + 'max' => 10, + 'step' => 5, + ]); + $invalidField + ->setValue(20) + ->setErrorMessages(['errorMsgs']); + + return [ + 'validElements' => [[$validMock, $validMock], false], + 'invalidElements' => [[$validMock, $invalidField], true] + ]; + } +} \ No newline at end of file diff --git a/tests/unit/Application/Model/ExportRenderer/CsvTest.php b/tests/unit/Application/Model/ExportRenderer/CsvTest.php index 51d9a97..2968a1b 100644 --- a/tests/unit/Application/Model/ExportRenderer/CsvTest.php +++ b/tests/unit/Application/Model/ExportRenderer/CsvTest.php @@ -19,6 +19,8 @@ use D3\DataWizard\Application\Model\Exceptions\RenderException; use D3\DataWizard\Application\Model\ExportRenderer\Csv; use League\Csv\Exception; use League\Csv\Writer; +use OxidEsales\Eshop\Core\Config; +use OxidEsales\Eshop\Core\Registry; use PHPUnit\Framework\MockObject\MockObject; class CsvTest extends ExportRendererTest @@ -93,6 +95,7 @@ class CsvTest extends ExportRendererTest 'no exception' => [false] ]; } + /** * @covers \D3\DataWizard\Application\Model\ExportRenderer\Csv::getCsv * @test @@ -108,4 +111,70 @@ class CsvTest extends ExportRendererTest ) ); } + + /** + * @covers \D3\DataWizard\Application\Model\ExportRenderer\Csv::getCsv + * @test + * @throws \ReflectionException + */ + public function canGetCsvNoSettings() + { + /** @var Config|MockObject $configMock */ + $configMock = $this->getMockBuilder(Config::class) + ->onlyMethods(['getConfigParam']) + ->getMock(); + $configMock->expects($this->atLeastOnce())->method('getConfigParam')->willReturnCallback( + function ($argName) { + switch ($argName) { + case 'sGiCsvFieldEncloser': + case 'sCSVSign': + return false; + default: + return Registry::getConfig()->getConfigParam($argName); + } + } + ); + + $modelMock = $this->getMockBuilder(Csv::class) + ->onlyMethods(['d3GetConfig']) + ->getMock(); + $modelMock->method('d3GetConfig')->willReturn($configMock); + $this->_oModel = $modelMock; + + $csv = $this->callMethod( + $this->_oModel, + 'getCsv' + ); + + $this->assertInstanceOf( + Writer::class, + $csv + ); + + $this->assertSame( + '"', + $csv->getEnclosure() + ); + + $this->assertSame( + ';', + $csv->getDelimiter() + ); + } + + /** + * @covers \D3\DataWizard\Application\Model\ExportRenderer\Csv::d3GetConfig + * @test + * @throws \ReflectionException + */ + public function canGetConfig() + { + $this->assertInstanceOf( + Config::class, + $this->callMethod( + $this->_oModel, + 'd3GetConfig' + ) + ); + } } \ No newline at end of file diff --git a/tests/unit/Application/Model/ExportRenderer/JsonTest.php b/tests/unit/Application/Model/ExportRenderer/JsonTest.php index a7e04c2..413aa80 100644 --- a/tests/unit/Application/Model/ExportRenderer/JsonTest.php +++ b/tests/unit/Application/Model/ExportRenderer/JsonTest.php @@ -15,6 +15,7 @@ declare(strict_types=1); namespace D3\DataWizard\tests\unit\Application\Model\ExportRenderer; +use D3\DataWizard\Application\Model\Exceptions\RenderException; use D3\DataWizard\Application\Model\ExportRenderer\Json; class JsonTest extends ExportRendererTest @@ -33,11 +34,15 @@ class JsonTest extends ExportRendererTest * @covers \D3\DataWizard\Application\Model\ExportRenderer\Json::getContent * @test * @throws \ReflectionException + * @dataProvider canGetContentDataProvider */ - public function canGetContent() + public function canGetContent($valueList, $expectException) { $fieldList = ['field1', 'field2']; - $valueList = ['value1', 'value2']; + + if ($expectException) { + $this->expectException(RenderException::class); + } $this->assertJson( $this->callMethod( @@ -47,4 +52,15 @@ class JsonTest extends ExportRendererTest ) ); } + + /** + * @return \string[][] + */ + public function canGetContentDataProvider(): array + { + return [ + 'valid' => [['value1', 'value2'], false], + 'invalid' => [["text" => "\xB1\x31"], true] // malformed UTF8 chars + ]; + } } \ No newline at end of file