Add unit test for actually compiling a v2 template

This commit is contained in:
Kijin Sung 2023-10-17 00:33:36 +09:00
parent 84471c83e6
commit eba9a4d333
4 changed files with 167 additions and 0 deletions

View file

@ -0,0 +1,50 @@
<config version="2" />
@use('Rhymix\Framework\Push', 'Push')
@include('^/common/tpl/refresh.html')
@load('^/common/js/plugins/ckeditor/')
@load('css/style.css')
<?php
$foo = 'FOOFOOFOO';
?>
@php
$bar = ['Rhy', 'miX', 'is', 'da', 'BEST!'];
@endphp
@verbatim
{{ $foo }}
@endverbatim
<form action="{\RX_BASEURL}" method="post">
@csrf
<input type="text" @required(Context::getInstance()->get('foo'))>
<input type="text" value="{$bar[0]}" required|if="$bar[3] === 'da'" />
</form>
<div class="foobar"|unless="isset($baz)">
@if ($foo || $bar)
<p>Hello {$foo|noescape}</p>
<p>{{ implode('|', array_map(function(\$i) { return strtoupper(\$i); }, $bar)) }}</p>
@end
</div>
@forelse (Context::get('bar') as $k => $val)
<div>
@empty ($nosuchvar)
<img src="foo/../bar/rhymix.svg" alt="unit tests are cool" />
<span <!--@if($k >= 2)-->class="{$val}"<!--@end-->></span>
@endempty
</div>
@empty
<div>Nothing here...</div>
@end
@desktop
<p>The full class name is {get_class(new Push)|escape}, {Push::class} really.</p>
@enddesktop
<div class="barContainer" data-bar="{$bar|json}"></div>
<script type="text/javascript">
const bar = @json($bar);
</script>

View file

@ -0,0 +1,50 @@
<?php if (!defined("RX_VERSION")) exit(); ?><?php $this->config->version = 2; ?>
<?php (function($__dir, $__path, $__vars = null) { $__tpl = new \Rhymix\Framework\Template($__dir, $__path, "html"); if ($__vars) $__tpl->setVars($__vars); echo $__tpl->compile(); })("common/tpl", 'refresh.html'); ?>
<?php \Context::loadJavascriptPlugin('ckeditor'); ?>
<?php \Context::loadFile(['./tests/_data/template/css/style.css', '', '', '', []]); ?>
<?php
$__Context->foo = 'FOOFOOFOO';
?>
<?php
$__Context->bar = ['Rhy', 'miX', 'is', 'da', 'BEST!'];
?>
{{ $foo }}
<form action="<?php echo htmlspecialchars(\RX_BASEURL, \ENT_QUOTES, 'UTF-8', false); ?>" method="post">
<input type="hidden" name="_rx_csrf_token" value="<?php echo \Rhymix\Framework\Session::getGenericToken(); ?>" />
<input type="text"<?php if (Context::getInstance()->get('foo')): ?> required="required"<?php endif; ?>>
<input type="text" value="<?php echo htmlspecialchars($__Context->bar[0] ?? '', \ENT_QUOTES, 'UTF-8', false); ?>"<?php if ($__Context->bar[3] === 'da'): ?> required="required"<?php endif; ?> />
</form>
<div<?php if (!(isset($__Context->baz))): ?> class="foobar"<?php endif; ?>>
<?php if ($__Context->foo || $__Context->bar): ?>
<p>Hello <?php echo $__Context->foo ?? ''; ?></p>
<p><?php echo htmlspecialchars(implode('|', array_map(function($i) { return strtoupper($i); }, $__Context->bar)), \ENT_QUOTES, 'UTF-8', false); ?></p>
<?php endif; ?>
</div>
<?php $__tmp_19148045bac5d8 = Context::get('bar') ?? []; if($__tmp_19148045bac5d8): foreach ($__tmp_19148045bac5d8 as $__Context->k => $__Context->val): ?>
<div>
<?php if (empty($__Context->nosuchvar)): ?>
<img src="/rhymix/tests/_data/template/bar/rhymix.svg" alt="unit tests are cool" />
<span <?php if ($__Context->k >= 2): ?>class="<?php echo htmlspecialchars($__Context->val ?? '', \ENT_QUOTES, 'UTF-8', false); ?>"<?php endif; ?>></span>
<?php endif; ?>
</div>
<?php endforeach; else: ?>
<div>Nothing here...</div>
<?php endif; ?>
<?php if (!$__Context->m): ?>
<p>The full class name is <?php echo htmlspecialchars(get_class(new Rhymix\Framework\Push), \ENT_QUOTES, 'UTF-8', true); ?>, <?php echo htmlspecialchars(Rhymix\Framework\Push::class, \ENT_QUOTES, 'UTF-8', false); ?> really.</p>
<?php endif; ?>
<div class="barContainer" data-bar="<?php echo $this->config->context === 'JS' ? (json_encode($__Context->bar ?? '', \JSON_UNESCAPED_UNICODE | \JSON_UNESCAPED_SLASHES)) : htmlspecialchars(json_encode($__Context->bar ?? '', \JSON_UNESCAPED_UNICODE | \JSON_UNESCAPED_SLASHES), \ENT_QUOTES, 'UTF-8', false); ?>"></div>
<script type="text/javascript"<?php $this->config->context = "JS"; ?>>
const bar = <?php echo $this->config->context === 'JS' ? json_encode($__Context->bar, \JSON_UNESCAPED_UNICODE | \JSON_UNESCAPED_SLASHES) : htmlspecialchars(json_encode($__Context->bar, \JSON_UNESCAPED_UNICODE | \JSON_UNESCAPED_SLASHES), \ENT_QUOTES, 'UTF-8', false); ?>;
<?php $this->config->context = "HTML"; ?></script>

