complete tests from mutation

This commit is contained in:
Daniel Seifert 2024-06-16 01:28:42 +02:00
parent 90b8a71e12
commit 7fc781af80
Signed by: DanielS
GPG Key ID: 6A513E13AEE66170
4 changed files with 60 additions and 24 deletions

View File

@ -40,8 +40,8 @@ class d3DicUtilities
); );
} }
public static function getVendorDir(): string public static function getVendorDir($current = __FILE__): string
{ {
return rtrim(dirname(__FILE__, 3), '/') . '/'; return dirname($current, 3). '/';
} }
} }

View File

@ -39,7 +39,7 @@ class definitionFileContainer
public function addDefinitions(string $definitionFile, string $type): void public function addDefinitions(string $definitionFile, string $type): void
{ {
Assert::that($type)->inArray($this->allowedTypes, 'invalid definition file type'); Assert::that($type)->inArray($this->allowedTypes, 'invalid definition file type');
Assert::that(rtrim(dirname(__FILE__, 3).'/').$definitionFile)->file('invalid definition file'); Assert::that(dirname(__FILE__, 3).'/'.$definitionFile)->file('invalid definition file');
$this->definitionFiles[$type][md5($definitionFile)] = $definitionFile; $this->definitionFiles[$type][md5($definitionFile)] = $definitionFile;
} }

View File

