Vergelijk commits
9 Commits
Auteur | SHA1 | Datum | |
---|---|---|---|
2300246ab8 | |||
2f12e28020 | |||
31c3ac4e50 | |||
3368873774 | |||
5bf406e06a | |||
bf1066294a | |||
6a07afeac4 | |||
1b2f3e0d0b | |||
afdb6ff4c1 |
15
CHANGELOG.md
15
CHANGELOG.md
@ -4,7 +4,20 @@ All notable changes to this project will be documented in this file.
|
||||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
||||
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||
|
||||
## [Unreleased](https://git.d3data.de/D3Public/DIContainer/compare/1.0.0.0...rel_1.x)
|
||||
## [Unreleased](https://git.d3data.de/D3Public/DIContainer/compare/2.1.0.0...rel_2.x)
|
||||
|
||||
## [2.1.0.0](https://git.d3data.de/D3Public/DIContainer/compare/2.0.0.0...2.1.0.0) - 2024-06-05
|
||||
### Changed
|
||||
- trigger error instead of throwing exception (exception version optional)
|
||||
- cache container decision ignores debug mode
|
||||
- improve profiling performce
|
||||
|
||||
## [2.0.0.0](https://git.d3data.de/D3Public/DIContainer/compare/1.0.0.0...2.0.0.0) - 2024-01-31
|
||||
### Added
|
||||
- installable in OXID 7
|
||||
|
||||
### Changed
|
||||
- retrieve definition files from vendor folder
|
||||
|
||||
## [1.0.0.0](https://git.d3data.de/D3Public/DIContainer/releases/tag/1.0.0.0) - 2023-01-10
|
||||
### Added
|
||||
|
@ -3,11 +3,11 @@
|
||||
|
||||
# Dependency Injection Container handler for OXID eShop
|
||||
|
||||
Enables the simple use of a class container outside of the OXID DI Services.
|
||||
Enables the simple use of a class container outside the OXID DI Services.
|
||||
|
||||
## Install
|
||||
|
||||
This package requires an Composer installed OXID eShop as defined in [composer.json](composer.json).
|
||||
This package requires a 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 informations.
|
||||
See [CHANGELOG](CHANGELOG.md) for further information.
|
||||
|
||||
## Contributing
|
||||
|
||||
|
@ -13,13 +13,29 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use D3\DIContainerHandler\d3DicException;
|
||||
use D3\DIContainerHandler\d3DicHandler;
|
||||
use Symfony\Component\DependencyInjection\Container;
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
* @return Container
|
||||
* @throws d3DicException
|
||||
*/
|
||||
function d3GetOxidDIC(): Container
|
||||
function d3GetOxidDIC_withExceptions(): Container
|
||||
{
|
||||
return d3DicHandler::getInstance();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Container
|
||||
*/
|
||||
function d3GetOxidDIC(): Container
|
||||
{
|
||||
try {
|
||||
return d3GetOxidDIC_withExceptions();
|
||||
// @codeCoverageIgnoreStart
|
||||
} catch (d3DicException $exception) {
|
||||
trigger_error($exception->getMessage(), E_USER_ERROR);
|
||||
}
|
||||
// @codeCoverageIgnoreEnd
|
||||
}
|
||||
|
@ -30,7 +30,8 @@
|
||||
"phpunit/phpunit": "^9.6",
|
||||
"friendsofphp/php-cs-fixer": "~3.13.0",
|
||||
"phpstan/phpstan": "^1.10",
|
||||
"rector/rector": "^0.18.13"
|
||||
"rector/rector": "^0.18.13",
|
||||
"mikey179/vfsstream": "^1.6.8"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
@ -47,6 +48,7 @@
|
||||
"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"
|
||||
}
|
||||
|
26
d3DicException.php
Normal file
26
d3DicException.php
Normal file
@ -0,0 +1,26 @@
|
||||
<?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);
|
||||
}
|
||||
}
|
115
d3DicHandler.php
115
d3DicHandler.php
@ -35,47 +35,21 @@ class d3DicHandler implements d3DicHandlerInterface
|
||||
];
|
||||
|
||||
/**
|
||||
* get instance
|
||||
*
|
||||
* @throws Exception
|
||||
* @return Container
|
||||
* @throws d3DicException
|
||||
*/
|
||||
public static function getInstance(): Container
|
||||
{
|
||||
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
|
||||
$caller = $trace[1];
|
||||
$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");
|
||||
}
|
||||
|
||||
if (null == self::$_instance) {
|
||||
$oDicHandler = oxNew(d3DicHandler::class);
|
||||
self::$_instance = $oDicHandler->buildContainer();
|
||||
}
|
||||
|
||||
return self::$_instance;
|
||||
return oxNew(d3DicHandler::class)->createInstance();
|
||||
}
|
||||
|
||||
/**
|
||||
* get instance
|
||||
*
|
||||
* @throws Exception
|
||||
* @throws d3DicException
|
||||
*/
|
||||
public static function getUncompiledInstance(): Container
|
||||
{
|
||||
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
|
||||
$caller = $trace[1];
|
||||
$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;
|
||||
return oxNew(d3DicHandler::class)->createInstance(false);
|
||||
}
|
||||
|
||||
public static function removeInstance(): void
|
||||
@ -83,6 +57,31 @@ class d3DicHandler implements d3DicHandlerInterface
|
||||
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);
|
||||
}
|
||||
|
||||
return self::$_instance;
|
||||
}
|
||||
|
||||
protected function getFunctionNameFromTrace()
|
||||
{
|
||||
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
|
||||
$caller = $trace[1];
|
||||
return $caller['function'];
|
||||
}
|
||||
|
||||
public function d3GetConfig(): Config
|
||||
{
|
||||
return Registry::getConfig();
|
||||
@ -112,6 +111,8 @@ class d3DicHandler implements d3DicHandlerInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ContainerBuilder $container
|
||||
* @return void
|
||||
* @throws Exception
|
||||
*/
|
||||
public function loadFiles(ContainerBuilder $container): void
|
||||
@ -127,32 +128,21 @@ 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
|
||||
{
|
||||
if ((bool) Registry::get(ConfigFile::class)->getVar('iDebug')) {
|
||||
startProfile(__METHOD__);
|
||||
}
|
||||
startProfile(__METHOD__);
|
||||
|
||||
$config = $this->d3GetConfig();
|
||||
|
||||
if ($config->isProductiveMode()
|
||||
&& ! $config->getConfigParam('iDebug')
|
||||
&& $this->isNotInTest()
|
||||
&& $this->cacheFileExists()
|
||||
) {
|
||||
if ($this->d3UseCachedContainer()) {
|
||||
$container = $this->d3GetCacheContainer();
|
||||
} else {
|
||||
$container = $this->getContainerBuilder();
|
||||
@ -160,37 +150,32 @@ class d3DicHandler implements d3DicHandlerInterface
|
||||
|
||||
if ($compileAndDump) {
|
||||
$container->compile();
|
||||
|
||||
if ($this->isNotInTest()) {
|
||||
$dumper = new PhpDumper($container);
|
||||
file_put_contents($this->d3GetCacheFilePath(), $dumper->dump(['class' => 'd3DIContainerCache']));
|
||||
}
|
||||
$dumper = $this->getPhpDumper($container);
|
||||
file_put_contents($this->d3GetCacheFilePath(), $dumper->dump(['class' => 'd3DIContainerCache']));
|
||||
}
|
||||
}
|
||||
|
||||
if ((bool) Registry::get(ConfigFile::class)->getVar('iDebug')) {
|
||||
stopProfile(__METHOD__);
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* clone
|
||||
*/
|
||||
public function __clone()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* constructor
|
||||
*/
|
||||
public function __construct()
|
||||
public function getPhpDumper(ContainerBuilder $containerBuilder): PhpDumper
|
||||
{
|
||||
return new PhpDumper( $containerBuilder);
|
||||
}
|
||||
}
|
||||
|
@ -16,7 +16,6 @@ declare(strict_types=1);
|
||||
namespace D3\DIContainerHandler;
|
||||
|
||||
use Assert\Assert;
|
||||
use Assert\InvalidArgumentException;
|
||||
|
||||
class definitionFileContainer
|
||||
{
|
||||
@ -32,6 +31,7 @@ class definitionFileContainer
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
/** keep clear */
|
||||
}
|
||||
|
||||
public function addDefinitions(string $definitionFile, string $type): void
|
||||
|
17
rector.php
17
rector.php
@ -2,27 +2,10 @@
|
||||
|
||||
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([
|
||||
|
@ -19,6 +19,11 @@
|
||||
<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">
|
||||
|
@ -15,6 +15,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace D3\DIContainerHandler\tests\autoload;
|
||||
|
||||
use D3\DIContainerHandler\d3DicException;
|
||||
use D3\TestingTools\Development\CanAccessRestricted;
|
||||
use Exception;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
@ -27,9 +28,27 @@ 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()
|
||||
|
50
tests/unit/d3DicExceptionTest.php
Normal file
50
tests/unit/d3DicExceptionTest.php
Normal file
@ -0,0 +1,50 @@
|
||||
<?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()
|
||||
);
|
||||
}
|
||||
}
|
@ -15,23 +15,33 @@ 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
|
||||
@ -118,6 +128,65 @@ 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
|
||||
@ -225,23 +294,6 @@ 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
|
||||
@ -276,52 +328,102 @@ 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 $productive, int $debug, bool $notInTest, bool $cacheFileExist, bool $cachedContainer): void
|
||||
public function buildContainerTest(bool $useCacheContainer, bool $compile): void
|
||||
{
|
||||
$cachedContainerMock = $this->getMockBuilder(d3DIContainerCache::class)
|
||||
->getMock();
|
||||
$structure = [
|
||||
'source_directory' => [],
|
||||
];
|
||||
vfsStream::setup();
|
||||
$fsRoot = vfsStream::create($structure);
|
||||
|
||||
$containerBuilderMock = $this->getMockBuilder(ContainerBuilder::class)->onlyMethods([ 'compile' ])->getMock();
|
||||
$containerBuilderMock->expects($this->exactly((int) ! $cachedContainer))->method('compile');
|
||||
$containerBuilderMock->expects($this->exactly((int) (!$useCacheContainer && $compile)))->method('compile');
|
||||
|
||||
$configMock = $this->getMockBuilder(Config::class)
|
||||
->onlyMethods(['isProductiveMode', 'getConfigParam'])
|
||||
/** @var PhpDumper|MockObject $phpDumperMock */
|
||||
$phpDumperMock = $this->getMockBuilder(PhpDumper::class)
|
||||
->disableOriginalConstructor()
|
||||
->onlyMethods(get_class_methods(PhpDumper::class))
|
||||
->getMock();
|
||||
$configMock->method('isProductiveMode')->willReturn($productive);
|
||||
$configMock->method('getConfigParam')->willReturnMap([['iDebug', $debug]]);
|
||||
$phpDumperMock->expects($this->exactly((int) (!$useCacheContainer && $compile)))->method('dump');
|
||||
|
||||
/** @var d3DicHandler|MockObject $sut */
|
||||
$sut = $this->getMockBuilder(d3DicHandler::class)
|
||||
->onlyMethods(['d3GetConfig', 'd3GetCacheContainer', 'getContainerBuilder', 'isNotInTest', 'cacheFileExists'])
|
||||
->onlyMethods(['d3UseCachedContainer', 'd3GetCacheContainer', 'getContainerBuilder', 'd3GetCacheFilePath', 'getPhpDumper'])
|
||||
->getMock();
|
||||
$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);
|
||||
$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);
|
||||
|
||||
$this->assertSame(
|
||||
$cachedContainer ? $cachedContainerMock : $containerBuilderMock,
|
||||
$this->assertInstanceOf(
|
||||
Container::class,
|
||||
$this->callMethod(
|
||||
$sut,
|
||||
'buildContainer',
|
||||
['false']
|
||||
[$compile]
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public function buildContainerTestDataProvider(): Generator
|
||||
{
|
||||
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];
|
||||
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, true];
|
||||
yield 'no cache file' => [true, 0, false, false];
|
||||
yield 'can use cached' => [true, 0, true, true];
|
||||
}
|
||||
|
||||
/**
|
||||
@ -341,4 +443,28 @@ 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]
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -29,13 +29,15 @@ 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 = null, string $expected): void
|
||||
public function getServiceIdTest(string $className, ?string $additional, string $expected): void
|
||||
{
|
||||
$sut = oxNew(d3DicUtilities::class);
|
||||
|
||||
|
@ -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
|
||||
{
|
||||
|
Laden…
x
Verwijs in nieuw issue
Block a user