Eyrie-Templates/tests/EngineTest.php

213 lines
7.5 KiB
PHP
Raw Permalink Normal View History

2026-01-06 23:29:10 +00:00
<?php
declare(strict_types=1);
namespace Eyrie\Tests;
use Eyrie\Engine;
use Eyrie\Loader\FileLoader;
use PHPUnit\Framework\TestCase;
class EngineTest extends TestCase
{
private string $tempDir;
private string $cacheDir;
protected function setUp(): void
{
$this->tempDir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'eyrie_tests_' . uniqid();
mkdir($this->tempDir);
$this->cacheDir = $this->tempDir . DIRECTORY_SEPARATOR . 'cache';
}
protected function tearDown(): void
{
$this->removeDirectory($this->tempDir);
}
public function testRenderSimpleText(): void
{
file_put_contents($this->tempDir . '/hello.eyrie.php', 'Hello World');
$loader = new FileLoader([$this->tempDir]);
$engine = new Engine($loader, ['cache' => $this->cacheDir]);
$this->assertEquals('Hello World', $engine->render('hello'));
}
public function testRenderVariable(): void
{
file_put_contents($this->tempDir . '/greet.eyrie.php', 'Hello << name >>!');
$loader = new FileLoader([$this->tempDir]);
$engine = new Engine($loader, ['cache' => $this->cacheDir]);
$this->assertEquals('Hello Junie!', $engine->render('greet', ['name' => 'Junie']));
}
public function testAutoEscaping(): void
{
file_put_contents($this->tempDir . '/escape.eyrie.php', '<< content >>');
$loader = new FileLoader([$this->tempDir]);
$engine = new Engine($loader, ['cache' => $this->cacheDir]);
$this->assertEquals('&lt;script&gt;', $engine->render('escape', ['content' => '<script>']));
}
public function testRenderIf(): void
{
$template = '
<( if show )>
Visible
<( endif )>
';
file_put_contents($this->tempDir . '/if.eyrie.php', $template);
$loader = new FileLoader([$this->tempDir]);
$engine = new Engine($loader, ['cache' => $this->cacheDir]);
$this->assertStringContainsString('Visible', $engine->render('if', ['show' => true]));
$this->assertStringNotContainsString('Visible', $engine->render('if', ['show' => false]));
}
public function testRenderIfElse(): void
{
$template = '
<( if show )>
Yes
<( else )>
No
<( endif )>
';
file_put_contents($this->tempDir . '/ifelse.eyrie.php', $template);
$loader = new FileLoader([$this->tempDir]);
$engine = new Engine($loader, ['cache' => $this->cacheDir]);
$this->assertStringContainsString('Yes', $engine->render('ifelse', ['show' => true]));
$this->assertStringContainsString('No', $engine->render('ifelse', ['show' => false]));
}
public function testRenderDotNotation(): void
{
file_put_contents($this->tempDir . '/dot.eyrie.php', '<< user.name >>');
$loader = new FileLoader([$this->tempDir]);
$engine = new Engine($loader, ['cache' => $this->cacheDir, 'debug' => true]);
$this->assertEquals('Junie', $engine->render('dot', ['user' => ['name' => 'Junie']]));
}
public function testRenderForeach(): void
{
$template = '
<( foreach item in items )>
<< item >>
<( endforeach )>
';
file_put_contents($this->tempDir . '/foreach.eyrie.php', $template);
$loader = new FileLoader([$this->tempDir]);
$engine = new Engine($loader, ['cache' => $this->cacheDir]);
$result = $engine->render('foreach', ['items' => ['A', 'B', 'C']]);
$this->assertStringContainsString('A', $result);
$this->assertStringContainsString('B', $result);
$this->assertStringContainsString('C', $result);
}
public function testRenderLoop(): void
{
$template = '
<( loop from 1 to 3 )>
<< loop.index >>:<< loop.last >>
<( endloop )>
';
file_put_contents($this->tempDir . '/loop.eyrie.php', $template);
$loader = new FileLoader([$this->tempDir]);
$engine = new Engine($loader, ['cache' => $this->cacheDir]);
$result = $engine->render('loop');
$this->assertStringContainsString('0:false', $result);
$this->assertStringContainsString('1:false', $result);
$this->assertStringContainsString('2:true', $result);
}
public function testRenderInheritance(): void
{
file_put_contents($this->tempDir . '/layout.eyrie.php', '<html><body>[[ block content ]]Default[[ endblock ]]</body></html>');
file_put_contents($this->tempDir . '/page.eyrie.php', '[[ extends "layout" ]] [[ block content ]]Page Content[[ endblock ]]');
$loader = new FileLoader([$this->tempDir]);
$engine = new Engine($loader, ['cache' => $this->cacheDir]);
$result = $engine->render('page');
$this->assertStringContainsString('<html><body>Page Content</body></html>', $result);
}
public function testRenderSuper(): void
{
file_put_contents($this->tempDir . '/layout_super.eyrie.php', '[[ block content ]]Default[[ endblock ]]');
file_put_contents($this->tempDir . '/page_super.eyrie.php', '[[ extends "layout_super" ]] [[ block content ]]Child [[ super ]] Content[[ endblock ]]');
$loader = new FileLoader([$this->tempDir]);
$engine = new Engine($loader, ['cache' => $this->cacheDir]);
$result = $engine->render('page_super');
$this->assertStringContainsString('Child Default Content', $result);
}
public function testRenderInclude(): void
{
file_put_contents($this->tempDir . '/partial.eyrie.php', 'Partial Content');
file_put_contents($this->tempDir . '/main.eyrie.php', 'Main [[ include "partial" ]]');
$loader = new FileLoader([$this->tempDir]);
$engine = new Engine($loader, ['cache' => $this->cacheDir]);
$result = $engine->render('main');
$this->assertStringContainsString('Main Partial Content', $result);
}
public function testRenderComponent(): void
{
file_put_contents($this->tempDir . '/Movie.eyrie.php', 'Movie: << title >>');
file_put_contents($this->tempDir . '/page_comp.eyrie.php', '<@ Movie title="Inception" />');
$loader = new FileLoader([$this->tempDir]);
$engine = new Engine($loader, ['cache' => $this->cacheDir]);
$result = $engine->render('page_comp');
$this->assertStringContainsString('Movie: Inception', $result);
}
public function testRenderFilters(): void
{
file_put_contents($this->tempDir . '/filter.eyrie.php', '<< name | upper >>');
$loader = new FileLoader([$this->tempDir]);
$engine = new Engine($loader, ['cache' => $this->cacheDir]);
$result = $engine->render('filter', ['name' => 'junie']);
$this->assertEquals('JUNIE', $result);
}
public function testRenderBlockContext(): void
{
file_put_contents($this->tempDir . '/layout_ctx.eyrie.php', '[[ block sidebar ]]Default[[ endblock ]]');
file_put_contents($this->tempDir . '/page_ctx.eyrie.php', '[[ extends "layout_ctx" ]] [[ block sidebar with { user: "Junie" } ]]Hello << user >>[[ endblock ]]');
$loader = new FileLoader([$this->tempDir]);
$engine = new Engine($loader, ['cache' => $this->cacheDir]);
$result = $engine->render('page_ctx');
$this->assertStringContainsString('Hello Junie', $result);
}
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);
}
}