improve code

This commit is contained in:
Daniel Seifert 2024-01-31 21:56:19 +01:00
parent b58f1b4156
commit 3a1c270932
Signed by: DanielS
GPG Key ID: 8A7C4C6ED1915C6F
7 changed files with 55 additions and 32 deletions

View File

@ -28,7 +28,8 @@
"require-dev": { "require-dev": {
"d3/testingtools": "^1.0", "d3/testingtools": "^1.0",
"phpunit/phpunit": "^9.6", "phpunit/phpunit": "^9.6",
"friendsofphp/php-cs-fixer": "~3.13.0" "friendsofphp/php-cs-fixer": "~3.13.0",
"phpstan/phpstan": "^1.10"
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {
@ -41,6 +42,9 @@
"scripts": { "scripts": {
"php-cs-fixer": "./vendor/bin/php-cs-fixer fix --config=vendor/d3/oxid-dic-handler/.php-cs-fixer.php", "php-cs-fixer": "./vendor/bin/php-cs-fixer fix --config=vendor/d3/oxid-dic-handler/.php-cs-fixer.php",
"phpstan": "./vendor/bin/phpstan --configuration=vendor/d3/oxid-dic-handler/phpstan.neon analyse",
"phpstan-report": "./vendor/bin/phpstan --configuration=vendor/d3/ordermanager/phpstan.neon analyse --error-format=json > vendor/d3/ordermanager/tests/phpstan.report.json",
"phpunit": "XDEBUG_MODE=coverage ./vendor/bin/phpunit --bootstrap=source/bootstrap.php --config=vendor/d3/oxid-dic-handler/tests/" "phpunit": "XDEBUG_MODE=coverage ./vendor/bin/phpunit --bootstrap=source/bootstrap.php --config=vendor/d3/oxid-dic-handler/tests/"
} }
} }

View File

@ -35,13 +35,12 @@ class definitionFileContainer
} }
/** /**
* @param $definitionFile * @param string $definitionFile
* @param $type * @param string $type
* @throws InvalidArgumentException
* *
* @return void * @return void
*/ */
public function addDefinitions($definitionFile, $type): void public function addDefinitions(string $definitionFile, string $type): void
{ {
Assert::that($type)->inArray($this->allowedTypes, 'invalid definition file type'); Assert::that($type)->inArray($this->allowedTypes, 'invalid definition file type');
Assert::that(rtrim(dirname(__FILE__, 3).'/').$definitionFile)->file('invalid definition file'); Assert::that(rtrim(dirname(__FILE__, 3).'/').$definitionFile)->file('invalid definition file');
@ -49,18 +48,22 @@ class definitionFileContainer
$this->definitionFiles[$type][md5($definitionFile)] = $definitionFile; $this->definitionFiles[$type][md5($definitionFile)] = $definitionFile;
} }
public function addYamlDefinitions($definitionFile): void /**
* @param string $definitionFile
*
* @return void
*/
public function addYamlDefinitions(string $definitionFile): void
{ {
$this->addDefinitions($definitionFile, self::TYPE_YAML); $this->addDefinitions($definitionFile, self::TYPE_YAML);
} }
/** /**
* @param $type * @param string $type
* *
* @return array * @return array
* @throws InvalidArgumentException
*/ */
public function getDefinitions($type): array public function getDefinitions(string $type): array
{ {
Assert::that($type)->inArray($this->allowedTypes, 'invalid definition file type'); Assert::that($type)->inArray($this->allowedTypes, 'invalid definition file type');

View File

@ -1,11 +1,13 @@
parameters: parameters:
bootstrapFiles:
- ./tests/classAlias.php
scanFiles: scanFiles:
- ../../oxid-esales/oxideshop-ce/source/bootstrap.php - ../../oxid-esales/oxideshop-ce/source/bootstrap.php
- ../../oxid-esales/oxideshop-ce/source/oxfunctions.php - ../../oxid-esales/oxideshop-ce/source/oxfunctions.php
- ../../oxid-esales/oxideshop-ce/source/overridablefunctions.php - ../../oxid-esales/oxideshop-ce/source/overridablefunctions.php
paths: paths:
- . - .
level: 0 level: 8
phpVersion: 80300 phpVersion: 80300
checkMissingIterableValueType: false checkMissingIterableValueType: false
featureToggles: featureToggles:

18
tests/classAlias.php Normal file
View File

@ -0,0 +1,18 @@
<?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 <support@shopmodule.com>
* @link https://www.oxidmodule.com
*/
declare(strict_types=1);
use Symfony\Component\DependencyInjection\Container;
class_alias(Container::class, d3DIContainerCache::class);

View File

@ -152,7 +152,7 @@ class d3DicHandlerTest extends TestCase
$this->assertMatchesRegularExpression( $this->assertMatchesRegularExpression(
'/.*?\/tmp\/.*?DicContainer_\d+\.php$/m', '/.*?\/tmp\/.*?DicContainer_\d+\.php$/m',
$this->callMethod( (string) $this->callMethod(
$sut, $sut,
'd3GetCacheFilePath' 'd3GetCacheFilePath'
) )
@ -215,14 +215,12 @@ class d3DicHandlerTest extends TestCase
$containerBuilderMock = $this->getMockBuilder(ContainerBuilder::class) $containerBuilderMock = $this->getMockBuilder(ContainerBuilder::class)
->getMock(); ->getMock();
/** @var YamlFileLoader|MockObject $fileLoaderMock */
$fileLoaderMock = $this->getMockBuilder(YamlFileLoader::class) $fileLoaderMock = $this->getMockBuilder(YamlFileLoader::class)
->disableOriginalConstructor() ->disableOriginalConstructor()
->onlyMethods(['load']) ->onlyMethods(['load'])
->getMock(); ->getMock();
$fileLoaderMock->expects($this->atLeastOnce())->method('load'); $fileLoaderMock->expects($this->atLeastOnce())->method('load');
/** @var d3DicHandler|MockObject $sut */
$sut = $this->getMockBuilder(d3DicHandler::class) $sut = $this->getMockBuilder(d3DicHandler::class)
->onlyMethods(['d3GetFileLoader']) ->onlyMethods(['d3GetFileLoader'])
->getMock(); ->getMock();
@ -254,15 +252,15 @@ class d3DicHandlerTest extends TestCase
} }
/** /**
* @param bool $cacheExist
* @test * @test
* @return void * @return void
* @throws ReflectionException * @throws ReflectionException
* @dataProvider cacheFileExistsTestDataProvider * @dataProvider cacheFileExistsTestDataProvider
* @covers \D3\DIContainerHandler\d3DicHandler::cacheFileExists * @covers \D3\DIContainerHandler\d3DicHandler::cacheFileExists
*/ */
public function cacheFileExistsTest($cacheExist) public function cacheFileExistsTest(bool $cacheExist)
{ {
/** @var d3DicHandler|MockObject $sut */
if (!$cacheExist) { if (!$cacheExist) {
$sut = $this->getMockBuilder(d3DicHandler::class) $sut = $this->getMockBuilder(d3DicHandler::class)
->onlyMethods(['d3GetCacheFilePath']) ->onlyMethods(['d3GetCacheFilePath'])
@ -309,18 +307,15 @@ class d3DicHandlerTest extends TestCase
$cachedContainerMock = $this->getMockBuilder(d3DIContainerCache::class) $cachedContainerMock = $this->getMockBuilder(d3DIContainerCache::class)
->getMock(); ->getMock();
/** @var ContainerBuilder|MockObject $containerBuilderMock */
$containerBuilderMock = $this->getMockBuilder(ContainerBuilder::class)->onlyMethods([ 'compile' ])->getMock(); $containerBuilderMock = $this->getMockBuilder(ContainerBuilder::class)->onlyMethods([ 'compile' ])->getMock();
$containerBuilderMock->expects($this->exactly((int) ! $cachedContainer))->method('compile'); $containerBuilderMock->expects($this->exactly((int) ! $cachedContainer))->method('compile');
/** @var Config|MockObject $configMock */
$configMock = $this->getMockBuilder(Config::class) $configMock = $this->getMockBuilder(Config::class)
->onlyMethods(['isProductiveMode', 'getConfigParam']) ->onlyMethods(['isProductiveMode', 'getConfigParam'])
->getMock(); ->getMock();
$configMock->method('isProductiveMode')->willReturn($productive); $configMock->method('isProductiveMode')->willReturn($productive);
$configMock->method('getConfigParam')->willReturnMap([['iDebug', $debug]]); $configMock->method('getConfigParam')->willReturnMap([['iDebug', $debug]]);
/** @var d3DicHandler|MockObject $sut */
$sut = $this->getMockBuilder(d3DicHandler::class) $sut = $this->getMockBuilder(d3DicHandler::class)
->onlyMethods(['d3GetConfig', 'd3GetCacheContainer', 'getContainerBuilder', 'isNotInTest', 'cacheFileExists']) ->onlyMethods(['d3GetConfig', 'd3GetCacheContainer', 'getContainerBuilder', 'isNotInTest', 'cacheFileExists'])
->getMock(); ->getMock();

View File

@ -28,16 +28,17 @@ class d3DicUtilitiesTest extends TestCase
/** /**
* @test * @test
*
* @param string $className * @param string $className
* @param string|null $additional * @param string|null $additional
* @param $expected * @param string $expected
* *
* @return void * @return void
* @throws ReflectionException * @throws ReflectionException
* @covers \D3\DIContainerHandler\d3DicUtilities::getServiceId * @covers \D3\DIContainerHandler\d3DicUtilities::getServiceId
* @dataProvider getServiceIdTestDataProvider * @dataProvider getServiceIdTestDataProvider
*/ */
public function getServiceIdTest(string $className, string $additional = null, $expected): void public function getServiceIdTest(string $className, string $additional = null, string $expected): void
{ {
$sut = oxNew(d3DicUtilities::class); $sut = oxNew(d3DicUtilities::class);
@ -62,14 +63,14 @@ class d3DicUtilitiesTest extends TestCase
* *
* @param string $className * @param string $className
* @param string $argumentName * @param string $argumentName
* @param $expected * @param string $expected
* *
* @return void * @return void
* @throws ReflectionException * @throws ReflectionException
* @covers \D3\DIContainerHandler\d3DicUtilities::getArgumentId * @covers \D3\DIContainerHandler\d3DicUtilities::getArgumentId
* @dataProvider getArgumentIdTestDataProvider * @dataProvider getArgumentIdTestDataProvider
*/ */
public function getArgumentIdTest(string $className, string $argumentName, $expected): void public function getArgumentIdTest(string $className, string $argumentName, string $expected): void
{ {
$sut = oxNew(d3DicUtilities::class); $sut = oxNew(d3DicUtilities::class);
@ -99,7 +100,7 @@ class d3DicUtilitiesTest extends TestCase
$sut = oxNew(d3DicUtilities::class); $sut = oxNew(d3DicUtilities::class);
$this->assertDirectoryExists( $this->assertDirectoryExists(
$this->callMethod( (string) $this->callMethod(
$sut, $sut,
'getVendorDir' 'getVendorDir'
) )

View File

@ -29,9 +29,9 @@ class definitionFileContainerTest extends TestCase
/** /**
* @test * @test
* *
* @param $file * @param string $file
* @param $type * @param string $type
* @param $sumand * @param int $sumand
* @param bool $expectException * @param bool $expectException
* *
* @return void * @return void
@ -39,7 +39,7 @@ class definitionFileContainerTest extends TestCase
* @dataProvider addDefinitionsTestDataProvider * @dataProvider addDefinitionsTestDataProvider
* @covers \D3\DIContainerHandler\definitionFileContainer::addDefinitions * @covers \D3\DIContainerHandler\definitionFileContainer::addDefinitions
*/ */
public function addDefinitionsTest($file, $type, $sumand, bool $expectException): void public function addDefinitionsTest(string $file, string $type, int $sumand, bool $expectException): void
{ {
$sut = oxNew(definitionFileContainer::class); $sut = oxNew(definitionFileContainer::class);
$sut->clear(); $sut->clear();
@ -188,7 +188,7 @@ class definitionFileContainerTest extends TestCase
$this->assertCount( $this->assertCount(
0, 0,
$this->callMethod( (array) $this->callMethod(
$sut, $sut,
'getAll' 'getAll'
) )