Compare commits

...

10 Commits

Author SHA1 Message Date
Daniel Seifert 943adfaa5e
improve code 2024-01-31 22:09:36 +01:00
Daniel Seifert 3a1c270932
improve code 2024-01-31 21:56:19 +01:00
Daniel Seifert b58f1b4156
improve code style 2024-01-31 21:17:51 +01:00
Daniel Seifert 8345a843c3
use testing tools 2024-01-31 21:13:53 +01:00
Daniel Seifert f5afd4617f
do refactoring 2024-01-31 20:56:30 +01:00
Daniel Seifert 8cc981902b
add tests 2024-01-31 19:24:51 +01:00
Daniel Seifert dd077688ad
fix code style 2023-11-23 15:05:40 +01:00
Daniel Seifert 53a9effeac
add missing type hints 2023-11-23 15:01:44 +01:00
Daniel Seifert 5e573a2435
don't use source directory for plugin code search 2023-07-26 11:56:01 +02:00
Daniel Seifert 814bf56964
fix package description 2023-01-11 23:29:43 +01:00
16 changed files with 901 additions and 95 deletions

13
.php-cs-fixer.php Normal file
View File

@ -0,0 +1,13 @@
<?php
$finder = PhpCsFixer\Finder::create()
->in(__DIR__)
;
$config = new PhpCsFixer\Config();
return $config->setRules([
'@PHP80Migration' => true,
'@PSR12' => true
])
->setFinder($finder)
;

View File

@ -14,12 +14,12 @@
declare(strict_types=1);
use D3\DIContainerHandler\d3DicHandler;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Container;
/**
* @return ContainerBuilder
* @throws Exception
*/
function d3GetOxidDIC(): ContainerBuilder
function d3GetOxidDIC(): Container
{
return d3DicHandler::getInstance();
}

View File

@ -22,7 +22,15 @@
"MIT"
],
"require": {
"symfony/dependency-injection": "^3.1|^5.4.11|^6.0"
"symfony/dependency-injection": "^3.1|^5.4.11|^6.0",
"beberlei/assert": "^3.3"
},
"require-dev": {
"d3/testingtools": "^1.0",
"phpunit/phpunit": "^9.6",
"friendsofphp/php-cs-fixer": "~3.13.0",
"phpstan/phpstan": "^1.10",
"rector/rector": "^0.18.13"
},
"autoload": {
"psr-4": {
@ -31,5 +39,15 @@
"files": [
"autoload/functions_oxDIC.php"
]
},
"scripts": {
"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/",
"rector": "./vendor/bin/rector process --dry-run --config ./vendor/d3/oxid-dic-handler/rector.php"
}
}

View File

