runx/tests/Functional/Infrastructure/Config/JsonConfigProviderTest.php
Chicory a90b049523
All checks were successful
Build and test. / build (push) Successful in 19s
Build and test. / test (push) Successful in 17s
Build and test. / cleanup (push) Successful in 6s
#13 Config provider test
2025-09-19 10:50:10 +03:00

71 lines
1.9 KiB
PHP

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