use current module setting service

This commit is contained in:
Daniel Seifert 2024-05-13 15:23:47 +02:00
parent 4cfcfb6d44
commit f730a380ec
5 changed files with 93 additions and 76 deletions

View File

@ -17,6 +17,7 @@ namespace D3\DataWizard\Application\Model;
use D3\DataWizard\Application\Model\Exceptions\InputUnvalidException; use D3\DataWizard\Application\Model\Exceptions\InputUnvalidException;
use D3\DataWizard\Application\Model\Exceptions\TaskException; use D3\DataWizard\Application\Model\Exceptions\TaskException;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Exception as DBALException; use Doctrine\DBAL\Exception as DBALException;
use FormManager\Inputs\Checkbox; use FormManager\Inputs\Checkbox;
use FormManager\Inputs\Input; use FormManager\Inputs\Input;
@ -88,8 +89,7 @@ abstract class ActionBase implements QueryBase
throw $exception; throw $exception;
} }
$connection = ContainerFactory::getInstance()->getContainer()->get(ConnectionProviderInterface::class)->get(); $affected = (int) $this->getConnection()->executeStatement($queryString, $parameters);
$affected = (int) $connection->executeStatement($queryString, $parameters);
/** @var TaskException $exception */ /** @var TaskException $exception */
$exception = oxNew( $exception = oxNew(
@ -105,6 +105,11 @@ abstract class ActionBase implements QueryBase
throw $exception; throw $exception;
} }
protected function getConnection(): Connection
{
return ContainerFactory::getInstance()->getContainer()->get(ConnectionProviderInterface::class)->get();
}
/** /**
* @return string * @return string
*/ */

View File

