Eyrie-Templates/tests/HelperTest.php

56 lines
1.4 KiB
PHP
Raw 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 HelperTest extends TestCase
{
private string $tempDir;
private string $cacheDir;
protected function setUp(): void
{
$this->tempDir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'eyrie_helper_tests_' . uniqid();
mkdir($this->tempDir);
$this->cacheDir = $this->tempDir . DIRECTORY_SEPARATOR . 'cache';
mkdir($this->cacheDir);
}
protected function tearDown(): void
{
$this->removeDirectory($this->tempDir);
}
public function testHelperCall(): void
{
file_put_contents($this->tempDir . '/helper.eyrie.php', '<< greet("Junie") >>');
$loader = new FileLoader([$this->tempDir]);
$engine = new Engine($loader, ['cache' => $this->cacheDir, 'debug' => true]);
$engine->addHelper('greet', function(string $name) {
return "Hello, $name!";
});
$result = $engine->render('helper');
$this->assertEquals('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);
}
}