@ -17,37 +17,35 @@ namespace D3\DIContainerHandler;
use d3DIContainerCache;
use Exception;
use OxidEsales\Eshop\Core\Config;
use OxidEsales\Eshop\Core\Registry;
use OxidEsales\Facts\Config\ConfigFile;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
class d3DicHandler implements d3DicHandlerInterface
{
/**
* instance
* @var array
*/
protected static $_instance;
protected static Container|null $_instance = null;
public static $circularReferenceMethodNames = array(
'getViewConfig'
);
public static array $circularReferenceMethodNames = [
'getViewConfig',
];
/**
* get instance
* @return ContainerBuilder
*
* @throws Exception
*/
public static function getInstance()
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)))
{
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");
}
@ -61,16 +59,16 @@ class d3DicHandler implements d3DicHandlerInterface
/**
* get instance
* @return ContainerBuilder
*
* @throws Exception
*/
public static function getUncompiledInstance()
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)))
{
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");
}
@ -80,78 +78,80 @@ class d3DicHandler implements d3DicHandlerInterface
return self::$_instance;
}
public static function removeInstance()
public static function removeInstance(): void
{
self::$_instance = null;
}
/**
* @return mixed
*/
public function d3GetConfig()
public function d3GetConfig(): Config
{
return Registry::getConfig();
}
public function d3GetCacheFilePath()
public function d3GetCacheFilePath(): string
{
return $this->d3GetConfig()->getConfigParam('sCompileDir').'/d3DicContainer_'.Registry::getConfig()->getShopId().'.php';
}
/**
* @return d3DIContainerCache|object
*/
public function d3GetCacheContainer()
public function d3GetCacheContainer(): Container
{
require_once $this->d3GetCacheFilePath();
return oxNew(d3DIContainerCache::class);
}
/**
* @param $container
* @return YamlFileLoader
*/
public function d3GetFileLoader($container)
public function d3GetFileLoader(ContainerBuilder $container): YamlFileLoader
{
/** @var YamlFileLoader $fileLoader */
$fileLoader = oxNew(YamlFileLoader::class,
$fileLoader = oxNew(
YamlFileLoader::class,
$container,
oxNew(FileLocator::class, $this->d3GetConfig()->getModulesDir())
oxNew(FileLocator::class, d3DicUtilities::getVendorDir())
);
return $fileLoader;
}
/**
* @param $container
* @throws Exception
*/
public function loadFiles($container)
public function loadFiles(ContainerBuilder $container): void
{
$loader = $this->d3GetFileLoader($container);
$fileContainer = oxNew(definitionFileContainer::class);
foreach ($fileContainer->getYamlDefinitions() as $file) {
$fullPath = $this->d3GetConfig()->getModulesDir().$file;
$fullPath = d3DicUtilities::getVendorDir().$file;
if (is_file($fullPath)) {
$loader->load($file);
}
}
}
/**
* @param bool $compileAndDump
* @return ContainerBuilder
*/
public function buildContainer(bool $compileAndDump = true)
protected function isNotInTest(): bool
{
if ((bool) Registry::get( ConfigFile::class)->getVar( 'iDebug')) startProfile(__METHOD__);
return false == defined('OXID_PHP_UNIT') || true == defined('D3_MODCFG_TEST');
}
protected function cacheFileExists(): bool
{
return file_exists($this->d3GetCacheFilePath());
}
/**
* @throws Exception
*/
public function buildContainer(bool $compileAndDump = true): Container
{
if ((bool) Registry::get(ConfigFile::class)->getVar('iDebug')) {
startProfile(__METHOD__);
}
$config = $this->d3GetConfig();
if ($config->isProductiveMode()
&& false == $config->getConfigParam('iDebug')
&& (false == defined('OXID_PHP_UNIT') || true == defined('D3_MODCFG_TEST'))
&& file_exists($this->d3GetCacheFilePath())
&& ! $config->getConfigParam('iDebug')
&& $this->isNotInTest()
&& $this->cacheFileExists()
) {
$container = $this->d3GetCacheContainer();
} else {
@ -161,19 +161,21 @@ class d3DicHandler implements d3DicHandlerInterface
if ($compileAndDump) {
$container->compile();
if (false == defined('OXID_PHP_UNIT')) {
if ($this->isNotInTest()) {
$dumper = new PhpDumper($container);
file_put_contents($this->d3GetCacheFilePath(), $dumper->dump(array('class' => 'd3DIContainerCache')));
file_put_contents($this->d3GetCacheFilePath(), $dumper->dump(['class' => 'd3DIContainerCache']));
}
}
}
if ((bool) Registry::get( ConfigFile::class)->getVar( 'iDebug')) stopProfile(__METHOD__);
if ((bool) Registry::get(ConfigFile::class)->getVar('iDebug')) {
stopProfile(__METHOD__);
}
return $container;
}
public function getContainerBuilder()
public function getContainerBuilder(): ContainerBuilder
{
return oxNew(ContainerBuilder::class);
}
@ -181,10 +183,14 @@ class d3DicHandler implements d3DicHandlerInterface
/**
* clone
*/
public function __clone() {}
public function __clone()
{
}
/**
* constructor
*/
public function __construct() {}
public function __construct()
{
}
}

View File

@ -15,10 +15,9 @@ declare(strict_types=1);
namespace D3\DIContainerHandler;
/**
* Interface d3DicHandlerInterface
*/
use Symfony\Component\DependencyInjection\Container;
interface d3DicHandlerInterface
{
public static function getInstance();
}
public static function getInstance(): Container;
}

View File

@ -18,11 +18,10 @@ namespace D3\DIContainerHandler;
class d3DicUtilities
{
/**
* @param $classNameSpace
* @param bool $additional
* @return string
* @param string|null $additional
*
*/
public static function getServiceId($classNameSpace, $additional = false)
public static function getServiceId(string $classNameSpace, string $additional = null): string
{
return strtolower(
($additional ? $additional.'.' : '').
@ -30,12 +29,7 @@ class d3DicUtilities
);
}
/**
* @param $classNameSpace
* @param $argumentName
* @return string
*/
public static function getArgumentId($classNamespace, $argumentName)
public static function getArgumentId(string $classNamespace, string $argumentName): string
{
return strtolower(
$classNamespace.
@ -43,4 +37,9 @@ class d3DicUtilities
$argumentName
);
}
public static function getVendorDir(): string
{
return rtrim(dirname(__FILE__, 3), '/') . '/';
}
}

