DataWizard/tests/unit/Application/Controller/Admin/d3AdminController.php

253 lines
8.4 KiB
PHP
Raw Normal View History

2021-11-24 23:38:28 +01:00
<?php
/**
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* https://www.d3data.de
*
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
* @author D3 Data Development - Daniel Seifert <info@shopmodule.com>
* @link https://www.oxidmodule.com
*/
declare(strict_types=1);
namespace D3\DataWizard\tests\unit\Application\Controller\Admin;
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\DataWizardException;
use D3\DataWizard\Application\Model\Exceptions\DebugException;
use D3\DataWizard\tests\tools\d3TestAction;
use D3\ModCfg\Tests\unit\d3ModCfgUnitTestCase;
2024-03-15 15:56:17 +01:00
use Doctrine\DBAL\Exception as DBALException;
2024-05-13 15:23:47 +02:00
use Exception;
2021-11-24 23:38:28 +01:00
use OxidEsales\Eshop\Core\Config;
use OxidEsales\Eshop\Core\Exception\DatabaseErrorException;
use OxidEsales\Eshop\Core\Registry;
use OxidEsales\Eshop\Core\Request;
use OxidEsales\Eshop\Core\UtilsView;
2024-05-13 15:23:47 +02:00
use OxidEsales\EshopCommunity\Internal\Framework\Module\Facade\ModuleSettingService;
2021-11-24 23:38:28 +01:00
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
use ReflectionException;
2024-05-13 15:23:47 +02:00
abstract class d3AdminController extends d3ModCfgUnitTestCase
2021-11-24 23:38:28 +01:00
{
/** @var d3ActionWizard|d3ExportWizard */
protected $_oController;
2021-11-26 23:16:32 +01:00
protected $testClassName;
2022-01-17 10:59:18 +01:00
public function tearDown(): void
2021-11-24 23:38:28 +01:00
{
parent::tearDown();
unset($this->_oController);
}
/**
* @covers \D3\DataWizard\Application\Controller\Admin\d3ActionWizard::__construct
* @covers \D3\DataWizard\Application\Controller\Admin\d3ExportWizard::__construct
* @test
* @throws ReflectionException
*/
public function testConstructor()
{
2024-05-13 15:23:47 +02:00
$this->setValue($this->_oController, 'configuration', oxNew(Configuration::class));
2021-11-24 23:38:28 +01:00
$this->callMethod(
$this->_oController,
'__construct'
);
$this->assertInstanceOf(
Configuration::class,
$this->getValue(
$this->_oController,
'configuration'
)
);
}
/**
2021-11-25 23:42:41 +01:00
* @covers \D3\DataWizard\Application\Controller\Admin\d3ActionWizard::runTask()
* @covers \D3\DataWizard\Application\Controller\Admin\d3ExportWizard::runTask()
2021-11-24 23:38:28 +01:00
* @test
* @throws ReflectionException
*/
public function runTaskPass()
{
2021-11-26 23:16:32 +01:00
/** @var d3ActionWizard|d3ExportWizard|MockObject $controllerMock */
$controllerMock = $this->getMockBuilder($this->testClassName)
2021-11-24 23:38:28 +01:00
->onlyMethods(['execute'])
->getMock();
2024-05-13 15:23:47 +02:00
$controllerMock->expects($this->once())->method('execute');
2021-11-24 23:38:28 +01:00
$this->_oController = $controllerMock;
$this->callMethod(
$this->_oController,
'runTask'
);
}
/**
2021-11-25 23:42:41 +01:00
* @covers \D3\DataWizard\Application\Controller\Admin\d3ActionWizard::runTask()
* @covers \D3\DataWizard\Application\Controller\Admin\d3ExportWizard::runTask()
2021-11-24 23:38:28 +01:00
* @test
* @param $exceptionClass
* @throws ReflectionException
* @dataProvider runTaskFailedDataProvider
*/
public function runTaskFailed($exceptionClass)
{
/** @var DataWizardException|DBALException|DatabaseErrorException|MockObject $exceptionMock */
$exceptionMock = $this->getMockBuilder($exceptionClass)
2024-05-13 15:23:47 +02:00
->setConstructorArgs(['exc_msg', 20, new Exception()])
2021-11-24 23:38:28 +01:00
->getMock();
2021-11-26 23:16:32 +01:00
/** @var d3ActionWizard|d3ExportWizard|MockObject $controllerMock */
$controllerMock = $this->getMockBuilder($this->testClassName)
2021-11-24 23:38:28 +01:00
->onlyMethods(['execute'])
->getMock();
$controllerMock->expects($this->once())->method('execute')->willThrowException($exceptionMock);
/** @var LoggerInterface|MockObject $loggerMock */
$loggerMock = $this->getMockBuilder(get_class(Registry::getLogger()))
->onlyMethods(['error'])
->disableOriginalConstructor()
->getMock();
$loggerMock->expects($this->atLeastOnce())->method('error')->with('exc_msg')->willReturn(true);
Registry::set('logger', $loggerMock);
/** @var UtilsView|MockObject $utilsViewMock */
$utilsViewMock = $this->getMockBuilder(get_class(Registry::getUtilsView()))
->onlyMethods(['addErrorToDisplay'])
->getMock();
$utilsViewMock->expects($this->atLeastOnce())->method('addErrorToDisplay')->with($exceptionMock)->willReturn(true);
Registry::set(UtilsView::class, $utilsViewMock);
$this->_oController = $controllerMock;
$this->callMethod(
$this->_oController,
'runTask'
);
}
/**
2021-11-25 23:42:41 +01:00
* @covers \D3\DataWizard\Application\Controller\Admin\d3ActionWizard::execute()
* @covers \D3\DataWizard\Application\Controller\Admin\d3ExportWizard::execute()
2021-11-24 23:38:28 +01:00
* @test
* @throws ReflectionException
* @dataProvider executePassDataProvider
*/
public function executePass($blDebug)
{
/** @var Request|MockObject $requestMock */
$requestMock = $this->getMockBuilder(get_class(Registry::getRequest()))
->onlyMethods(['getRequestEscapedParameter'])
->getMock();
$requestMock->expects($this->atLeastOnce())->method('getRequestEscapedParameter')->with('taskid')->willReturn('testTaskId');
Registry::set(Request::class, $requestMock);
/** @var d3TestAction|MockObject $actionMock */
$actionMock = $this->getMockBuilder(d3TestAction::class)
->onlyMethods([
'getQuery',
2022-01-17 10:59:18 +01:00
'run',
2021-11-24 23:38:28 +01:00
])
->getMock();
$actionMock->expects($this->atLeastOnce())->method('getQuery')->willReturn(['SELECT 1', ['1']]);
$actionMock->expects($this->exactly((int) !$blDebug))->method('run')->willReturn(true);
/** @var Configuration|MockObject $configurationMock */
$configurationMock = $this->getMockBuilder(Configuration::class)
->onlyMethods(['getActionById'])
->disableOriginalConstructor()
->getMock();
$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);
}
$this->callMethod(
$this->_oController,
'execute'
);
}
/**
* @return array
*/
public function executePassDataProvider(): array
{
return [
'no debug' => [false],
'debug' => [true],
];
}
/**
2021-11-25 23:42:41 +01:00
* @covers \D3\DataWizard\Application\Controller\Admin\d3ActionWizard::getUserMessages()
* @covers \D3\DataWizard\Application\Controller\Admin\d3ExportWizard::getUserMessages()
2021-11-24 23:38:28 +01:00
* @test
* @throws ReflectionException
*/
public function canGetUserMessages()
{
$this->assertNull(
$this->callMethod(
$this->_oController,
'getUserMessages'
)
);
}
/**
2021-11-25 23:42:41 +01:00
* @covers \D3\DataWizard\Application\Controller\Admin\d3ActionWizard::getHelpURL()
* @covers \D3\DataWizard\Application\Controller\Admin\d3ExportWizard::getHelpURL()
2021-11-24 23:38:28 +01:00
* @test
* @throws ReflectionException
*/
public function canGetHelpUrl()
{
$this->assertNull(
$this->callMethod(
$this->_oController,
'getHelpURL'
)
);
}
2021-11-26 23:16:32 +01:00
/**
* @test
2024-05-13 15:23:47 +02:00
* @return void
2021-11-26 23:16:32 +01:00
* @throws ReflectionException
2024-05-13 15:23:47 +02:00
* @covers \D3\DataWizard\Application\Controller\Admin\d3ActionWizard::getSettingsService()
* @covers \D3\DataWizard\Application\Controller\Admin\d3ExportWizard::getSettingsService()
2021-11-26 23:16:32 +01:00
*/
2024-05-13 15:23:47 +02:00
public function canGetSettingsService(): void
2021-11-26 23:16:32 +01:00
{
$this->assertInstanceOf(
2024-05-13 15:23:47 +02:00
ModuleSettingService::class,
2021-11-26 23:16:32 +01:00
$this->callMethod(
$this->_oController,
2024-05-13 15:23:47 +02:00
'getSettingsService'
2021-11-26 23:16:32 +01:00
)
);
}
2022-01-17 10:59:18 +01:00
}