49 lines
1.6 KiB
PHP
49 lines
1.6 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
declare(strict_types=1);
|
||
|
|
|
||
|
|
namespace Scape\Tests;
|
||
|
|
|
||
|
|
use PHPUnit\Framework\TestCase;
|
||
|
|
use Scape\Engine;
|
||
|
|
use Scape\Interfaces\HostProviderInterface;
|
||
|
|
|
||
|
|
class HostProviderTest extends TestCase
|
||
|
|
{
|
||
|
|
public function testHostNamespaceDelegation(): void
|
||
|
|
{
|
||
|
|
$mockProvider = new class implements HostProviderInterface {
|
||
|
|
public function has(string $name): bool {
|
||
|
|
return in_array($name, ['version', 'translate']);
|
||
|
|
}
|
||
|
|
public function call(string $name, array $args = []): mixed {
|
||
|
|
if ($name === 'version') return '1.0.0';
|
||
|
|
if ($name === 'translate') {
|
||
|
|
$key = $args[0] ?? '';
|
||
|
|
$lang = $args[1] ?? 'en';
|
||
|
|
return "Translated '$key' to $lang";
|
||
|
|
}
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
$engine = new Engine();
|
||
|
|
$engine->registerHostProvider($mockProvider);
|
||
|
|
|
||
|
|
$templatesDir = __DIR__ . '/../templates';
|
||
|
|
file_put_contents($templatesDir . '/tests/host_test.scape.php',
|
||
|
|
'Version: {{ host.version }}' . "\n" .
|
||
|
|
'i18n: {{ host.translate(\'welcome\', \'fr\') }}' . "\n" .
|
||
|
|
'Var arg: {{ host.translate(my_key) }}'
|
||
|
|
);
|
||
|
|
|
||
|
|
$output = $engine->render('tests.host_test', ['my_key' => 'hello']);
|
||
|
|
|
||
|
|
$this->assertStringContainsString('Version: 1.0.0', $output);
|
||
|
|
$this->assertStringContainsString('i18n: Translated 'welcome' to fr', $output);
|
||
|
|
$this->assertStringContainsString('Var arg: Translated 'hello' to en', $output);
|
||
|
|
|
||
|
|
unlink($templatesDir . '/tests/host_test.scape.php');
|
||
|
|
}
|
||
|
|
}
|