View File

@ -15,67 +15,60 @@ declare(strict_types=1);
namespace D3\DIContainerHandler;
use Assert\Assert;
use Assert\InvalidArgumentException;
class definitionFileContainer
{
public const TYPE_YAML = 'yml';
protected $definitionFiles = [
self::TYPE_YAML => []
protected array $definitionFiles = [
self::TYPE_YAML => [],
];
protected $allowedTypes = [
self::TYPE_YAML
protected array $allowedTypes = [
self::TYPE_YAML,
];
public function __construct()
{
$this->addYamlDefinitions('d3/modcfg/Config/services.yaml');
}
public function addDefinitions($definitionFile, $type)
public function addDefinitions(string $definitionFile, string $type): void
{
if (!in_array($type, $this->allowedTypes)) {
throw new \InvalidArgumentException('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');
$this->definitionFiles[$type][md5($definitionFile)] = $definitionFile;
}
public function addYamlDefinitions($definitionFile)
public function addYamlDefinitions(string $definitionFile): void
{
$this->addDefinitions($definitionFile, self::TYPE_YAML);
}
public function getDefinitions($type)
public function getDefinitions(string $type): array
{
if (!in_array($type, $this->allowedTypes)) {
throw new \InvalidArgumentException('invalid definition file type');
}
Assert::that($type)->inArray($this->allowedTypes, 'invalid definition file type');
return $this->definitionFiles[$type];
}
public function getYamlDefinitions()
public function getYamlDefinitions(): array
{
return $this->getDefinitions(self::TYPE_YAML);
}
/**
* @param string $definitionFile
* @return bool
* @return array[]
*/
public function has($definitionFile): bool
{
return isset($this->definitionFiles[md5($definitionFile)]);
}
public function getAll()
public function getAll(): array
{
return $this->definitionFiles;
}
public function clear()
public function clear(): void
{
$this->definitionFiles = [];
}
}
}

14
phpstan.neon Normal file
View File

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

55
rector.php Normal file
View File

@ -0,0 +1,55 @@
<?php
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([
__DIR__ . '/.',
]);
$rectorConfig->bootstrapFiles([
__DIR__.'/../../oxid-esales/oxideshop-ce/source/oxfunctions.php',
__DIR__.'/../../oxid-esales/oxideshop-ce/source/overridablefunctions.php',
]);
$rectorConfig->skip(
[
MixedTypeRector::class, // shouldn't remove argument annotations
]
);
// define sets of rules
$rectorConfig->sets([
LevelSetList::UP_TO_PHP_80, // lowest possible PHP version for this plugin is 8.0
SetList::TYPE_DECLARATION,
SetList::INSTANCEOF,
SetList::EARLY_RETURN,
SetList::DEAD_CODE,
SetList::CODE_QUALITY,
SetList::CODING_STYLE,
]);
$rectorConfig->importNames();
};

1
tests/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
.phpunit.result.cache

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);

28
tests/phpunit.xml Normal file
View File

@ -0,0 +1,28 @@
<?xml version="1.0"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
backupGlobals="true"
backupStaticAttributes="false"
beStrictAboutTestsThatDoNotTestAnything="false"
colors="false"
convertErrorsToExceptions="true"
convertNoticesToExceptions="false"
convertWarningsToExceptions="true"
forceCoversAnnotation="false"
processIsolation="false"
stopOnError="false"
stopOnFailure="false"
stopOnIncomplete="false"
stopOnSkipped="false"
verbose="false"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage includeUncoveredFiles="true" processUncoveredFiles="true">
<include>
<directory suffix=".php">../</directory>
</include>
</coverage>
<testsuites>
<testsuite name="Unit">
<directory>unit/</directory>
</testsuite>
</testsuites>
</phpunit>

View File

@ -0,0 +1,38 @@
<?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\autoload;
use D3\TestingTools\Development\CanAccessRestricted;
use Exception;
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
class functions_oxDICTest extends TestCase
{
use CanAccessRestricted;
/**
* @test
* @throws Exception
*/
public function d3GetOxidDICTest(): void
{
$this->assertInstanceOf(
ContainerBuilder::class,
d3GetOxidDIC()
);
}
}

View File

