make better testable

This commit is contained in:
Daniel Seifert 2024-05-27 13:41:52 +02:00
parent 3368873774
commit 31c3ac4e50
9 changed files with 256 additions and 89 deletions

View File

@ -33,7 +33,9 @@ function d3GetOxidDIC(): Container
{ {
try { try {
return d3GetOxidDIC_withExceptions(); return d3GetOxidDIC_withExceptions();
// @codeCoverageIgnoreStart
} catch (d3DicException $exception) { } catch (d3DicException $exception) {
trigger_error($exception->getMessage(), E_USER_ERROR); trigger_error($exception->getMessage(), E_USER_ERROR);
} }
// @codeCoverageIgnoreEnd
} }

View File

@ -30,7 +30,8 @@
"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", "phpstan/phpstan": "^1.10",
"rector/rector": "^0.18.13" "rector/rector": "^0.18.13",
"mikey179/vfsstream": "^1.6.8"
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {

View File

@ -40,24 +40,7 @@ class d3DicHandler implements d3DicHandlerInterface
*/ */
public static function getInstance(): Container public static function getInstance(): Container
{ {
try { return oxNew(d3DicHandler::class)->createInstance();
$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();
}
} catch (Exception $exception) {
throw new d3DicException($exception);
}
return self::$_instance;
} }
/** /**
@ -66,17 +49,25 @@ class d3DicHandler implements d3DicHandlerInterface
*/ */
public static function getUncompiledInstance(): Container public static function getUncompiledInstance(): Container
{ {
try { return oxNew(d3DicHandler::class)->createInstance(false);
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); }
$caller = $trace[1];
$functionName = $caller['function'];
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))) { 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"); throw oxNew(Exception::class, 'method ' . $functionName . " can't use DIC due the danger of circular reference");
} }
$oDicHandler = oxNew(d3DicHandler::class); if (null == self::$_instance) {
self::$_instance = $oDicHandler->buildContainer(false); self::$_instance = $this->buildContainer($compiled);
}
} catch (Exception $exception) { } catch (Exception $exception) {
throw new d3DicException($exception); throw new d3DicException($exception);
} }
@ -84,9 +75,11 @@ class d3DicHandler implements d3DicHandlerInterface
return self::$_instance; return self::$_instance;
} }
public static function removeInstance(): void protected function getFunctionNameFromTrace()
{ {
self::$_instance = null; $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
$caller = $trace[1];
return $caller['function'];
} }
public function d3GetConfig(): Config public function d3GetConfig(): Config
@ -135,11 +128,6 @@ class d3DicHandler implements d3DicHandlerInterface
} }
} }
protected function isNotInTest(): bool
{
return false == defined('OXID_PHP_UNIT') || true == defined('D3_MODCFG_TEST');
}
protected function cacheFileExists(): bool protected function cacheFileExists(): bool
{ {
return file_exists($this->d3GetCacheFilePath()); return file_exists($this->d3GetCacheFilePath());
@ -162,13 +150,10 @@ class d3DicHandler implements d3DicHandlerInterface
if ($compileAndDump) { if ($compileAndDump) {
$container->compile(); $container->compile();
$dumper = $this->getPhpDumper($container);
if ($this->isNotInTest()) {
$dumper = new PhpDumper($container);
file_put_contents($this->d3GetCacheFilePath(), $dumper->dump(['class' => 'd3DIContainerCache'])); file_put_contents($this->d3GetCacheFilePath(), $dumper->dump(['class' => 'd3DIContainerCache']));
} }
} }
}
stopProfile(__METHOD__); stopProfile(__METHOD__);
@ -181,7 +166,6 @@ class d3DicHandler implements d3DicHandlerInterface
return $config->isProductiveMode() return $config->isProductiveMode()
&& !$config->getConfigParam('iDebug') && !$config->getConfigParam('iDebug')
&& $this->isNotInTest()
&& $this->cacheFileExists(); && $this->cacheFileExists();
} }
@ -190,13 +174,8 @@ class d3DicHandler implements d3DicHandlerInterface
return oxNew(ContainerBuilder::class); return oxNew(ContainerBuilder::class);
} }
public function __clone() public function getPhpDumper(ContainerBuilder $containerBuilder): PhpDumper
{ {
/** keep clear */ return new PhpDumper( $containerBuilder);
}
public function __construct()
{
/** keep clear */
} }
} }

View File

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

View File

