Files
netslim-core/tests/Kernel/RuntimePathsTest.php

88 lines
2.8 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Kernel;
use Netig\Netslim\Kernel\Runtime\RuntimePaths;
use PHPUnit\Framework\TestCase;
use Tests\Support\TestRuntimeFactory;
#[\PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations]
final class RuntimePathsTest extends TestCase
{
protected function tearDown(): void
{
TestRuntimeFactory::resetRuntime();
}
public function testGetProjectRootReturnsIsolatedTestProjectRoot(): void
{
self::assertSame(TestRuntimeFactory::projectRoot(), RuntimePaths::getProjectRoot());
}
public function testGetConfigPathReturnsRootConfigDirectoryAndFilePath(): void
{
self::assertSame(TestRuntimeFactory::path('config'), RuntimePaths::getConfigPath());
self::assertSame(TestRuntimeFactory::path('config/modules.php'), RuntimePaths::getConfigPath('modules.php'));
}
public function testApplicationRootDefaultsToProjectRoot(): void
{
self::assertSame(TestRuntimeFactory::applicationRoot(), RuntimePaths::getApplicationRoot());
self::assertSame(TestRuntimeFactory::path('config'), RuntimePaths::getApplicationConfigPath());
}
public function testApplicationRootCanPointToTheIsolatedFixtureApplication(): void
{
RuntimePaths::setApplicationRoot(TestRuntimeFactory::applicationRoot());
self::assertSame(TestRuntimeFactory::applicationRoot(), RuntimePaths::getApplicationRoot());
self::assertSame(TestRuntimeFactory::path('config/modules.php'), RuntimePaths::getApplicationConfigPath('modules.php'));
self::assertSame(TestRuntimeFactory::path('templates/Kernel'), RuntimePaths::getApplicationPath('templates/Kernel'));
}
public function testGetTwigCacheReturnsFalseInDev(): void
{
self::assertFalse(RuntimePaths::getTwigCache(true));
}
public function testGetTwigCacheReturnsCachePathOutsideDev(): void
{
$cachePath = RuntimePaths::getTwigCache(false);
self::assertIsString($cachePath);
self::assertStringEndsWith('/var/cache/twig', $cachePath);
}
public function testGetDatabasePathCreatesDatabaseFileWhenMissing(): void
{
$dbFile = TestRuntimeFactory::path('database/app.sqlite');
$dbDir = dirname($dbFile);
$backup = $dbFile . '.bak-test';
if (file_exists($backup)) {
@unlink($backup);
}
if (file_exists($dbFile)) {
rename($dbFile, $backup);
}
@unlink($dbFile);
try {
$path = RuntimePaths::getDatabasePath();
self::assertSame($dbFile, $path);
self::assertDirectoryExists($dbDir);
self::assertFileExists($dbFile);
} finally {
@unlink($dbFile);
if (file_exists($backup)) {
rename($backup, $dbFile);
}
}
}
}