fileSystem = new InlineTemplatesFileSystem();
$this->environment = new Environment(
fileSystem: $this->fileSystem
);
$this->environment->tagRegistry->register(TemplateTag::class);
$this->environment->tagRegistry->register(RenderTag::class);
$this->environment->filterRegistry->register(Data::class);
}
public function test_template_tag_registers_template(): void
{
$template = $this->environment->parseString(<<<'LIQUID'
{% template session %}
{{ facts[randomNumber] }}
{% endtemplate %}
LIQUID
);
$context = $this->environment->newRenderContext(
data: [
'facts' => ['Fact 1', 'Fact 2', 'Fact 3'],
'randomNumber' => 1,
'size_mod' => '--large',
]
);
$result = $template->render($context);
// Template tag should not output anything
$this->assertEquals('', $result);
// Template should be registered in the file system
$this->assertTrue($this->fileSystem->hasTemplate('session'));
$registeredTemplate = $this->fileSystem->readTemplateFile('session');
$this->assertStringContainsString('{{ facts[randomNumber] }}', $registeredTemplate);
$this->assertStringContainsString('{{ size_mod }}', $registeredTemplate);
}
public function test_template_tag_with_render_tag(): void
{
$template = $this->environment->parseString(<<<'LIQUID'
{% template session %}
{{ facts[randomNumber] }}
{% endtemplate %}
{% render "session",
trmnl: trmnl,
facts: facts,
randomNumber: randomNumber,
size_mod: ""
%}
LIQUID
);
$context = $this->environment->newRenderContext(
data: [
'facts' => ['Fact 1', 'Fact 2', 'Fact 3'],
'randomNumber' => 1,
'trmnl' => ['plugin_settings' => ['instance_name' => 'Test']],
]
);
$result = $template->render($context);
// Should render the template content
$this->assertStringContainsString('Fact 2', $result); // facts[1]
$this->assertStringContainsString('class="layout"', $result);
$this->assertStringContainsString('class="value text--center"', $result);
}
public function test_apply_liquid_replacements_converts_with_syntax(): void
{
// This test simulates the applyLiquidReplacements method from the Plugin model
$originalLiquid = <<<'LIQUID'
{% template session %}
{{ facts[randomNumber] }}
{% endtemplate %}
{% render "session" with
trmnl: trmnl,
facts: facts,
randomNumber: randomNumber,
size_mod: ""
%}
LIQUID;
// Apply the same replacement logic as in Plugin::applyLiquidReplacements
$convertedLiquid = preg_replace(
'/{%\s*render\s+([^}]+?)\s+with\s+/i',
'{% render $1, ',
$originalLiquid
);
// Verify the conversion worked
$this->assertStringContainsString('{% render "session",', $convertedLiquid);
$this->assertStringNotContainsString('{% render "session" with', $convertedLiquid);
// Verify the rest of the content is preserved
$this->assertStringContainsString('trmnl: trmnl,', $convertedLiquid);
$this->assertStringContainsString('facts: facts,', $convertedLiquid);
}
public function test_template_tag_with_render_with_tag(): void
{
$originalLiquid = <<<'LIQUID'
{% template session %}
{{ facts[randomNumber] }}
{% endtemplate %}
{% render "session" with
trmnl: trmnl,
facts: facts,
randomNumber: randomNumber,
size_mod: ""
%}
LIQUID;
// Apply the same replacement logic as in applyLiquidReplacements
$convertedLiquid = preg_replace(
'/{%\s*render\s+([^}]+?)\s+with\s+/i',
'{% render $1, ',
$originalLiquid
);
$template = $this->environment->parseString($convertedLiquid);
$context = $this->environment->newRenderContext(
data: [
'facts' => ['Fact 1', 'Fact 2', 'Fact 3'],
'randomNumber' => 1,
'trmnl' => ['plugin_settings' => ['instance_name' => 'Test']],
]
);
$result = $template->render($context);
// Should render the template content
$this->assertStringContainsString('Fact 2', $result); // facts[1]
$this->assertStringContainsString('class="layout"', $result);
$this->assertStringContainsString('class="value text--center"', $result);
}
public function test_template_tag_with_multiple_templates(): void
{
$template = $this->environment->parseString(<<<'LIQUID'
{% template session %}
{{ facts[randomNumber] }}
{% endtemplate %}
{% template title_bar %}
{{ trmnl.plugin_settings.instance_name }}
{{ instance }}
{% endtemplate %}
{% render "session",
trmnl: trmnl,
facts: facts,
randomNumber: randomNumber,
size_mod: ""
%}
{% render "title_bar",
trmnl: trmnl,
instance: "Please try to enjoy each fact equally."
%}
LIQUID
);
$context = $this->environment->newRenderContext(
data: [
'size' => 'full',
'facts' => ['Fact 1', 'Fact 2', 'Fact 3'],
'randomNumber' => 1,
'trmnl' => ['plugin_settings' => ['instance_name' => 'Test Plugin']],
]
);
$result = $template->render($context);
// Should render both templates
$this->assertStringContainsString('Fact 2', $result);
$this->assertStringContainsString('Test Plugin', $result);
$this->assertStringContainsString('Please try to enjoy each fact equally', $result);
$this->assertStringContainsString('class="view view--full"', $result);
}
public function test_template_tag_invalid_name(): void
{
$this->expectException(LiquidException::class);
$template = $this->environment->parseString(<<<'LIQUID'
{% template invalid-name %}
Content
{% endtemplate %}
LIQUID
);
$context = $this->environment->newRenderContext();
$template->render($context);
}
public function test_template_tag_without_file_system(): void
{
$template = $this->environment->parseString(<<<'LIQUID'
{% template session %}
Content
{% endtemplate %}
LIQUID
);
$context = $this->environment->newRenderContext();
$result = $template->render($context);
// Should not throw an error and should return empty string
$this->assertEquals('', $result);
}
}