Eyrie-Templates/tests/Loader/FileLoaderTest.php
2026-01-06 17:29:10 -06:00

86 lines
2.5 KiB
PHP

<?php
declare(strict_types=1);
namespace Eyrie\Tests\Loader;
use Eyrie\Loader\FileLoader;
use PHPUnit\Framework\TestCase;
class FileLoaderTest extends TestCase
{
private string $tempDir;
protected function setUp(): void
{
$this->tempDir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'eyrie_tests_' . uniqid();
mkdir($this->tempDir);
}
protected function tearDown(): void
{
$this->removeDirectory($this->tempDir);
}
public function testLoadExistingTemplate(): void
{
$templatePath = $this->tempDir . DIRECTORY_SEPARATOR . 'test.eyrie.php';
file_put_contents($templatePath, 'Hello World');
$loader = new FileLoader([$this->tempDir]);
$this->assertEquals('Hello World', $loader->load('test'));
}
public function testLoadDotNotationTemplate(): void
{
$subDir = $this->tempDir . DIRECTORY_SEPARATOR . 'layouts';
mkdir($subDir);
$templatePath = $subDir . DIRECTORY_SEPARATOR . 'base.eyrie.php';
file_put_contents($templatePath, 'Layout Content');
$loader = new FileLoader([$this->tempDir]);
$this->assertEquals('Layout Content', $loader->load('layouts.base'));
}
public function testLoadNonExistentTemplateThrowsException(): void
{
$loader = new FileLoader([$this->tempDir]);
$this->expectException(\RuntimeException::class);
$loader->load('non_existent');
}
public function testExists(): void
{
$templatePath = $this->tempDir . DIRECTORY_SEPARATOR . 'test.eyrie.php';
file_put_contents($templatePath, 'Hello World');
$loader = new FileLoader([$this->tempDir]);
$this->assertTrue($loader->exists('test'));
$this->assertFalse($loader->exists('non_existent'));
}
public function testIsFresh(): void
{
$templatePath = $this->tempDir . DIRECTORY_SEPARATOR . 'test.eyrie.php';
file_put_contents($templatePath, 'Hello World');
$time = time();
$loader = new FileLoader([$this->tempDir]);
$this->assertTrue($loader->isFresh('test', $time + 10));
$this->assertFalse($loader->isFresh('test', $time - 10));
}
private function removeDirectory(string $dir): void
{
if (!is_dir($dir)) {
return;
}
$files = array_diff(scandir($dir), ['.', '..']);
foreach ($files as $file) {
(is_dir("$dir/$file")) ? $this->removeDirectory("$dir/$file") : unlink("$dir/$file");
}
rmdir($dir);
}
}