@ -28,6 +28,7 @@ class functions_oxDICTest extends TestCase
/** /**
* @test * @test
* @throws Exception * @throws Exception
* @covers ::d3GetOxidDIC_withExceptions()
*/ */
public function d3GetOxidDIC_withExceptionsTest(): void public function d3GetOxidDIC_withExceptionsTest(): void
{ {
@ -42,6 +43,7 @@ class functions_oxDICTest extends TestCase
/** /**
* @test * @test
* @throws Exception * @throws Exception
* @covers ::d3GetOxidDIC()
*/ */
public function d3GetOxidDICTest(): void public function d3GetOxidDICTest(): void
{ {

View 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()
);
}
}

View File

@ -15,23 +15,33 @@ declare(strict_types=1);
namespace D3\DIContainerHandler\tests; namespace D3\DIContainerHandler\tests;
use D3\DIContainerHandler\d3DicException;
use D3\DIContainerHandler\d3DicHandler; use D3\DIContainerHandler\d3DicHandler;
use D3\TestingTools\Development\CanAccessRestricted; use D3\TestingTools\Development\CanAccessRestricted;
use d3DIContainerCache; use d3DIContainerCache;
use Exception;
use Generator; use Generator;
use org\bovigo\vfs\vfsStream;
use OxidEsales\Eshop\Core\Config; use OxidEsales\Eshop\Core\Config;
use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use ReflectionException; use ReflectionException;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
define('D3_MODCFG_TEST', true);
class d3DicHandlerTest extends TestCase class d3DicHandlerTest extends TestCase
{ {
use CanAccessRestricted; use CanAccessRestricted;
public function setUp(): void
{
parent::setUp();
d3DicHandler::removeInstance();
}
/** /**
* @test * @test
* @throws ReflectionException * @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 * @test
* @throws ReflectionException * @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 * @test
* @throws ReflectionException * @throws ReflectionException
@ -276,52 +328,102 @@ class d3DicHandlerTest extends TestCase
/** /**
* @test * @test
* @param bool $useCacheContainer
* @param bool $compile
* *
* * @return void
* @throws ReflectionException * @throws ReflectionException
* @dataProvider buildContainerTestDataProvider * @dataProvider buildContainerTestDataProvider
* @covers \D3\DIContainerHandler\d3DicHandler::buildContainer * @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) $structure = [
->getMock(); 'source_directory' => [],
];
vfsStream::setup();
$fsRoot = vfsStream::create($structure);
$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) (!$useCacheContainer && $compile)))->method('compile');
$configMock = $this->getMockBuilder(Config::class) /** @var PhpDumper|MockObject $phpDumperMock */
->onlyMethods(['isProductiveMode', 'getConfigParam']) $phpDumperMock = $this->getMockBuilder(PhpDumper::class)
->disableOriginalConstructor()
->onlyMethods(get_class_methods(PhpDumper::class))
->getMock(); ->getMock();
$configMock->method('isProductiveMode')->willReturn($productive); $phpDumperMock->expects($this->exactly((int) (!$useCacheContainer && $compile)))->method('dump');
$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(['d3UseCachedContainer', 'd3GetCacheContainer', 'getContainerBuilder', 'd3GetCacheFilePath', 'getPhpDumper'])
->getMock(); ->getMock();
$sut->method('d3GetConfig')->willReturn($configMock); $sut->expects($this->once())->method('d3UseCachedContainer')->willReturn($useCacheContainer);
$sut->expects($this->exactly((int) $cachedContainer))->method('d3GetCacheContainer')->willReturn($cachedContainerMock); $sut->expects($this->exactly((int) $useCacheContainer))->method('d3GetCacheContainer');
$sut->expects($this->exactly((int) !$cachedContainer))->method('getContainerBuilder')->willReturn($containerBuilderMock); $sut->expects($this->exactly((int) !$useCacheContainer))->method('getContainerBuilder')->willReturn($containerBuilderMock);
$sut->method('isNotInTest')->willReturn($notInTest); $sut->method('d3GetCacheFilePath')->willReturn($fsRoot->getChild('source_directory')->path().'/DIContainer.php');
$sut->method('cacheFileExists')->willReturn($cacheFileExist); $sut->method('getPhpDumper')->willReturn($phpDumperMock);
$this->assertSame( $this->assertInstanceOf(
$cachedContainer ? $cachedContainerMock : $containerBuilderMock, Container::class,
$this->callMethod( $this->callMethod(
$sut, $sut,
'buildContainer', 'buildContainer',
['false'] [$compile]
) )
); );
} }
public function buildContainerTestDataProvider(): Generator public function buildContainerTestDataProvider(): Generator
{ {
yield 'notProductive' => [false, 0, false, true, false]; yield "can't use cached container, do compile" => [false, true];
yield 'debug' => [true, 1, false, true, false]; yield "can't use cached container, don't compile" => [false, false];
yield 'inTest' => [true, 0, false, true, false]; yield "use cached container" => [true, false];
yield 'cacheFileNotExist' => [true, 0, false, false, false]; }
yield 'cachedContainer' => [true, 0, true, true, true];
/**
* @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];
} }
/** /**
@ -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]
)
);
}
} }

View File

@ -29,7 +29,9 @@ class d3DicUtilitiesTest extends TestCase
/** /**
* @test * @test
* *
* @param string $className
* @param string|null $additional * @param string|null $additional
* @param string $expected
* *
* @throws ReflectionException * @throws ReflectionException
* @covers \D3\DIContainerHandler\d3DicUtilities::getServiceId * @covers \D3\DIContainerHandler\d3DicUtilities::getServiceId

View File

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