8
0
DIContainer/tests/unit/definitionFileContainerTest.php

182 Zeilen
4.8 KiB
PHP

2024-01-31 19:24:51 +01:00
<?php
/**
2024-06-15 23:10:49 +02:00
* Copyright (c) D3 Data Development (Inh. Thomas Dartsch)
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
2024-01-31 19:24:51 +01:00
*
* https://www.d3data.de
*
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
2024-06-15 23:10:49 +02:00
* @author D3 Data Development - Daniel Seifert <info@shopmodule.com>
2024-01-31 19:24:51 +01:00
* @link https://www.oxidmodule.com
*/
declare(strict_types=1);
2024-06-16 00:01:36 +02:00
namespace D3\DIContainerHandler\tests\unit;
2024-01-31 19:24:51 +01:00
use Assert\InvalidArgumentException;
use D3\DIContainerHandler\definitionFileContainer;
2024-01-31 21:13:53 +01:00
use D3\TestingTools\Development\CanAccessRestricted;
2024-01-31 19:24:51 +01:00
use Generator;
use PHPUnit\Framework\TestCase;
use ReflectionException;
class definitionFileContainerTest extends TestCase
{
2024-01-31 21:13:53 +01:00
use CanAccessRestricted;
2024-01-31 19:24:51 +01:00
/**
* @test
*
* @throws ReflectionException
* @dataProvider addDefinitionsTestDataProvider
* @covers \D3\DIContainerHandler\definitionFileContainer::addDefinitions
2024-05-27 13:41:52 +02:00
* @covers \D3\DIContainerHandler\definitionFileContainer::__construct
2024-01-31 19:24:51 +01:00
*/
2024-01-31 21:56:19 +01:00
public function addDefinitionsTest(string $file, string $type, int $sumand, bool $expectException): void
2024-01-31 19:24:51 +01:00
{
$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
*/
2024-01-31 22:09:36 +01:00
public function clearTest(): void
2024-01-31 19:24:51 +01:00
{
$sut = oxNew(definitionFileContainer::class);
$sut->addYamlDefinitions('d3/modcfg/Config/services.yaml');
$sut->clear();
$this->assertCount(
0,
2024-01-31 21:56:19 +01:00
(array) $this->callMethod(
2024-01-31 19:24:51 +01:00
$sut,
'getAll'
)
);
}
2024-01-31 21:13:53 +01:00
}