Files
AggloSign/backend/tests/Unit/TemplateRendererTest.php
2026-09-30 16:27:32 +02:00

55 lines
1.9 KiB
PHP

<?php
namespace Tests\Unit;
use App\Signature\TemplateRenderer;
use PHPUnit\Framework\TestCase;
class TemplateRendererTest extends TestCase
{
private TemplateRenderer $r;
protected function setUp(): void
{
$this->r = new TemplateRenderer;
}
public function test_substitue_et_echappe_les_variables(): void
{
$this->assertSame('Bonjour Jean &lt;b&gt;', $this->r->render('Bonjour {{prenom}}', ['prenom' => 'Jean <b>']));
}
public function test_valeur_brute_avec_triple_accolade(): void
{
$this->assertSame('<img src="x">', $this->r->render('{{{banniere}}}', ['banniere' => '<img src="x">']));
}
public function test_bloc_if_masque_si_vide(): void
{
$tpl = 'A{{#if mobile}} Mob. {{mobile}}{{/if}}B';
$this->assertSame('AB', $this->r->render($tpl, ['mobile' => ' ']));
$this->assertSame('A Mob. 06B', $this->r->render($tpl, ['mobile' => '06']));
}
public function test_blocs_imbriques_et_unless(): void
{
$tpl = '{{#if adresse}}{{adresse}}{{#if ville}}, {{ville}}{{/if}}{{/if}}{{#unless adresse}}-{{/unless}}';
$this->assertSame('1 rue X, Béziers', $this->r->render($tpl, ['adresse' => '1 rue X', 'ville' => 'Béziers']));
$this->assertSame('1 rue X', $this->r->render($tpl, ['adresse' => '1 rue X']));
$this->assertSame('-', $this->r->render($tpl, []));
}
public function test_variable_inconnue_vide_et_casse_ignoree(): void
{
$this->assertSame('[]', $this->r->render('[{{Inconnue}}]', []));
$this->assertSame('x', $this->r->render('{{PRENOM}}', ['prenom' => 'x']));
}
public function test_liste_des_variables(): void
{
$vars = $this->r->variables('{{prenom}} {{#if mobile}}{{mobile}}{{/if}} {{{banniere}}}');
sort($vars);
$this->assertSame(['banniere', 'mobile', 'prenom'], $vars);
}
}