@ -0,0 +1,344 @@
<?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\d3DicHandler;
use D3\TestingTools\Development\CanAccessRestricted;
use d3DIContainerCache;
use Generator;
use OxidEsales\Eshop\Core\Config;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use ReflectionException;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
define('D3_MODCFG_TEST', true);
class d3DicHandlerTest extends TestCase
{
use CanAccessRestricted;
/**
* @test
* @throws ReflectionException
* @covers \D3\DIContainerHandler\d3DicHandler::getInstance
*/
public function getInstanceTest(): void
{
$sut = new d3DicHandler();
$this->setValue(
$sut,
'_instance',
null
);
$containerBuilder = $this->callMethod(
$sut,
'getInstance'
);
$this->assertInstanceOf(
ContainerBuilder::class,
$containerBuilder
);
$this->assertSame(
$containerBuilder,
$this->callMethod(
$sut,
'getInstance'
)
);
$this->assertTrue($containerBuilder->isCompiled());
}
/**
* @test
* @throws ReflectionException
* @covers \D3\DIContainerHandler\d3DicHandler::getUncompiledInstance
*/
public function getUncompiledInstanceTest(): void
{
$sut = new d3DicHandler();
$containerBuilder = $this->callMethod(
$sut,
'getUncompiledInstance'
);
$this->assertInstanceOf(
ContainerBuilder::class,
$containerBuilder
);
$this->assertFalse($containerBuilder->isCompiled());
}
/**
* @test
* @throws ReflectionException
* @covers \D3\DIContainerHandler\d3DicHandler::removeInstance
*/
public function removeInstanceTest(): void
{
$sut = new d3DicHandler();
$containerBuilder = $this->callMethod(
$sut,
'getInstance'
);
$this->callMethod(
$sut,
'removeInstance'
);
$this->assertNotSame(
$containerBuilder,
$this->callMethod(
$sut,
'getInstance'
)
);
}
/**
* @test
* @throws ReflectionException
* @covers \D3\DIContainerHandler\d3DicHandler::d3GetConfig
*/
public function d3GetConfigTest(): void
{
$sut = new d3DicHandler();
$this->assertInstanceOf(
Config::class,
$this->callMethod(
$sut,
'd3GetConfig'
)
);
}
/**
* @test
* @throws ReflectionException
* @covers \D3\DIContainerHandler\d3DicHandler::d3GetCacheFilePath
*/
public function d3GetCacheFilePathTest(): void
{
$sut = new d3DicHandler();
$this->assertMatchesRegularExpression(
'/.*?\/tmp\/.*?DicContainer_\d+\.php$/m',
(string) $this->callMethod(
$sut,
'd3GetCacheFilePath'
)
);
}
/**
* @test
* @throws ReflectionException
* @covers \D3\DIContainerHandler\d3DicHandler::d3GetCacheContainer
*/
public function d3GetCacheContainerTest(): void
{
$sut = new d3DicHandler();
$this->callMethod($sut, 'buildContainer');
$this->assertInstanceOf(
d3DIContainerCache::class,
$this->callMethod(
$sut,
'd3GetCacheContainer'
)
);
}
/**
* @test
* @throws ReflectionException
* @covers \D3\DIContainerHandler\d3DicHandler::d3GetFileLoader
*/
public function d3GetFileLoaderTest(): void
{
$sut = new d3DicHandler();
$containerBuilderMock = $this->getMockBuilder(ContainerBuilder::class)
->getMock();
$this->assertInstanceOf(
YamlFileLoader::class,
$this->callMethod(
$sut,
'd3GetFileLoader',
[$containerBuilderMock]
)
);
}
/**
* @test
* @throws ReflectionException
* @covers \D3\DIContainerHandler\d3DicHandler::loadFiles
*/
public function loadFilesTest(): void
{
/** @var ContainerBuilder|MockObject $containerBuilderMock */
$containerBuilderMock = $this->getMockBuilder(ContainerBuilder::class)
->getMock();
$fileLoaderMock = $this->getMockBuilder(YamlFileLoader::class)
->disableOriginalConstructor()
->onlyMethods(['load'])
->getMock();
$fileLoaderMock->expects($this->atLeastOnce())->method('load');
$sut = $this->getMockBuilder(d3DicHandler::class)
->onlyMethods(['d3GetFileLoader'])
->getMock();
$sut->method('d3GetFileLoader')->willReturn($fileLoaderMock);
$this->callMethod(
$sut,
'loadFiles',
[$containerBuilderMock]
);
}
/**
* @test
* @throws ReflectionException
* @covers \D3\DIContainerHandler\d3DicHandler::isNotInTest
*/
public function isNotInTest(): void
{
$sut = new d3DicHandler();
$this->assertTrue(
$this->callMethod(
$sut,
'isNotInTest'
)
);
}
/**
* @test
* @throws ReflectionException
* @dataProvider cacheFileExistsTestDataProvider
* @covers \D3\DIContainerHandler\d3DicHandler::cacheFileExists
*/
public function cacheFileExistsTest(bool $cacheExist): void
{
if (!$cacheExist) {
$sut = $this->getMockBuilder(d3DicHandler::class)
->onlyMethods(['d3GetCacheFilePath'])
->getMock();
$sut->method('d3GetCacheFilePath')->willReturn('foo');
} else {
$sut = new d3DicHandler();
}
$this->assertSame(
$cacheExist,
$this->callMethod(
$sut,
'cacheFileExists'
)
);
}
public function cacheFileExistsTestDataProvider(): Generator
{
yield 'cacheExist' => [true];
yield 'cacheMissing'=> [false];
}
/**
* @test
*
*
* @throws ReflectionException
* @dataProvider buildContainerTestDataProvider
* @covers \D3\DIContainerHandler\d3DicHandler::buildContainer
*/
public function buildContainerTest(bool $productive, int $debug, bool $notInTest, bool $cacheFileExist, bool $cachedContainer): void
{
$cachedContainerMock = $this->getMockBuilder(d3DIContainerCache::class)
->getMock();
$containerBuilderMock = $this->getMockBuilder(ContainerBuilder::class)->onlyMethods([ 'compile' ])->getMock();
$containerBuilderMock->expects($this->exactly((int) ! $cachedContainer))->method('compile');
$configMock = $this->getMockBuilder(Config::class)
->onlyMethods(['isProductiveMode', 'getConfigParam'])
->getMock();
$configMock->method('isProductiveMode')->willReturn($productive);
$configMock->method('getConfigParam')->willReturnMap([['iDebug', $debug]]);
$sut = $this->getMockBuilder(d3DicHandler::class)
->onlyMethods(['d3GetConfig', 'd3GetCacheContainer', 'getContainerBuilder', 'isNotInTest', 'cacheFileExists'])
->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);
$this->assertSame(
$cachedContainer ? $cachedContainerMock : $containerBuilderMock,
$this->callMethod(
$sut,
'buildContainer',
['false']
)
);
}
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];
}
/**
* @test
* @throws ReflectionException
* @covers \D3\DIContainerHandler\d3DicHandler::getContainerBuilder
*/
public function getContainerBuilderTest(): void
{
$sut = new d3DicHandler();
$this->assertInstanceOf(
ContainerBuilder::class,
$this->callMethod(
$sut,
'getContainerBuilder'
)
);
}
}

