DataWizard/tests/unit/Application/Model/ActionBaseTest.php

319 lignes
9.1 KiB
PHP
Brut Vue normale Historique

2021-11-26 23:16:32 +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\Model;
2021-11-28 01:04:46 +01:00
use D3\DataWizard\Application\Model\Exceptions\TaskException;
2021-11-26 23:16:32 +01:00
use D3\DataWizard\tests\tools\d3TestAction;
use D3\ModCfg\Tests\unit\d3ModCfgUnitTestCase;
2024-05-13 15:23:47 +02:00
use Doctrine\DBAL\Connection;
2021-11-26 23:16:32 +01:00
use FormManager\Inputs\Hidden;
use FormManager\Inputs\Number;
use FormManager\Inputs\Radio;
2021-11-28 01:04:46 +01:00
use OxidEsales\Eshop\Core\Database\Adapter\Doctrine\Database;
2021-11-26 23:16:32 +01:00
use OxidEsales\Eshop\Core\Exception\StandardException;
use PHPUnit\Framework\MockObject\MockObject;
use ReflectionException;
class ActionBaseTest extends d3ModCfgUnitTestCase
{
/** @var d3TestAction */
protected $_oModel;
2022-01-17 10:59:18 +01:00
public function setUp(): void
2021-11-26 23:16:32 +01:00
{
parent::setUp();
$this->_oModel = oxNew(d3TestAction::class);
}
2022-01-17 10:59:18 +01:00
public function tearDown(): void
2021-11-26 23:16:32 +01:00
{
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'
)
);
}
2021-12-20 13:41:24 +01:00
public function canGetHasFormElementsDataProvider(): array
2021-11-26 23:16:32 +01:00
{
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',
2022-01-17 10:59:18 +01:00
'setAttribute',
2021-11-26 23:16:32 +01:00
])
->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],
2022-01-17 10:59:18 +01:00
'Hidden' => [Hidden::class],
2021-11-26 23:16:32 +01:00
];
}
/**
* @covers \D3\DataWizard\Application\Model\ActionBase::run
* @test
* @throws ReflectionException
*/
public function canRunWithoutFormElements()
{
$modelMock = $this->getMockBuilder(d3TestAction::class)
->onlyMethods([
'hasFormElements',
'executeAction',
2022-01-17 10:59:18 +01:00
'getQuery',
2021-11-26 23:16:32 +01:00
])
->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',
2022-01-17 10:59:18 +01:00
'getFormElements',
2021-11-26 23:16:32 +01:00
])
->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],
2022-01-17 10:59:18 +01:00
'invalidElements' => [[$validMock, $invalidField], true],
2021-11-26 23:16:32 +01:00
];
}
2021-11-28 01:04:46 +01:00
/**
* @covers \D3\DataWizard\Application\Model\ActionBase::executeAction
* @test
* @throws ReflectionException
* @dataProvider canExecuteActionDataProvider
*/
public function canExecuteAction($query, $throwsException)
{
2024-05-13 15:23:47 +02:00
/** @var Database|MockObject $connectionMock */
$connectionMock = $this->getMockBuilder(Connection::class)
->disableOriginalConstructor()
->onlyMethods(['executeStatement'])
2021-11-28 01:04:46 +01:00
->getMock();
2024-05-13 15:23:47 +02:00
$connectionMock->expects($this->exactly((int) !$throwsException))->method('executeStatement')->willReturn(1);
2021-11-28 01:04:46 +01:00
/** @var d3TestAction|MockObject $modelMock */
$modelMock = $this->getMockBuilder(d3TestAction::class)
2024-05-13 15:23:47 +02:00
->onlyMethods(['getConnection'])
2021-11-28 01:04:46 +01:00
->getMock();
2024-05-13 15:23:47 +02:00
$modelMock->expects($this->exactly((int) !$throwsException))->method('getConnection')->willReturn($connectionMock);
2021-11-28 01:04:46 +01:00
$this->_oModel = $modelMock;
try {
$this->callMethod(
$this->_oModel,
'executeAction',
[[$query, ['parameters']]]
);
} catch (TaskException $e) {
if ($throwsException) {
$this->assertStringContainsString('ACTIONSELECT', $e->getMessage());
} else {
$this->assertStringContainsString('ACTIONRESULT', $e->getMessage());
}
}
}
/**
* @return array[]
*/
public function canExecuteActionDataProvider(): array
{
return [
'Select throws exception' => ['SELECT 1', true],
'Update dont throws exception' => ['UPDATE 1', false],
];
}
/**
2024-05-13 15:23:47 +02:00
* @covers \D3\DataWizard\Application\Model\ActionBase::getConnection
2021-11-28 01:04:46 +01:00
* @test
* @throws ReflectionException
*/
2024-05-13 15:23:47 +02:00
public function canGetConnection()
2021-11-28 01:04:46 +01:00
{
$this->assertInstanceOf(
2024-05-13 15:23:47 +02:00
Connection::class,
2021-11-28 01:04:46 +01:00
$this->callMethod(
$this->_oModel,
2024-05-13 15:23:47 +02:00
'getConnection'
2021-11-28 01:04:46 +01:00
)
);
}
2022-01-17 10:59:18 +01:00
}