@ -17,16 +17,19 @@ namespace D3\DataWizard\tests\unit\Application\Controller\Admin;
use D3\DataWizard\Application\Controller\Admin\d3ActionWizard; use D3\DataWizard\Application\Controller\Admin\d3ActionWizard;
use D3\DataWizard\Application\Model\Configuration; use D3\DataWizard\Application\Model\Configuration;
use D3\DataWizard\Application\Model\Constants;
use D3\DataWizard\Application\Model\Exceptions\DataWizardException;
use D3\DataWizard\Application\Model\Exceptions\DebugException; use D3\DataWizard\Application\Model\Exceptions\DebugException;
use D3\DataWizard\Application\Model\ExportRenderer\RendererBridge; use D3\DataWizard\Application\Model\ExportRenderer\RendererBridge;
use D3\DataWizard\tests\tools\d3TestAction; use D3\DataWizard\tests\tools\d3TestAction;
use OxidEsales\Eshop\Core\Config; use Doctrine\DBAL\Exception as DBALException;
use OxidEsales\Eshop\Core\Registry; use OxidEsales\Eshop\Core\Registry;
use OxidEsales\Eshop\Core\Request; use OxidEsales\Eshop\Core\Request;
use OxidEsales\EshopCommunity\Internal\Framework\Module\Facade\ModuleSettingService;
use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\MockObject\MockObject;
use ReflectionException; use ReflectionException;
class d3ActionWizardTest extends d3AdminControllerTest class d3ActionWizardTest extends d3AdminController
{ {
/** @var d3ActionWizard */ /** @var d3ActionWizard */
protected $_oController; protected $_oController;
@ -118,26 +121,21 @@ class d3ActionWizardTest extends d3AdminControllerTest
$requestMock->expects($this->any())->method('getRequestEscapedParameter')->willReturnCallback([$this, 'executePassRequestCallback']); $requestMock->expects($this->any())->method('getRequestEscapedParameter')->willReturnCallback([$this, 'executePassRequestCallback']);
Registry::set(Request::class, $requestMock); Registry::set(Request::class, $requestMock);
/** @var Config|MockObject $configMock */ /** @var ModuleSettingService $settingsServiceMock */
$configMock = $this->getMockBuilder(Config::class) $settingsServiceMock = $this->getMockBuilder(ModuleSettingService::class)
->onlyMethods(['getConfigParam']) ->disableOriginalConstructor()
->onlyMethods(['getBoolean'])
->getMock(); ->getMock();
$configMock->expects($this->atLeastOnce())->method('getConfigParam')->willReturnCallback( $settingsServiceMock->expects($this->once())->method('getBoolean')->with(
function ($argName) use ($blDebug) { $this->identicalTo('d3datawizard_debug'),
switch ($argName) { $this->identicalTo(Constants::OXID_MODULE_ID)
case 'd3datawizard_debug': )->willReturn($blDebug);
return $blDebug;
default:
return Registry::getConfig()->getConfigParam($argName);
}
}
);
/** @var d3ActionWizard|MockObject $controllerMock */ /** @var d3ActionWizard|MockObject $controllerMock */
$controllerMock = $this->getMockBuilder(d3ActionWizard::class) $controllerMock = $this->getMockBuilder(d3ActionWizard::class)
->onlyMethods(['d3GetConfig']) ->onlyMethods(['getSettingsService'])
->getMock(); ->getMock();
$controllerMock->method('d3GetConfig')->willReturn($configMock); $controllerMock->method('getSettingsService')->willReturn($settingsServiceMock);
$this->_oController = $controllerMock; $this->_oController = $controllerMock;
/** @var d3TestAction|MockObject $actionMock */ /** @var d3TestAction|MockObject $actionMock */
@ -148,7 +146,7 @@ class d3ActionWizardTest extends d3AdminControllerTest
]) ])
->getMock(); ->getMock();
$actionMock->expects($this->atLeastOnce())->method('getQuery')->willReturn(['SELECT 1', ['1']]); $actionMock->expects($this->atLeastOnce())->method('getQuery')->willReturn(['SELECT 1', ['1']]);
$actionMock->expects($this->exactly((int) !$blDebug))->method('run')->willReturn(true); $actionMock->expects($this->exactly((int) !$blDebug))->method('run');
/** @var Configuration|MockObject $configurationMock */ /** @var Configuration|MockObject $configurationMock */
$configurationMock = $this->getMockBuilder(Configuration::class) $configurationMock = $this->getMockBuilder(Configuration::class)
@ -170,14 +168,11 @@ class d3ActionWizardTest extends d3AdminControllerTest
public function executePassRequestCallback($varName) public function executePassRequestCallback($varName)
{ {
switch ($varName) { return match ( $varName ) {
case 'taskid': 'taskid' => 'testTaskId',
return 'testTaskId'; 'format' => RendererBridge::FORMAT_CSV,
case 'format': default => oxNew( Request::class )->getRequestEscapedParameter( $varName ),
return RendererBridge::FORMAT_CSV; };
default:
return oxNew(Request::class)->getRequestEscapedParameter($varName);
}
} }
/** /**
@ -190,4 +185,15 @@ class d3ActionWizardTest extends d3AdminControllerTest
'debug' => [true], 'debug' => [true],
]; ];
} }
/**
* @return string[][]
*/
public function runTaskFailedDataProvider(): array
{
return [
[DataWizardException::class],
[DBALException::class],
];
}
} }

View File

