56 lines
1.4 KiB
PHP
56 lines
1.4 KiB
PHP
<?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);
|
|
}
|
|
}
|