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

196 lines
6.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\DebugException;
2021-11-26 23:16:32 +01:00
use D3\DataWizard\Application\Model\ExportRenderer\RendererBridge;
2021-11-24 23:38:28 +01:00
use D3\DataWizard\tests\tools\d3TestAction;
use D3\DataWizard\tests\tools\d3TestExport;
use OxidEsales\Eshop\Core\Config;
use OxidEsales\Eshop\Core\Registry;
use OxidEsales\Eshop\Core\Request;
use PHPUnit\Framework\MockObject\MockObject;
use ReflectionException;
class d3ExportWizardTest extends d3AdminControllerTest
{
/** @var d3ExportWizard */
protected $_oController;
2021-11-26 23:16:32 +01:00
protected $testClassName = d3ExportWizard::class;
2021-11-24 23:38:28 +01:00
public function setUp() : void
{
parent::setUp();
2021-11-26 23:16:32 +01:00
$this->_oController = oxNew($this->testClassName);
2021-11-24 23:38:28 +01:00
}
/**
2021-11-26 23:16:32 +01:00
* @covers \D3\DataWizard\Application\Controller\Admin\d3ExportWizard::getGroups()
2021-11-24 23:38:28 +01:00
* @test
* @throws ReflectionException
*/
public function canGetGroups()
{
$expected = ['expected' => 'array'];
/** @var Configuration|MockObject $configurationMock */
$configurationMock = $this->getMockBuilder(Configuration::class)
->onlyMethods(['getExportGroups'])
->getMock();
$configurationMock->expects($this->atLeastOnce())->method('getExportGroups')->willReturn($expected);
$this->setValue($this->_oController, 'configuration', $configurationMock);
$this->assertSame(
$expected,
$this->callMethod(
$this->_oController,
'getGroups'
)
);
}
/**
2021-11-26 23:16:32 +01:00
* @covers \D3\DataWizard\Application\Controller\Admin\d3ExportWizard::getGroupTasks()
2021-11-24 23:38:28 +01:00
* @test
* @throws ReflectionException
* @dataProvider canGetGroupTasksDataProvider
*/
public function canGetGroupTasks($argument)
{
$expected = ['expected' => 'array'];
/** @var Configuration|MockObject $configurationMock */
$configurationMock = $this->getMockBuilder(Configuration::class)
->onlyMethods(['getExportsByGroup'])
->getMock();
$configurationMock->expects($this->atLeastOnce())->method('getExportsByGroup')->with($argument)->willReturn($expected);
$this->setValue($this->_oController, 'configuration', $configurationMock);
$this->assertSame(
$expected,
$this->callMethod(
$this->_oController,
'getGroupTasks',
[$argument]
)
);
}
/**
* @return array
*/
public function canGetGroupTasksDataProvider(): array
{
return [
['test1'],
['test2']
];
}
/**
2021-11-26 23:16:32 +01:00
* @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();
2021-11-26 23:16:32 +01:00
$requestMock->expects($this->any())->method('getRequestEscapedParameter')->willReturnCallback([$this, 'executePassRequestCallback']);
//OnConsecutiveCalls('testTaskId', 'CSV');
2021-11-24 23:38:28 +01:00
Registry::set(Request::class, $requestMock);
2021-11-26 23:16:32 +01:00
/** @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;
2021-11-24 23:38:28 +01:00
/** @var d3TestAction|MockObject $exportMock */
$exportMock = $this->getMockBuilder(d3TestExport::class)
->onlyMethods([
'getQuery',
'run'
])
->getMock();
$exportMock->expects($this->atLeastOnce())->method('getQuery')->willReturn(['SELECT 1', ['1']]);
$exportMock->expects($this->exactly((int) !$blDebug))->method('run')->willReturn('');
/** @var Configuration|MockObject $configurationMock */
$configurationMock = $this->getMockBuilder(Configuration::class)
->onlyMethods(['getExportById'])
->disableOriginalConstructor()
->getMock();
$configurationMock->expects($this->atLeastOnce())->method('getExportById')->with('testTaskId')->willReturn($exportMock);
$this->setValue($this->_oController, 'configuration', $configurationMock);
if ($blDebug) {
$this->expectException(DebugException::class);
}
$this->callMethod(
$this->_oController,
'execute'
);
}
2021-11-26 23:16:32 +01:00
public function executePassRequestCallback($varName)
{
switch ($varName) {
case 'taskid':
return 'testTaskId';
case 'format':
return RendererBridge::FORMAT_CSV;
default:
return oxNew(Request::class)->getRequestEscapedParameter($varName);
}
}
2021-11-24 23:38:28 +01:00
/**
* @return array
*/
public function executePassDataProvider(): array
{
return [
'no debug' => [false],
'debug' => [true],
];
}
}