Vergelijk commits

..

No commits in common. "rel_2.x" and "main" have entirely different histories.

13 gewijzigde bestanden met toevoegingen van 135 en 349 verwijderingen

Bestand weergeven

@ -3,11 +3,11 @@
# Dependency Injection Container handler for OXID eShop
Enables the simple use of a class container outside the OXID DI Services.
Enables the simple use of a class container outside of the OXID DI Services.
## Install
This package requires a Composer installed OXID eShop as defined in [composer.json](composer.json).
This package requires an Composer installed OXID eShop as defined in [composer.json](composer.json).
Open a command line interface and navigate to the shop root directory (parent of source and vendor). Execute the following command. Adapt the paths to your environment.
@ -31,7 +31,7 @@ Then empty the TMP folder.
## Changelog
See [CHANGELOG](CHANGELOG.md) for further information.
See [CHANGELOG](CHANGELOG.md) for further informations.
## Contributing

Bestand weergeven

@ -13,29 +13,13 @@
declare(strict_types=1);
use D3\DIContainerHandler\d3DicException;
use D3\DIContainerHandler\d3DicHandler;
use Symfony\Component\DependencyInjection\Container;
/**
* @return Container
* @throws d3DicException
*/
function d3GetOxidDIC_withExceptions(): Container
{
return d3DicHandler::getInstance();
}
/**
* @return Container
* @throws Exception
*/
function d3GetOxidDIC(): Container
{
try {
return d3GetOxidDIC_withExceptions();
// @codeCoverageIgnoreStart
} catch (d3DicException $exception) {
trigger_error($exception->getMessage(), E_USER_ERROR);
}
// @codeCoverageIgnoreEnd
return d3DicHandler::getInstance();
}

Bestand weergeven

@ -30,8 +30,7 @@
"phpunit/phpunit": "^9.6",
"friendsofphp/php-cs-fixer": "~3.13.0",
"phpstan/phpstan": "^1.10",
"rector/rector": "^0.18.13",
"mikey179/vfsstream": "^1.6.8"
"rector/rector": "^0.18.13"
},
"autoload": {
"psr-4": {
@ -48,7 +47,6 @@
"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-coverage": "XDEBUG_MODE=coverage ./vendor/bin/phpunit --bootstrap=source/bootstrap.php --config=vendor/d3/oxid-dic-handler/tests/ --coverage-html=vendor/d3/oxid-dic-handler/tests/result/coverage",
"rector": "./vendor/bin/rector process --dry-run --config ./vendor/d3/oxid-dic-handler/rector.php"
}

Bestand weergeven

@ -1,26 +0,0 @@
<?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);
namespace D3\DIContainerHandler;
use Exception;
class d3DicException extends Exception
{
public function __construct(Exception $previous)
{
parent::__construct($previous->getMessage(), $previous->getCode(), $previous);
}
}

Bestand weergeven