@ -132,16 +132,16 @@ class d3DicHandlerTest extends TestCase
/** /**
* @test * @test
* @param bool $throwException * @param bool $throwException
* @param bool $expectException * @param bool $expectException
* @param string $circularReferenceMethod * @param string $circularReferenceMethod
* * @param string $expectedExceptionMessage
* @return void * @return void
* @throws ReflectionException * @throws ReflectionException
* @covers \D3\DIContainerHandler\d3DicHandler::createInstance * @covers \D3\DIContainerHandler\d3DicHandler::createInstance
* @dataProvider canCreateInstanceDataProvider * @dataProvider canCreateInstanceDataProvider
*/ */
public function canCreateInstance(bool $throwException, bool $expectException, string $circularReferenceMethod = '') public function canCreateInstance(bool $throwException, bool $expectException, string $circularReferenceMethod = '', string $expectedExceptionMessage = ''): void
{ {
/** @var d3DicHandler|MockObject $sut */ /** @var d3DicHandler|MockObject $sut */
$sut = $this->getMockBuilder(d3DicHandler::class) $sut = $this->getMockBuilder(d3DicHandler::class)
@ -154,6 +154,7 @@ class d3DicHandlerTest extends TestCase
$sut->method('getFunctionNameFromTrace')->willReturn($circularReferenceMethod); $sut->method('getFunctionNameFromTrace')->willReturn($circularReferenceMethod);
if ($expectException) { if ($expectException) {
$this->expectException(d3DicException::class); $this->expectException(d3DicException::class);
$this->expectExceptionMessage($expectedExceptionMessage);
} }
$this->callMethod( $this->callMethod(
@ -165,8 +166,8 @@ class d3DicHandlerTest extends TestCase
public function canCreateInstanceDataProvider(): Generator public function canCreateInstanceDataProvider(): Generator
{ {
yield "don't throw exception" => [false, false]; yield "don't throw exception" => [false, false];
yield "throw exception" => [true, true]; yield "throw exception" => [true, true, '', 'fixture'];
yield "has circular reference method name" => [false, true, 'getViewConfig']; yield "has circular reference method name" => [false, true, 'getViewConfig', 'method getViewConfig can\'t use DIC due the danger of circular reference'];
} }
/** /**
@ -334,13 +335,13 @@ class d3DicHandlerTest extends TestCase
* @test * @test
* @param bool $useCacheContainer * @param bool $useCacheContainer
* @param bool $compile * @param bool $compile
* * @param bool $useDefault
* @return void * @return void
* @throws ReflectionException * @throws ReflectionException
* @dataProvider buildContainerTestDataProvider * @dataProvider buildContainerTestDataProvider
* @covers \D3\DIContainerHandler\d3DicHandler::buildContainer * @covers \D3\DIContainerHandler\d3DicHandler::buildContainer
*/ */
public function buildContainerTest(bool $useCacheContainer, bool $compile): void public function buildContainerTest(bool $useCacheContainer, bool $compile, bool $useDefault = false): void
{ {
$structure = [ $structure = [
'source_directory' => [], 'source_directory' => [],
@ -356,26 +357,46 @@ class d3DicHandlerTest extends TestCase
->disableOriginalConstructor() ->disableOriginalConstructor()
->onlyMethods(get_class_methods(PhpDumper::class)) ->onlyMethods(get_class_methods(PhpDumper::class))
->getMock(); ->getMock();
$phpDumperMock->expects($this->exactly((int) (!$useCacheContainer && $compile)))->method('dump'); $phpDumperMock->expects($this->exactly((int) (!$useCacheContainer && $compile)))->method('dump')->willReturn('fixture');
/** @var d3DicHandler|MockObject $sut */ /** @var d3DicHandler|MockObject $sut */
$sut = $this->getMockBuilder(d3DicHandler::class) $sut = $this->getMockBuilder(d3DicHandler::class)
->onlyMethods(['d3UseCachedContainer', 'd3GetCacheContainer', 'getContainerBuilder', 'd3GetCacheFilePath', 'getPhpDumper']) ->onlyMethods(
[
'd3UseCachedContainer', 'd3GetCacheContainer', 'getContainerBuilder',
'loadFiles','d3GetCacheFilePath', 'getPhpDumper'
])
->getMock(); ->getMock();
$sut->expects($this->once())->method('d3UseCachedContainer')->willReturn($useCacheContainer); $sut->expects($this->once())->method('d3UseCachedContainer')->willReturn($useCacheContainer);
$sut->expects($this->exactly((int) $useCacheContainer))->method('d3GetCacheContainer'); $sut->expects($this->exactly((int) $useCacheContainer))->method('d3GetCacheContainer');
$sut->expects($this->exactly((int) !$useCacheContainer))->method('getContainerBuilder')->willReturn($containerBuilderMock); $sut->expects($this->exactly((int) !$useCacheContainer))->method('getContainerBuilder')->willReturn($containerBuilderMock);
$sut->expects($this->exactly((int) !$useCacheContainer))->method('loadFiles');
$sut->method('d3GetCacheFilePath')->willReturn($fsRoot->getChild('source_directory')->url().'/DIContainer.php'); $sut->method('d3GetCacheFilePath')->willReturn($fsRoot->getChild('source_directory')->url().'/DIContainer.php');
$sut->method('getPhpDumper')->willReturn($phpDumperMock); $sut->method('getPhpDumper')->willReturn($phpDumperMock);
$this->assertInstanceOf( $useDefault ?
Container::class, $this->assertInstanceOf(
$this->callMethod( Container::class,
$sut, $this->callMethod(
'buildContainer', $sut,
[$compile] 'buildContainer'
) )
); ):
$this->assertInstanceOf(
Container::class,
$this->callMethod(
$sut,
'buildContainer',
[$compile]
)
);
if (!$useCacheContainer && $compile) {
$this->assertSame(
'fixture',
file_get_contents($fsRoot->getChild('source_directory')->url() . '/DIContainer.php')
);
}
} }
public function buildContainerTestDataProvider(): Generator public function buildContainerTestDataProvider(): Generator
@ -383,6 +404,7 @@ class d3DicHandlerTest extends TestCase
yield "can't use cached container, do compile" => [false, true]; yield "can't use cached container, do compile" => [false, true];
yield "can't use cached container, don't compile" => [false, false]; yield "can't use cached container, don't compile" => [false, false];
yield "use cached container" => [true, false]; yield "use cached container" => [true, false];
yield "can't use cached container, do compile, default" => [false, true, true];
} }
/** /**

View File

@ -90,16 +90,30 @@ class d3DicUtilitiesTest extends TestCase
* @test * @test
* @throws ReflectionException * @throws ReflectionException
* @covers \D3\DIContainerHandler\d3DicUtilities::getVendorDir() * @covers \D3\DIContainerHandler\d3DicUtilities::getVendorDir()
* @dataProvider getVendorDirTestDataProvider
*/ */
public function getVendorDirTest(): void public function getVendorDirTest(string $path): void
{ {
$sut = oxNew(d3DicUtilities::class); $sut = oxNew(d3DicUtilities::class);
$vendorDir = (string) $this->callMethod(
$sut,
'getVendorDir',
[$path]
);
$this->assertSame('/var/www/html/vendor/', $vendorDir);
$this->assertDirectoryExists( $this->assertDirectoryExists(
(string) $this->callMethod( $this->callMethod(
$sut, $sut,
'getVendorDir' 'getVendorDir'
) )
); );
} }
public function getVendorDirTestDataProvider(): Generator
{
yield 'default' => ['/var/www/html/vendor/d3/oxid-dic-handler/d3DicUtilities.php'];
yield 'space after' => ['/var/www/html/vendor/d3/oxid-dic-handler/d3DicUtilities.php '];
}
} }