generated from chicory/php-env
#13 Config provider test
This commit is contained in:
parent
217fd63da9
commit
a90b049523
@ -0,0 +1,70 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Tests\Functional\Infrastructure\Config;
|
||||||
|
|
||||||
|
use CuyZ\Valinor\Mapper\TreeMapper;
|
||||||
|
use CuyZ\Valinor\MapperBuilder;
|
||||||
|
use PHPUnit\Framework\Attributes\CoversClass;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use Runx\Infrastructure\Config\Config;
|
||||||
|
use Runx\Infrastructure\Config\Provider\JsonConfigProvider;
|
||||||
|
|
||||||
|
#[CoversClass(JsonConfigProvider::class)]
|
||||||
|
class JsonConfigProviderTest extends TestCase
|
||||||
|
{
|
||||||
|
private string $tmpFile;
|
||||||
|
private TreeMapper $mapper;
|
||||||
|
|
||||||
|
protected function setUp(): void
|
||||||
|
{
|
||||||
|
$this->mapper = (new MapperBuilder())->mapper();
|
||||||
|
|
||||||
|
$this->tmpFile = tempnam(sys_get_temp_dir(), 'cfg_');
|
||||||
|
$configExample = __DIR__ . '/../../../../config/config.example.json';
|
||||||
|
|
||||||
|
if (false === $this->tmpFile) {
|
||||||
|
$this->markTestSkipped('Unable to create temporary config file.');
|
||||||
|
}
|
||||||
|
|
||||||
|
copy($configExample, $this->tmpFile);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function tearDown(): void
|
||||||
|
{
|
||||||
|
if (is_file($this->tmpFile)) {
|
||||||
|
unlink($this->tmpFile);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testThrowsIfFileNotReadable(): void
|
||||||
|
{
|
||||||
|
$provider = new JsonConfigProvider('/non/existent/path.json', $this->mapper);
|
||||||
|
|
||||||
|
$this->expectException(\RuntimeException::class);
|
||||||
|
$this->expectExceptionMessage('not found');
|
||||||
|
|
||||||
|
$provider->getConfig();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testThrowsOnMappingError(): void
|
||||||
|
{
|
||||||
|
file_put_contents($this->tmpFile, '{"foo":"bar"}');
|
||||||
|
|
||||||
|
$provider = new JsonConfigProvider($this->tmpFile, $this->mapper);
|
||||||
|
|
||||||
|
$this->expectException(\RuntimeException::class);
|
||||||
|
$this->expectExceptionMessage('Failed to map');
|
||||||
|
|
||||||
|
$provider->getConfig();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testReturnsConfigOnSuccess(): void
|
||||||
|
{
|
||||||
|
$provider = new JsonConfigProvider($this->tmpFile, $this->mapper);
|
||||||
|
$config = $provider->getConfig();
|
||||||
|
|
||||||
|
self::assertInstanceOf(Config::class, $config);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user