@ -23,16 +23,18 @@ use D3\DataWizard\Application\Model\Exceptions\DebugException;
use D3\DataWizard\tests\tools\d3TestAction; use D3\DataWizard\tests\tools\d3TestAction;
use D3\ModCfg\Tests\unit\d3ModCfgUnitTestCase; use D3\ModCfg\Tests\unit\d3ModCfgUnitTestCase;
use Doctrine\DBAL\Exception as DBALException; use Doctrine\DBAL\Exception as DBALException;
use Exception;
use OxidEsales\Eshop\Core\Config; use OxidEsales\Eshop\Core\Config;
use OxidEsales\Eshop\Core\Exception\DatabaseErrorException; use OxidEsales\Eshop\Core\Exception\DatabaseErrorException;
use OxidEsales\Eshop\Core\Registry; use OxidEsales\Eshop\Core\Registry;
use OxidEsales\Eshop\Core\Request; use OxidEsales\Eshop\Core\Request;
use OxidEsales\Eshop\Core\UtilsView; use OxidEsales\Eshop\Core\UtilsView;
use OxidEsales\EshopCommunity\Internal\Framework\Module\Facade\ModuleSettingService;
use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use ReflectionException; use ReflectionException;
abstract class d3AdminControllerTest extends d3ModCfgUnitTestCase abstract class d3AdminController extends d3ModCfgUnitTestCase
{ {
/** @var d3ActionWizard|d3ExportWizard */ /** @var d3ActionWizard|d3ExportWizard */
protected $_oController; protected $_oController;
@ -54,7 +56,7 @@ abstract class d3AdminControllerTest extends d3ModCfgUnitTestCase
*/ */
public function testConstructor() public function testConstructor()
{ {
$this->setValue($this->_oController, 'configuration', null); $this->setValue($this->_oController, 'configuration', oxNew(Configuration::class));
$this->callMethod( $this->callMethod(
$this->_oController, $this->_oController,
@ -82,7 +84,7 @@ abstract class d3AdminControllerTest extends d3ModCfgUnitTestCase
$controllerMock = $this->getMockBuilder($this->testClassName) $controllerMock = $this->getMockBuilder($this->testClassName)
->onlyMethods(['execute']) ->onlyMethods(['execute'])
->getMock(); ->getMock();
$controllerMock->expects($this->once())->method('execute')->willReturn(true); $controllerMock->expects($this->once())->method('execute');
$this->_oController = $controllerMock; $this->_oController = $controllerMock;
@ -104,9 +106,8 @@ abstract class d3AdminControllerTest extends d3ModCfgUnitTestCase
{ {
/** @var DataWizardException|DBALException|DatabaseErrorException|MockObject $exceptionMock */ /** @var DataWizardException|DBALException|DatabaseErrorException|MockObject $exceptionMock */
$exceptionMock = $this->getMockBuilder($exceptionClass) $exceptionMock = $this->getMockBuilder($exceptionClass)
->disableOriginalConstructor() ->setConstructorArgs(['exc_msg', 20, new Exception()])
->getMock(); ->getMock();
$this->setValue($exceptionMock, 'message', 'exc_msg');
/** @var d3ActionWizard|d3ExportWizard|MockObject $controllerMock */ /** @var d3ActionWizard|d3ExportWizard|MockObject $controllerMock */
$controllerMock = $this->getMockBuilder($this->testClassName) $controllerMock = $this->getMockBuilder($this->testClassName)
@ -137,18 +138,6 @@ abstract class d3AdminControllerTest extends d3ModCfgUnitTestCase
); );
} }
/**
* @return \string[][]
*/
public function runTaskFailedDataProvider(): array
{
return [
[DataWizardException::class],
[DBALException::class],
[DatabaseErrorException::class],
];
}
/** /**
* @covers \D3\DataWizard\Application\Controller\Admin\d3ActionWizard::execute() * @covers \D3\DataWizard\Application\Controller\Admin\d3ActionWizard::execute()
* @covers \D3\DataWizard\Application\Controller\Admin\d3ExportWizard::execute() * @covers \D3\DataWizard\Application\Controller\Admin\d3ExportWizard::execute()
@ -244,18 +233,19 @@ abstract class d3AdminControllerTest extends d3ModCfgUnitTestCase
} }
/** /**
* @covers \D3\DataWizard\Application\Controller\Admin\d3ExportWizard::d3GetConfig
* @covers \D3\DataWizard\Application\Controller\Admin\d3ActionWizard::d3GetConfig
* @test * @test
* @return void
* @throws ReflectionException * @throws ReflectionException
* @covers \D3\DataWizard\Application\Controller\Admin\d3ActionWizard::getSettingsService()
* @covers \D3\DataWizard\Application\Controller\Admin\d3ExportWizard::getSettingsService()
*/ */
public function canGetConfig() public function canGetSettingsService(): void
{ {
$this->assertInstanceOf( $this->assertInstanceOf(
Config::class, ModuleSettingService::class,
$this->callMethod( $this->callMethod(
$this->_oController, $this->_oController,
'd3GetConfig' 'getSettingsService'
) )
); );
} }

View File

@ -17,17 +17,22 @@ namespace D3\DataWizard\tests\unit\Application\Controller\Admin;
use D3\DataWizard\Application\Controller\Admin\d3ExportWizard; use D3\DataWizard\Application\Controller\Admin\d3ExportWizard;
use D3\DataWizard\Application\Model\Configuration; use D3\DataWizard\Application\Model\Configuration;
use D3\DataWizard\Application\Model\Constants;
use D3\DataWizard\Application\Model\Exceptions\DataWizardException;
use D3\DataWizard\Application\Model\Exceptions\DebugException; use D3\DataWizard\Application\Model\Exceptions\DebugException;
use D3\DataWizard\Application\Model\ExportRenderer\RendererBridge; use D3\DataWizard\Application\Model\ExportRenderer\RendererBridge;
use D3\DataWizard\tests\tools\d3TestAction; use D3\DataWizard\tests\tools\d3TestAction;
use D3\DataWizard\tests\tools\d3TestExport; use D3\DataWizard\tests\tools\d3TestExport;
use Doctrine\DBAL\Exception as DBALException;
use OxidEsales\Eshop\Core\Config; use OxidEsales\Eshop\Core\Config;
use OxidEsales\Eshop\Core\Exception\DatabaseErrorException;
use OxidEsales\Eshop\Core\Registry; use OxidEsales\Eshop\Core\Registry;
use OxidEsales\Eshop\Core\Request; use OxidEsales\Eshop\Core\Request;
use OxidEsales\EshopCommunity\Internal\Framework\Module\Facade\ModuleSettingService;
use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\MockObject\MockObject;
use ReflectionException; use ReflectionException;
class d3ExportWizardTest extends d3AdminControllerTest class d3ExportWizardTest extends d3AdminController
{ {
/** @var d3ExportWizard */ /** @var d3ExportWizard */
protected $_oController; protected $_oController;
@ -120,26 +125,21 @@ class d3ExportWizardTest extends d3AdminControllerTest
//OnConsecutiveCalls('testTaskId', 'CSV'); //OnConsecutiveCalls('testTaskId', 'CSV');
Registry::set(Request::class, $requestMock); Registry::set(Request::class, $requestMock);
/** @var Config|MockObject $configMock */ /** @var ModuleSettingService $settingsServiceMock */
$configMock = $this->getMockBuilder(Config::class) $settingsServiceMock = $this->getMockBuilder(ModuleSettingService::class)
->onlyMethods(['getConfigParam']) ->disableOriginalConstructor()
->onlyMethods(['getBoolean'])
->getMock(); ->getMock();
$configMock->expects($this->atLeastOnce())->method('getConfigParam')->willReturnCallback( $settingsServiceMock->expects($this->once())->method('getBoolean')->with(
function ($argName) use ($blDebug) { $this->identicalTo('d3datawizard_debug'),
switch ($argName) { $this->identicalTo(Constants::OXID_MODULE_ID)
case 'd3datawizard_debug': )->willReturn($blDebug);
return $blDebug;
default:
return Registry::getConfig()->getConfigParam($argName);
}
}
);
/** @var d3ExportWizard|MockObject $controllerMock */ /** @var d3ExportWizard|MockObject $controllerMock */
$controllerMock = $this->getMockBuilder(d3ExportWizard::class) $controllerMock = $this->getMockBuilder(d3ExportWizard::class)
->onlyMethods(['d3GetConfig']) ->onlyMethods(['getSettingsService'])
->getMock(); ->getMock();
$controllerMock->method('d3GetConfig')->willReturn($configMock); $controllerMock->method('getSettingsService')->willReturn($settingsServiceMock);
$this->_oController = $controllerMock; $this->_oController = $controllerMock;
/** @var d3TestAction|MockObject $exportMock */ /** @var d3TestAction|MockObject $exportMock */
@ -192,4 +192,18 @@ class d3ExportWizardTest extends d3AdminControllerTest
'debug' => [true], 'debug' => [true],
]; ];
} }
/**
* @return string[][]
*/
public function runTaskFailedDataProvider(): array
{
return [
[DataWizardException::class],
[DBALException::class],
[DatabaseErrorException::class],
];
}
} }