@ -35,51 +35,52 @@ class d3DicHandler implements d3DicHandlerInterface
];
/**
* @return Container
* @throws d3DicException
* get instance
*
* @throws Exception
*/
public static function getInstance(): Container
{
return oxNew(d3DicHandler::class)->createInstance();
}
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
$caller = $trace[1];
$functionName = $caller['function'];
/**
* get instance
* @throws d3DicException
*/
public static function getUncompiledInstance(): Container
{
return oxNew(d3DicHandler::class)->createInstance(false);
}
if (in_array(strtolower($functionName), array_map('strtolower', self::$circularReferenceMethodNames))) {
throw oxNew(Exception::class, 'method '.$functionName." can't use DIC due the danger of circular reference");
}
public static function removeInstance(): void
{
self::$_instance = null;
}
public function createInstance(bool $compiled = true): Container
{
try {
$functionName = $this->getFunctionNameFromTrace();
if (in_array(strtolower($functionName), array_map('strtolower', self::$circularReferenceMethodNames))) {
throw oxNew(Exception::class, 'method ' . $functionName . " can't use DIC due the danger of circular reference");
}
if (null == self::$_instance) {
self::$_instance = $this->buildContainer($compiled);
}
} catch (Exception $exception) {
throw new d3DicException($exception);
if (null == self::$_instance) {
$oDicHandler = oxNew(d3DicHandler::class);
self::$_instance = $oDicHandler->buildContainer();
}
return self::$_instance;
}
protected function getFunctionNameFromTrace()
/**
* get instance
*
* @throws Exception
*/
public static function getUncompiledInstance(): Container
{
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
$caller = $trace[1];
return $caller['function'];
$functionName = $caller['function'];
if (in_array(strtolower($functionName), array_map('strtolower', self::$circularReferenceMethodNames))) {
throw oxNew(Exception::class, 'method '.$functionName." can't use DIC due the danger of circular reference");
}
$oDicHandler = oxNew(d3DicHandler::class);
self::$_instance = $oDicHandler->buildContainer(false);
return self::$_instance;
}
public static function removeInstance(): void
{
self::$_instance = null;
}
public function d3GetConfig(): Config
@ -111,8 +112,6 @@ class d3DicHandler implements d3DicHandlerInterface
}
/**
* @param ContainerBuilder $container
* @return void
* @throws Exception
*/
public function loadFiles(ContainerBuilder $container): void
@ -128,21 +127,32 @@ class d3DicHandler implements d3DicHandlerInterface
}
}
protected function isNotInTest(): bool
{
return false == defined('OXID_PHP_UNIT') || true == defined('D3_MODCFG_TEST');
}
protected function cacheFileExists(): bool
{
return file_exists($this->d3GetCacheFilePath());
}
/**
* @param bool $compileAndDump
* @return Container
* @throws Exception
*/
public function buildContainer(bool $compileAndDump = true): Container
{
startProfile(__METHOD__);
if ((bool) Registry::get(ConfigFile::class)->getVar('iDebug')) {
startProfile(__METHOD__);
}
if ($this->d3UseCachedContainer()) {
$config = $this->d3GetConfig();
if ($config->isProductiveMode()
&& ! $config->getConfigParam('iDebug')
&& $this->isNotInTest()
&& $this->cacheFileExists()
) {
$container = $this->d3GetCacheContainer();
} else {
$container = $this->getContainerBuilder();
@ -150,32 +160,37 @@ class d3DicHandler implements d3DicHandlerInterface
if ($compileAndDump) {
$container->compile();
$dumper = $this->getPhpDumper($container);
file_put_contents($this->d3GetCacheFilePath(), $dumper->dump(['class' => 'd3DIContainerCache']));
if ($this->isNotInTest()) {
$dumper = new PhpDumper($container);
file_put_contents($this->d3GetCacheFilePath(), $dumper->dump(['class' => 'd3DIContainerCache']));
}
}
}
stopProfile(__METHOD__);
if ((bool) Registry::get(ConfigFile::class)->getVar('iDebug')) {
stopProfile(__METHOD__);
}
return $container;
}
protected function d3UseCachedContainer(): bool
{
$config = $this->d3GetConfig();
return $config->isProductiveMode()
&& !$config->getConfigParam('iDebug')
&& $this->cacheFileExists();
}
public function getContainerBuilder(): ContainerBuilder
{
return oxNew(ContainerBuilder::class);
}
public function getPhpDumper(ContainerBuilder $containerBuilder): PhpDumper
/**
* clone
*/
public function __clone()
{
}
/**
* constructor
*/
public function __construct()
{
return new PhpDumper( $containerBuilder);
}
}

Bestand weergeven

