Skip to content

Commit

Permalink
Change: add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
satrun77 committed Nov 1, 2021
1 parent dbeb38a commit dfdbb49
Show file tree
Hide file tree
Showing 2 changed files with 278 additions and 0 deletions.
93 changes: 93 additions & 0 deletions tests/ParseSpacelessTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

namespace Moo\Test;

use SilverStripe\Dev\SapphireTest;
//use PHPUnit_Framework_TestCase;
use SilverStripe\View\ArrayData;
use SilverStripe\View\SSViewer;
use SilverStripe\View\TemplateGlobalProvider;

/**
* @internal
* @coversNothing
*/
class ParseSpacelessTest extends SapphireTest implements TemplateGlobalProvider
{
public function testSimpleSpaceCleanUp()
{
$template = <<<'Template'
<% spaceless %>
... 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 <strong>One</strong> <strong> Two</strong>HTML$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 <strong>One</strong><strong> Two</strong>HTML One Two - Three ... ... Template syntax and HTML One Two - Three... ...',
$output
);
}

public function testWithTemplateFunctionIncludeHtml()
{
$template = <<<'Template'
<% spaceless %>
{$Hello($Name).Spaceless}!
<div>Some random text....</div>
<% end_spaceless %>
Template;

$data = ArrayData::create(['Name' => 'First Name']);
$output = SSViewer::execute_string($template, $data);
$this->assertEquals(
'<div><span>Hello</span><strong>First Name</strong></div>! <div>Some random text....</div>',
$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(
'<div> <span>Hello</span> <strong>'.$name.'</strong></div>',
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');
}
}
185 changes: 185 additions & 0 deletions tests/ParseTemplateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
<?php

namespace Moo\Test;

use SilverStripe\Dev\SapphireTest;
//use PHPUnit_Framework_TestCase;
use SilverStripe\View\ArrayData;
use SilverStripe\View\SSViewer;

/**
* @internal
* @coversNothing
*/
class ParseTemplateTest extends SapphireTest
{
protected function setUp(): void
{
// Add path to templates used for testing
SSViewer::add_themes([
__DIR__.'/support',
]);

parent::setUp();
}

public function testTemplateUsage()
{
// Template to render
$template = <<<'Template'
<% template Card %>
<% set Name %>Full Name<% end_set %>
<% set Website %>
View portfolio: <a href="{$Link.URL}">{$Link.Title}</a>
<% end_set %>
<h1>Hello world</h1>
<% include Tags %>
<% end_template %>
Template;

// The expected output
$expected = <<<'Template'
<div class="card">
<h1>Hello world</h1>
<div>Tag1, Tag2</div>
<div>
<div>Full Name</div>
<div>View portfolio: <a href="http://portfolio.com">portfolio.com</a></div>
</div>
</div>
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: <a href="{$Link.URL}">{$Link.Title}</a>
<% end_set %>
<h1>Hello world</h1>
<% end_template %>
Template;

// The expected output
$expected = <<<'Template'
<div class="card card--namespace">
<h1>Hello world</h1>
<div>
<div>Full Name</div>
<div>View portfolio: <a href="http://portfolio.com">portfolio.com</a></div>
</div>
</div>
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: <a href="{$Link.URL}">{$Link.Title}</a>
<% end_set %>
<h1>Hello world</h1>
<% end_template %>
Template;

// The expected output
$expected = <<<'Template'
<div class="card card--var">
<h1>Hello world</h1>
<div>
<div>Full Name</div>
<div>View portfolio: <a href="http://portfolio.com">portfolio.com</a></div>
</div>
</div>
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: <a href="{$Link.URL}">{$Link.Title}</a>
<% end_set %>
<h1>Hello world</h1>
<% end_template %>
Template;

// The expected output
$expected = <<<'Template'
<div class="card card--method">
<h1>Hello world</h1>
<div>
<div>Full Name</div>
<div>View portfolio: <a href="http://portfolio.com">portfolio.com</a></div>
</div>
</div>
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);
}
}

0 comments on commit dfdbb49

Please sign in to comment.