View File

@ -18,6 +18,7 @@ namespace D3\DataWizard\tests\unit\Application\Model;
use D3\DataWizard\Application\Model\Exceptions\TaskException; use D3\DataWizard\Application\Model\Exceptions\TaskException;
use D3\DataWizard\tests\tools\d3TestAction; use D3\DataWizard\tests\tools\d3TestAction;
use D3\ModCfg\Tests\unit\d3ModCfgUnitTestCase; use D3\ModCfg\Tests\unit\d3ModCfgUnitTestCase;
use Doctrine\DBAL\Connection;
use FormManager\Inputs\Hidden; use FormManager\Inputs\Hidden;
use FormManager\Inputs\Number; use FormManager\Inputs\Number;
use FormManager\Inputs\Radio; use FormManager\Inputs\Radio;
@ -258,17 +259,18 @@ class ActionBaseTest extends d3ModCfgUnitTestCase
*/ */
public function canExecuteAction($query, $throwsException) public function canExecuteAction($query, $throwsException)
{ {
/** @var Database|MockObject $dbMock */ /** @var Database|MockObject $connectionMock */
$dbMock = $this->getMockBuilder(Database::class) $connectionMock = $this->getMockBuilder(Connection::class)
->onlyMethods(['execute']) ->disableOriginalConstructor()
->onlyMethods(['executeStatement'])
->getMock(); ->getMock();
$dbMock->expects($this->exactly((int) !$throwsException))->method('execute')->willReturn(true); $connectionMock->expects($this->exactly((int) !$throwsException))->method('executeStatement')->willReturn(1);
/** @var d3TestAction|MockObject $modelMock */ /** @var d3TestAction|MockObject $modelMock */
$modelMock = $this->getMockBuilder(d3TestAction::class) $modelMock = $this->getMockBuilder(d3TestAction::class)
->onlyMethods(['d3GetDb']) ->onlyMethods(['getConnection'])
->getMock(); ->getMock();
$modelMock->expects($this->exactly((int) !$throwsException))->method('d3GetDb')->willReturn($dbMock); $modelMock->expects($this->exactly((int) !$throwsException))->method('getConnection')->willReturn($connectionMock);
$this->_oModel = $modelMock; $this->_oModel = $modelMock;
@ -299,17 +301,17 @@ class ActionBaseTest extends d3ModCfgUnitTestCase
} }
/** /**
* @covers \D3\DataWizard\Application\Model\ActionBase::d3GetDb * @covers \D3\DataWizard\Application\Model\ActionBase::getConnection
* @test * @test
* @throws ReflectionException * @throws ReflectionException
*/ */
public function canGetDb() public function canGetConnection()
{ {
$this->assertInstanceOf( $this->assertInstanceOf(
Database::class, Connection::class,
$this->callMethod( $this->callMethod(
$this->_oModel, $this->_oModel,
'd3GetDb' 'getConnection'
) )
); );
} }