View File

@ -0,0 +1,101 @@
<?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\d3DicHandler;
use D3\DIContainerHandler\d3DicUtilities;
use D3\TestingTools\Development\CanAccessRestricted;
use Generator;
use PHPUnit\Framework\TestCase;
use ReflectionException;
class d3DicUtilitiesTest extends TestCase
{
use CanAccessRestricted;
/**
* @test
*
* @param string|null $additional
*
* @throws ReflectionException
* @covers \D3\DIContainerHandler\d3DicUtilities::getServiceId
* @dataProvider getServiceIdTestDataProvider
*/
public function getServiceIdTest(string $className, string $additional = null, string $expected): void
{
$sut = oxNew(d3DicUtilities::class);
$this->assertSame(
$expected,
$this->callMethod(
$sut,
'getServiceId',
[$className, $additional]
)
);
}
public function getServiceIdTestDataProvider(): Generator
{
yield 'NS only' => [d3DicHandler::class, null, 'd3\dicontainerhandler\d3dichandler'];
yield 'NS + additional' => [d3DicHandler::class, 'additional', 'additional.d3\dicontainerhandler\d3dichandler'];
}
/**
* @test
*
*
* @throws ReflectionException
* @covers \D3\DIContainerHandler\d3DicUtilities::getArgumentId
* @dataProvider getArgumentIdTestDataProvider
*/
public function getArgumentIdTest(string $className, string $argumentName, string $expected): void
{
$sut = oxNew(d3DicUtilities::class);
$this->assertSame(
$expected,
$this->callMethod(
$sut,
'getArgumentId',
[ $className, $argumentName]
)
);
}
public function getArgumentIdTestDataProvider(): Generator
{
yield 'default' => [d3DicHandler::class, 'argumentName', 'd3\dicontainerhandler\d3dichandler.args.argumentname'];
}
/**
* @test
* @throws ReflectionException
* @covers \D3\DIContainerHandler\d3DicUtilities::getVendorDir()
*/
public function getVendorDirTest(): void
{
$sut = oxNew(d3DicUtilities::class);
$this->assertDirectoryExists(
(string) $this->callMethod(
$sut,
'getVendorDir'
)
);
}
}