@ -16,6 +16,7 @@ declare(strict_types=1);
namespace D3\DIContainerHandler;
use Assert\Assert;
use Assert\InvalidArgumentException;
class definitionFileContainer
{
@ -31,7 +32,6 @@ class definitionFileContainer
public function __construct()
{
/** keep clear */
}
public function addDefinitions(string $definitionFile, string $type): void

Bestand weergeven

@ -2,10 +2,27 @@
declare(strict_types=1);
use Rector\CodeQuality\Rector\Class_\CompleteDynamicPropertiesRector;
use Rector\CodeQuality\Rector\ClassMethod\InlineArrayReturnAssignRector;
use Rector\CodeQuality\Rector\Foreach_\ForeachItemsAssignToEmptyArrayToAssignRector;
use Rector\Config\RectorConfig;
use Rector\DeadCode\Rector\Assign\RemoveDoubleAssignRector;
use Rector\DeadCode\Rector\If_\RemoveUnusedNonEmptyArrayBeforeForeachRector;
use Rector\DeadCode\Rector\StaticCall\RemoveParentCallWithoutParentRector;
use Rector\Php80\Rector\FunctionLike\MixedTypeRector;
use Rector\Set\ValueObject\LevelSetList;
use Rector\Set\ValueObject\SetList;
use Rector\TypeDeclaration\Rector\ClassMethod\AddVoidReturnTypeWhereNoReturnRector;
use Rector\TypeDeclaration\Rector\ClassMethod\BoolReturnTypeFromStrictScalarReturnsRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ParamTypeByMethodCallTypeRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromReturnDirectArrayRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictBoolReturnExprRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictNewArrayRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictScalarReturnExprRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictTypedCallRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnUnionTypeRector;
use Rector\TypeDeclaration\Rector\ClassMethod\StrictArrayParamDimFetchRector;
use Rector\TypeDeclaration\Rector\ClassMethod\StrictStringParamConcatRector;
return static function (RectorConfig $rectorConfig): void {
$rectorConfig->paths([

Bestand weergeven

@ -19,11 +19,6 @@
<include>
<directory suffix=".php">../</directory>
</include>
<exclude>
<directory suffix=".php">./</directory>
<file>../rector.php</file>
<file>../.php-cs-fixer.php</file>
</exclude>
</coverage>
<testsuites>
<testsuite name="Unit">

Bestand weergeven

@ -15,7 +15,6 @@ declare(strict_types=1);
namespace D3\DIContainerHandler\tests\autoload;
use D3\DIContainerHandler\d3DicException;
use D3\TestingTools\Development\CanAccessRestricted;
use Exception;
use PHPUnit\Framework\TestCase;
@ -28,27 +27,9 @@ class functions_oxDICTest extends TestCase
/**
* @test
* @throws Exception
* @covers ::d3GetOxidDIC_withExceptions()
*/
public function d3GetOxidDIC_withExceptionsTest(): void
{
error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING);
$this->assertInstanceOf(
ContainerBuilder::class,
d3GetOxidDIC_withExceptions()
);
}
/**
* @test
* @throws Exception
* @covers ::d3GetOxidDIC()
*/
public function d3GetOxidDICTest(): void
{
error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING);
$this->assertInstanceOf(
ContainerBuilder::class,
d3GetOxidDIC()

Bestand weergeven

@ -1,50 +0,0 @@
<?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);
namespace D3\DIContainerHandler\tests;
use D3\DIContainerHandler\d3DicException;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
class d3DicExceptionTest extends TestCase
{
/**
* @test
* @return void
* @covers \D3\DIContainerHandler\d3DicException::__construct
*/
public function canConstruct()
{
$previousMessage = 'previousMessage';
$previousCode = 123;
$previous = new InvalidArgumentException($previousMessage, $previousCode);
$exception = new d3DicException($previous);
$this->assertSame(
$previous,
$exception->getPrevious()
);
$this->assertSame(
$previousMessage,
$exception->getMessage()
);
$this->assertSame(
$previousCode,
$exception->getCode()
);
}
}

Bestand weergeven

@ -15,33 +15,23 @@ declare(strict_types=1);
namespace D3\DIContainerHandler\tests;
use D3\DIContainerHandler\d3DicException;
use D3\DIContainerHandler\d3DicHandler;
use D3\TestingTools\Development\CanAccessRestricted;
use d3DIContainerCache;
use Exception;
use Generator;
use org\bovigo\vfs\vfsStream;
use OxidEsales\Eshop\Core\Config;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use ReflectionException;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
define('D3_MODCFG_TEST', true);
class d3DicHandlerTest extends TestCase
{
use CanAccessRestricted;
public function setUp(): void
{
parent::setUp();
d3DicHandler::removeInstance();
}
/**
* @test
* @throws ReflectionException
@ -128,65 +118,6 @@ class d3DicHandlerTest extends TestCase
);
}
/**
* @test
* @param bool $throwException
* @param bool $expectException
* @param string $circularReferenceMethod
*
* @return void
* @throws ReflectionException
* @covers \D3\DIContainerHandler\d3DicHandler::createInstance
* @dataProvider canCreateInstanceDataProvider
*/
public function canCreateInstance(bool $throwException, bool $expectException, string $circularReferenceMethod = '')
{
/** @var d3DicHandler|MockObject $sut */
$sut = $this->getMockBuilder(d3DicHandler::class)
->onlyMethods(['buildContainer', 'getFunctionNameFromTrace'])
->getMock();
if ($throwException)
$sut->method( 'buildContainer' )->willThrowException( new Exception( 'fixture' ) );
$sut->method('getFunctionNameFromTrace')->willReturn($circularReferenceMethod);
if ($expectException)
$this->expectException(d3DicException::class);
$this->callMethod(
$sut,
'createInstance'
);
}
public function canCreateInstanceDataProvider(): Generator
{
yield "don't throw exception" => [false, false];
yield "throw exception" => [true, true];
yield "has circular reference method name" => [false, true, 'getViewConfig'];
}
/**
* @test
* @return void
* @throws ReflectionException
* @covers \D3\DIContainerHandler\d3DicHandler::getFunctionNameFromTrace
*/
public function canGetFunctionNameFromTrace()
{
/** @var d3DicHandler|MockObject $sut */
$sut = $this->getMockBuilder(d3DicHandler::class)
->onlyMethods(get_class_methods(d3DicHandler::class))
->getMock();
$this->assertSame(
'invokeArgs',
$this->callMethod(
$sut,
'getFunctionNameFromTrace'
)
);
}
/**
* @test
* @throws ReflectionException
@ -294,6 +225,23 @@ class d3DicHandlerTest extends TestCase
);
}
/**
* @test
* @throws ReflectionException
* @covers \D3\DIContainerHandler\d3DicHandler::isNotInTest
*/
public function isNotInTest(): void
{
$sut = new d3DicHandler();
$this->assertTrue(
$this->callMethod(
$sut,
'isNotInTest'
)
);
}
/**
* @test
* @throws ReflectionException
@ -328,102 +276,52 @@ class d3DicHandlerTest extends TestCase
/**
* @test
* @param bool $useCacheContainer
* @param bool $compile
*
* @return void
*
* @throws ReflectionException
* @dataProvider buildContainerTestDataProvider
* @covers \D3\DIContainerHandler\d3DicHandler::buildContainer
*/
public function buildContainerTest(bool $useCacheContainer, bool $compile): void
public function buildContainerTest(bool $productive, int $debug, bool $notInTest, bool $cacheFileExist, bool $cachedContainer): void
{
$structure = [
'source_directory' => [],
];
vfsStream::setup();
$fsRoot = vfsStream::create($structure);
$cachedContainerMock = $this->getMockBuilder(d3DIContainerCache::class)
->getMock();
$containerBuilderMock = $this->getMockBuilder(ContainerBuilder::class)->onlyMethods([ 'compile' ])->getMock();
$containerBuilderMock->expects($this->exactly((int) (!$useCacheContainer && $compile)))->method('compile');
$containerBuilderMock->expects($this->exactly((int) ! $cachedContainer))->method('compile');
/** @var PhpDumper|MockObject $phpDumperMock */
$phpDumperMock = $this->getMockBuilder(PhpDumper::class)
->disableOriginalConstructor()
->onlyMethods(get_class_methods(PhpDumper::class))
$configMock = $this->getMockBuilder(Config::class)
->onlyMethods(['isProductiveMode', 'getConfigParam'])
->getMock();
$phpDumperMock->expects($this->exactly((int) (!$useCacheContainer && $compile)))->method('dump');
$configMock->method('isProductiveMode')->willReturn($productive);
$configMock->method('getConfigParam')->willReturnMap([['iDebug', $debug]]);
/** @var d3DicHandler|MockObject $sut */
$sut = $this->getMockBuilder(d3DicHandler::class)
->onlyMethods(['d3UseCachedContainer', 'd3GetCacheContainer', 'getContainerBuilder', 'd3GetCacheFilePath', 'getPhpDumper'])
->onlyMethods(['d3GetConfig', 'd3GetCacheContainer', 'getContainerBuilder', 'isNotInTest', 'cacheFileExists'])
->getMock();
$sut->expects($this->once())->method('d3UseCachedContainer')->willReturn($useCacheContainer);
$sut->expects($this->exactly((int) $useCacheContainer))->method('d3GetCacheContainer');
$sut->expects($this->exactly((int) !$useCacheContainer))->method('getContainerBuilder')->willReturn($containerBuilderMock);
$sut->method('d3GetCacheFilePath')->willReturn($fsRoot->getChild('source_directory')->path().'/DIContainer.php');
$sut->method('getPhpDumper')->willReturn($phpDumperMock);
$sut->method('d3GetConfig')->willReturn($configMock);
$sut->expects($this->exactly((int) $cachedContainer))->method('d3GetCacheContainer')->willReturn($cachedContainerMock);
$sut->expects($this->exactly((int) !$cachedContainer))->method('getContainerBuilder')->willReturn($containerBuilderMock);
$sut->method('isNotInTest')->willReturn($notInTest);
$sut->method('cacheFileExists')->willReturn($cacheFileExist);
$this->assertInstanceOf(
Container::class,
$this->assertSame(
$cachedContainer ? $cachedContainerMock : $containerBuilderMock,
$this->callMethod(
$sut,
'buildContainer',
[$compile]
['false']
)
);
}
public function buildContainerTestDataProvider(): Generator
{
yield "can't use cached container, do compile" => [false, true];
yield "can't use cached container, don't compile" => [false, false];
yield "use cached container" => [true, false];
}
/**
* @test
* @param bool $productive
* @param int $debug
* @param bool $cacheFileExist
* @param bool $expected
*
* @return void
* @throws ReflectionException
* @covers \D3\DIContainerHandler\d3DicHandler::d3UseCachedContainer
* @dataProvider canUseCachedContainerDataProvider
*/
public function canUseCachedContainerTest(bool $productive, int $debug, bool $cacheFileExist, bool $expected)
{
/** @var Config|MockObject $configMock */
$configMock = $this->getMockBuilder(Config::class)
->onlyMethods(['isProductiveMode', 'getConfigParam'])
->getMock();
$configMock->method('isProductiveMode')->willReturn($productive);
$configMock->method('getConfigParam')->willReturnMap([['iDebug', NULL, $debug]]);
/** @var d3DicHandler|MockObject $sut */
$sut = $this->getMockBuilder(d3DicHandler::class)
->onlyMethods(['d3GetConfig', 'cacheFileExists'])
->getMock();
$sut->method('d3GetConfig')->willReturn($configMock);
$sut->method('cacheFileExists')->willReturn($cacheFileExist);
$this->assertSame(
$expected,
$this->callMethod(
$sut,
'd3UseCachedContainer'
)
);
}
public function canUseCachedContainerDataProvider(): Generator
{
yield "not productive" => [false, 0, true, false];
yield 'is debug' => [true, 1, true, false];
yield 'no cache file' => [true, 0, false, false];
yield 'can use cached' => [true, 0, true, true];
yield 'notProductive' => [false, 0, false, true, false];
yield 'debug' => [true, 1, false, true, false];
yield 'inTest' => [true, 0, false, true, false];
yield 'cacheFileNotExist' => [true, 0, false, false, false];
yield 'cachedContainer' => [true, 0, true, true, true];
}
/**
@ -443,28 +341,4 @@ class d3DicHandlerTest extends TestCase
)
);
}
/**
* @test
* @return void
* @throws ReflectionException
* @covers \D3\DIContainerHandler\d3DicHandler::getPhpDumper
*/
public function canGetPhpDumper(): void
{
/** @var ContainerBuilder|MockObject $containerBuilderMock */
$containerBuilderMock = $this->getMockBuilder(ContainerBuilder::class)
->onlyMethods(['isCompiled'])
->getMock();
$containerBuilderMock->method('isCompiled')->willReturn(true);
$this->assertInstanceOf(
PhpDumper::class,
$this->callMethod(
new d3DicHandler(),
'getPhpDumper',
[$containerBuilderMock]
)
);
}
}

Bestand weergeven

@ -29,15 +29,13 @@ class d3DicUtilitiesTest extends TestCase
/**
* @test
*
* @param string $className
* @param string|null $additional
* @param string $expected
*
* @throws ReflectionException
* @covers \D3\DIContainerHandler\d3DicUtilities::getServiceId
* @dataProvider getServiceIdTestDataProvider
*/
public function getServiceIdTest(string $className, ?string $additional, string $expected): void
public function getServiceIdTest(string $className, string $additional = null, string $expected): void
{
$sut = oxNew(d3DicUtilities::class);

Bestand weergeven

@ -29,10 +29,10 @@ class definitionFileContainerTest extends TestCase
/**
* @test
*
*
* @throws ReflectionException
* @dataProvider addDefinitionsTestDataProvider
* @covers \D3\DIContainerHandler\definitionFileContainer::addDefinitions
* @covers \D3\DIContainerHandler\definitionFileContainer::__construct
*/
public function addDefinitionsTest(string $file, string $type, int $sumand, bool $expectException): void
{