View file

@ -0,0 +1,49 @@
<script>
top.location.reload();
</script>
{{ $foo }}
<form action="/rhymix/" method="post">
<input type="hidden" name="_rx_csrf_token" value="" />
<input type="text" required="required">
<input type="text" value="Rhy" required="required" />
</form>
<div class="foobar">
<p>Hello FOOFOOFOO</p>
<p>RHY|MIX|IS|DA|BEST!</p>
</div>
<div>
<img src="/rhymix/tests/_data/template/bar/rhymix.svg" alt="unit tests are cool" />
<span ></span>
</div>
<div>
<img src="/rhymix/tests/_data/template/bar/rhymix.svg" alt="unit tests are cool" />
<span ></span>
</div>
<div>
<img src="/rhymix/tests/_data/template/bar/rhymix.svg" alt="unit tests are cool" />
<span class="is"></span>
</div>
<div>
<img src="/rhymix/tests/_data/template/bar/rhymix.svg" alt="unit tests are cool" />
<span class="da"></span>
</div>
<div>
<img src="/rhymix/tests/_data/template/bar/rhymix.svg" alt="unit tests are cool" />
<span class="BEST!"></span>
</div>
<p>The full class name is Rhymix\Framework\Push, Rhymix\Framework\Push really.</p>
<div class="barContainer" data-bar="[&quot;Rhy&quot;,&quot;miX&quot;,&quot;is&quot;,&quot;da&quot;,&quot;BEST!&quot;]"></div>
<script type="text/javascript">
const bar = ["Rhy","miX","is","da","BEST!"];
</script>

View file

@ -976,7 +976,25 @@ class TemplateParserV2Test extends \Codeception\Test\Unit
public function testCompile()
{
Context::init();
$tmpl = new \Rhymix\Framework\Template('./tests/_data/template', 'v2example.html');
$compiled_output = $tmpl->compileDirect('./tests/_data/template', 'v2example.html');
$tmpvar = preg_match('/(\$__tmp_[0-9a-f]{14})/', $compiled_output, $m) ? $m[1] : '';
//file_put_contents(\RX_BASEDIR . 'tests/_data/template/v2result1.php', $compiled_output);
$expected = file_get_contents(\RX_BASEDIR . 'tests/_data/template/v2result1.php');
$expected = preg_replace('/(\$__tmp_[0-9a-f]{14})/', $tmpvar, $expected);
$this->assertEquals($expected, $compiled_output);
$executed_output = $tmpl->compile();
$executed_output = preg_replace('/<!--#Template(Start|End):.+?-->\n/', '', $executed_output);
$tmpvar = preg_match('/(\$__tmp_[0-9a-f]{14})/', $executed_output, $m) ? $m[1] : '';
//file_put_contents(\RX_BASEDIR . 'tests/_data/template/v2result2.php', $executed_output);
$expected = file_get_contents(\RX_BASEDIR . 'tests/_data/template/v2result2.php');
$expected = preg_replace('/(\$__tmp_[0-9a-f]{14})/', $tmpvar, $expected);
$this->assertEquals($expected, $executed_output);
}
protected function _parse($source, $force_v2 = true)