diff --git a/tests/ParseSpacelessTest.php b/tests/ParseSpacelessTest.php new file mode 100644 index 0000000..7cd3cea --- /dev/null +++ b/tests/ParseSpacelessTest.php @@ -0,0 +1,93 @@ + + ... Template syntax and HTML ... +<% end_spaceless %> +Template; + + $output = SSViewer::execute_string($template, []); + + $this->assertEquals('... Template syntax and HTML ...', $output); + $this->assertNotEquals('... Template syntax and HTML ... ', $output); + $this->assertNotEquals(' ... Template syntax and HTML ...', $output); + } + + public function testWithTemplateFunctionIncludeValidSpaces() + { + $template = <<<'Template' +<% spaceless %> + ... Template syntax and One TwoHTML$Concat(' ', 'One', ' ', 'Two', ' - ', 'Three') ... + ... Template syntax and HTML{$Concat(' ', 'One', ' ', 'Two', ' - ', 'Three')}... + ... +<% end_spaceless %> +Template; + $output = SSViewer::execute_string($template, []); + $this->assertEquals( + '... Template syntax and One TwoHTML One Two - Three ... ... Template syntax and HTML One Two - Three... ...', + $output + ); + } + + public function testWithTemplateFunctionIncludeHtml() + { + $template = <<<'Template' +<% spaceless %> + {$Hello($Name).Spaceless}! + +
Some random text....
+<% end_spaceless %> +Template; + + $data = ArrayData::create(['Name' => 'First Name']); + $output = SSViewer::execute_string($template, $data); + $this->assertEquals( + '
HelloFirst Name
!
Some random text....
', + $output + ); + } + + public static function get_template_global_variables() + { + return [ + 'Hello' => [ + 'method' => 'Hello', + 'casting' => 'HTMLText', + ], + 'Concat' => [ + 'method' => 'Concat', + 'casting' => 'HTMLText', + ], + ]; + } + + public static function Hello(?string $name): string + { + return html_entity_decode( + '
Hello '.$name.'
', + ENT_QUOTES | ENT_XML1, + 'UTF-8' + ); + } + + public static function Concat(): string + { + return html_entity_decode(implode('', func_get_args()), ENT_QUOTES | ENT_XML1, 'UTF-8'); + } +} diff --git a/tests/ParseTemplateTest.php b/tests/ParseTemplateTest.php new file mode 100644 index 0000000..7e40a39 --- /dev/null +++ b/tests/ParseTemplateTest.php @@ -0,0 +1,185 @@ + + <% set Name %>Full Name<% end_set %> + <% set Website %> + View portfolio: {$Link.Title} + <% end_set %> + +

Hello world

+ <% include Tags %> +<% end_template %> +Template; + + // The expected output + $expected = <<<'Template' +
+

Hello world

+
Tag1, Tag2
+
+
Full Name
+
View portfolio: portfolio.com
+
+
+Template; + + // Assert template output matches the expected + $this->assertTemplate($template, $expected); + } + + public function testTemplateNameWithNamespace() + { + // Template to render + $template = <<<'Template' +<% template 'Namespace\Card' %> + <% set Name %>Full Name<% end_set %> + <% set Website %> + View portfolio: {$Link.Title} + <% end_set %> + +

Hello world

+ +<% end_template %> +Template; + + // The expected output + $expected = <<<'Template' +
+

Hello world

+
+
Full Name
+
View portfolio: portfolio.com
+
+
+Template; + + // Assert template output matches the expected + $this->assertTemplate($template, $expected); + } + + public function testTemplateWithVarAsFileName() + { + // Template to render + $template = <<<'Template' +<% template $MyTemplate %> + <% set Name %>Full Name<% end_set %> + <% set Website %> + View portfolio: {$Link.Title} + <% end_set %> + +

Hello world

+ +<% end_template %> +Template; + + // The expected output + $expected = <<<'Template' +
+

Hello world

+
+
Full Name
+
View portfolio: portfolio.com
+
+
+Template; + + // Assert template output matches the expected + $this->assertTemplate($template, $expected); + } + + public function testTemplateNameFromMethod() + { + // Template to render + $template = <<<'Template' +<% template $Me.CoolTemplate %> + <% set Name %>Full Name<% end_set %> + <% set Website %> + View portfolio: {$Link.Title} + <% end_set %> + +

Hello world

+ +<% end_template %> +Template; + + // The expected output + $expected = <<<'Template' +
+

Hello world

+
+
Full Name
+
View portfolio: portfolio.com
+
+
+Template; + + // Assert template output matches the expected + $this->assertTemplate($template, $expected); + } + + /** + * Get data to be used for template testing. + */ + protected function getTemplateData(): ArrayData + { + return new class() extends ArrayData { + public function __construct() + { + parent::__construct([ + 'MyTemplate' => 'Namespace\CardVar', + 'Link' => [ + 'URL' => 'http://portfolio.com', + 'Title' => 'portfolio.com', + ], + ]); + } + + public function getCoolTemplate(): string + { + return 'Namespace\CardMethod'; + } + }; + } + + /** + * Compile and assert template. + */ + protected function assertTemplate(string $template, string $expected): void + { + // Data to use as template Top context + $data = $this->getTemplateData(); + + // Render template + $output = SSViewer::execute_string($template, $data); + + // Assert template output matches the expected + $this->assertXmlStringEqualsXmlString($expected, $output); + } +}