From eba9a4d3333cbb3abaeb30f02594851eab2c7709 Mon Sep 17 00:00:00 2001 From: Kijin Sung Date: Tue, 17 Oct 2023 00:33:36 +0900 Subject: [PATCH] Add unit test for actually compiling a v2 template --- tests/_data/template/v2example.html | 50 +++++++++++++++++++ tests/_data/template/v2result1.php | 50 +++++++++++++++++++ tests/_data/template/v2result2.php | 49 ++++++++++++++++++ .../parsers/TemplateParserV2Test.php | 18 +++++++ 4 files changed, 167 insertions(+) create mode 100644 tests/_data/template/v2result1.php create mode 100644 tests/_data/template/v2result2.php diff --git a/tests/_data/template/v2example.html b/tests/_data/template/v2example.html index e69de29bb..49b49628b 100644 --- a/tests/_data/template/v2example.html +++ b/tests/_data/template/v2example.html @@ -0,0 +1,50 @@ + + +@use('Rhymix\Framework\Push', 'Push') +@include('^/common/tpl/refresh.html') +@load('^/common/js/plugins/ckeditor/') +@load('css/style.css') + + +@php + $bar = ['Rhy', 'miX', 'is', 'da', 'BEST!']; +@endphp +@verbatim + {{ $foo }} +@endverbatim + +
+ @csrf + get('foo'))> + +
+ +
+ @if ($foo || $bar) +

Hello {$foo|noescape}

+

{{ implode('|', array_map(function(\$i) { return strtoupper(\$i); }, $bar)) }}

+ @end +
+ +@forelse (Context::get('bar') as $k => $val) +
+ @empty ($nosuchvar) + unit tests are cool + = 2)-->class="{$val}"> + @endempty +
+@empty +
Nothing here...
+@end + +@desktop +

The full class name is {get_class(new Push)|escape}, {Push::class} really.

+@enddesktop + +
+ + diff --git a/tests/_data/template/v2result1.php b/tests/_data/template/v2result1.php new file mode 100644 index 000000000..e60aabfbf --- /dev/null +++ b/tests/_data/template/v2result1.php @@ -0,0 +1,50 @@ +config->version = 2; ?> + + +setVars($__vars); echo $__tpl->compile(); })("common/tpl", 'refresh.html'); ?> + + + +foo = 'FOOFOOFOO'; +?> +bar = ['Rhy', 'miX', 'is', 'da', 'BEST!']; +?> + + {{ $foo }} + + +
+ + get('foo')): ?> required="required"> + bar[3] === 'da'): ?> required="required" /> +
+ +baz))): ?> class="foobar"> +foo || $__Context->bar): ?> +

Hello foo ?? ''; ?>

+

bar)), \ENT_QUOTES, 'UTF-8', false); ?>

+ + + +k => $__Context->val): ?> +
+nosuchvar)): ?> + unit tests are cool + k >= 2): ?>class="val ?? '', \ENT_QUOTES, 'UTF-8', false); ?>"> + +
+ +
Nothing here...
+ + +m): ?> +

The full class name is , really.

+ + +
+ + diff --git a/tests/_data/template/v2result2.php b/tests/_data/template/v2result2.php new file mode 100644 index 000000000..c44af8993 --- /dev/null +++ b/tests/_data/template/v2result2.php @@ -0,0 +1,49 @@ + + + + + + {{ $foo }} + + +
+ + + +
+ +
+

Hello FOOFOOFOO

+

RHY|MIX|IS|DA|BEST!

+
+ +
+ unit tests are cool + +
+
+ unit tests are cool + +
+
+ unit tests are cool + +
+
+ unit tests are cool + +
+
+ unit tests are cool + +
+ +

The full class name is Rhymix\Framework\Push, Rhymix\Framework\Push really.

+ +
+ + diff --git a/tests/unit/framework/parsers/TemplateParserV2Test.php b/tests/unit/framework/parsers/TemplateParserV2Test.php index c011d3860..4d1251c99 100644 --- a/tests/unit/framework/parsers/TemplateParserV2Test.php +++ b/tests/unit/framework/parsers/TemplateParserV2Test.php @@ -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('/\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)