View File

@ -0,0 +1,179 @@
<?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 Assert\InvalidArgumentException;
use D3\DIContainerHandler\definitionFileContainer;
use D3\TestingTools\Development\CanAccessRestricted;
use Generator;
use PHPUnit\Framework\TestCase;
use ReflectionException;
class definitionFileContainerTest extends TestCase
{
use CanAccessRestricted;
/**
* @test
*
*
* @throws ReflectionException
* @dataProvider addDefinitionsTestDataProvider
* @covers \D3\DIContainerHandler\definitionFileContainer::addDefinitions
*/
public function addDefinitionsTest(string $file, string $type, int $sumand, bool $expectException): void
{
$sut = oxNew(definitionFileContainer::class);
$sut->clear();
$currentCount = count($sut->getAll());
if ($expectException) {
$this->expectException(InvalidArgumentException::class);
}
$this->callMethod(
$sut,
'addDefinitions',
[$file, $type]
);
$this->assertCount(
$currentCount + $sumand,
$sut->getAll()
);
}
public function addDefinitionsTestDataProvider(): Generator
{
yield 'invalid file' => ['foo.txt', definitionFileContainer::TYPE_YAML, 0, true];
yield 'invalid type' => ['d3/modcfg/Config/services.yaml', 'txt', 0, true];
yield 'ok' => ['d3/modcfg/Config/services.yaml', definitionFileContainer::TYPE_YAML, 1, false];
}
/**
* @test
* @throws ReflectionException
* @covers \D3\DIContainerHandler\definitionFileContainer::addYamlDefinitions
*/
public function addYamlDefinitionsTest(): void
{
$sut = $this->getMockBuilder(definitionFileContainer::class)
->onlyMethods(['addDefinitions'])
->getMock();
$sut->expects($this->once())->method('addDefinitions');
$this->callMethod(
$sut,
'addYamlDefinitions',
['d3/modcfg/Config/services.yaml']
);
}
/**
* @test
*
*
* @throws ReflectionException
* @covers \D3\DIContainerHandler\definitionFileContainer::getDefinitions
* @dataProvider getDefinitionsTestDataProvider
*/
public function getDefinitionsTest(string $type, bool $expectException): void
{
$sut = oxNew(definitionFileContainer::class);
if ($expectException) {
$this->expectException(InvalidArgumentException::class);
}
$definitions = $this->callMethod(
$sut,
'getDefinitions',
[$type]
);
$this->assertIsArray($definitions);
}
public function getDefinitionsTestDataProvider(): Generator
{
yield 'type ok' => [definitionFileContainer::TYPE_YAML, false];
yield 'type not ok' => ['txt', true];
}
/**
* @test
* @throws ReflectionException
* @covers \D3\DIContainerHandler\definitionFileContainer::getYamlDefinitions
*/
public function getYamlDefinitionsTest(): void
{
$sut = $this->getMockBuilder(definitionFileContainer::class)
->onlyMethods(['getDefinitions'])
->getMock();
$sut->expects($this->once())->method('getDefinitions');
$this->callMethod(
$sut,
'getYamlDefinitions'
);
}
/**
* @test
* @throws ReflectionException
* @covers \D3\DIContainerHandler\definitionFileContainer::getAll
*/
public function getAllTest(): void
{
$fixture = ['foo'];
$sut = oxNew(definitionFileContainer::class);
$this->setValue(
$sut,
'definitionFiles',
$fixture
);
$this->assertSame(
$fixture,
$this->callMethod(
$sut,
'getAll'
)
);
}
/**
* @test
* @throws ReflectionException
* @covers \D3\DIContainerHandler\definitionFileContainer::clear
*/
public function clearTest(): void
{
$sut = oxNew(definitionFileContainer::class);
$sut->addYamlDefinitions('d3/modcfg/Config/services.yaml');
$sut->clear();
$this->assertCount(
0,
(array) $this->callMethod(
$sut,
'getAll'
)
);
}
}