| 1 | <?php |
| 2 | /************************************************************* |
| 3 | # 076 License |
| 4 | |
| 5 | Copyright (c) テクニカル諏訪子 |
| 6 | |
| 7 | Permission is hereby granted to any person obtaining a copy of the software |
| 8 | Little Beast (the "Software") to use, modify, merge, copy, publish, distribute, |
| 9 | sublicense, and/or sell copies of the Software, subject to the following conditions: |
| 10 | |
| 11 | 1. **Origin Attribution**: |
| 12 | - You must not misrepresent the origin of the Software; you must not claim |
| 13 | you created the original Software. |
| 14 | - If the Software is used in a product, you must either: |
| 15 | a. Provide clear attribution in the product's documentation, user interface, |
| 16 | or other visible areas, **OR** |
| 17 | b. Pay the original developers a fee they specify in writing. |
| 18 | 2. **Usage Restriction**: |
| 19 | - The Software, or any derivative works, dependencies, or libraries |
| 20 | incorporating it, must not be used for censorship or to suppress freedom of |
| 21 | speech, expression, or creativity. Prohibited uses include, but are not |
| 22 | limited to: |
| 23 | - Censorship of so-called "hate speech", visuals, non-mainstream opinions, |
| 24 | ideas, or objective reality. |
| 25 | - Tools or systems designed to restrict access to information or |
| 26 | artistic works. |
| 27 | 3. **Notice Preservation**: |
| 28 | - This license and the above copyright notice must remain intact in all copies |
| 29 | of the source code. |
| 30 | |
| 31 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
| 32 | INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A |
| 33 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 34 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
| 35 | CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE |
| 36 | OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 37 | */ |
| 38 | namespace Std\Test; |
| 39 | |
| 40 | require_once __DIR__.'/../../../autoload.php'; |
| 41 | |
| 42 | use Std\Lib\Tester; |
| 43 | use Std\Lib\Template; |
| 44 | |
| 45 | $test = new Tester([ |
| 46 | 'colorOutput' => true, |
| 47 | 'verboseOutput' => true |
| 48 | ]); |
| 49 | |
| 50 | // 一時的なviewディレクトリにテンプレートを作る |
| 51 | $viewDir = sys_get_temp_dir().'/lbview_'.bin2hex(random_bytes(6)); |
| 52 | mkdir("{$viewDir}/common", 0700, true); |
| 53 | $tmpl = function(string $src, array $vars = [], string $name = 't') use ($viewDir): string { |
| 54 | file_put_contents("{$viewDir}/{$name}.maron", $src); |
| 55 | $t = new Template('/', $viewDir); |
| 56 | foreach ($vars as $k => $v) $t->assign($k, $v); |
| 57 | return $t->fetch($name); |
| 58 | }; |
| 59 | |
| 60 | $test->describe('Maronテンプレート', function($test) use ($tmpl, $viewDir): void { |
| 61 | $test->it('変数をエスケープして出力するはず', function($test) use ($tmpl): void { |
| 62 | $test->assertEquals('<p><b>"x"</b></p>', $tmpl('<p>{{ $v }}</p>', ['v' => '<b>"x"</b>'])); |
| 63 | }); |
| 64 | |
| 65 | $test->it('{{{ }}}はそのまま出力するはず', function($test) use ($tmpl): void { |
| 66 | $test->assertEquals('<p><b>x</b></p>', $tmpl('<p>{{{ $v }}}</p>', ['v' => '<b>x</b>'])); |
| 67 | $test->assertEquals('<b>x</b>', $tmpl('{{{$v}}}', ['v' => '<b>x</b>'])); |
| 68 | }); |
| 69 | |
| 70 | $test->it('if・elif・elseを処理するはず', function($test) use ($tmpl): void { |
| 71 | $src = '{@ if ($n > 1) @}多{@ elif ($n === 1) @}一{@ else @}無{@ endif @}'; |
| 72 | $test->assertEquals('多', $tmpl($src, ['n' => 5])); |
| 73 | $test->assertEquals('一', $tmpl($src, ['n' => 1])); |
| 74 | $test->assertEquals('無', $tmpl($src, ['n' => 0])); |
| 75 | }); |
| 76 | |
| 77 | $test->it('foreachとforを処理するはず', function($test) use ($tmpl): void { |
| 78 | $test->assertEquals('a=1,b=2,', $tmpl('{@ foreach ($xs as $k => $v) @}{{ $k }}={{ $v }},{@ endforeach @}', ['xs' => ['a' => 1, 'b' => 2]])); |
| 79 | $test->assertEquals('012', $tmpl('{@ for ($i = 0; $i < 3; $i++) @}{{ $i }}{@ endfor @}')); |
| 80 | }); |
| 81 | |
| 82 | $test->it('入れ子のブロックを処理するはず', function($test) use ($tmpl): void { |
| 83 | $src = '{@ foreach ($xs as $x) @}{@ if ($x % 2) @}[{{ $x }}]{@ endif @}{@ endforeach @}'; |
| 84 | $test->assertEquals('[1][3]', $tmpl($src, ['xs' => [1, 2, 3, 4]])); |
| 85 | }); |
| 86 | |
| 87 | $test->it('変数の代入とPHPのコードを処理するはず', function($test) use ($tmpl): void { |
| 88 | $test->assertEquals('200', $tmpl('{$ $x = 100 * 2 $}{{ $x }}')); |
| 89 | $test->assertEquals('hi', $tmpl('{! echo "hi"; !}')); |
| 90 | }); |
| 91 | |
| 92 | $test->it('コメントは出力も実行もしないはず', function($test) use ($tmpl): void { |
| 93 | // 以前はコメントの中の{{ }}が先に変換され、「*/」でPHPのコメントが壊れた |
| 94 | $test->assertEquals('ab', $tmpl('a{# {{ $undefinedVar }} */ #}b')); |
| 95 | $test->assertEquals('ab', $tmpl("a{# 複数\n行 #}b")); |
| 96 | }); |
| 97 | |
| 98 | $test->it('インクルードを処理するはず', function($test) use ($tmpl, $viewDir): void { |
| 99 | file_put_contents("{$viewDir}/common/head.maron", '<h1>{{ $title }}</h1>'); |
| 100 | $test->assertEquals('<h1>T&T</h1>本文', $tmpl('{@ include(common/head) @}本文', ['title' => 'T&T'])); |
| 101 | }); |
| 102 | |
| 103 | $test->it('循環したインクルードで止まるはず', function($test) use ($tmpl, $viewDir): void { |
| 104 | file_put_contents("{$viewDir}/common/loop.maron", '{@ include(common/loop) @}'); |
| 105 | $test->assertThrows(fn() => $tmpl('{@ include(common/loop) @}'), \RuntimeException::class); |
| 106 | }); |
| 107 | |
| 108 | $test->it('無いテンプレートは例外のはず', function($test) use ($viewDir): void { |
| 109 | $test->assertThrows(fn() => (new Template('/', $viewDir))->fetch('nothing'), \RuntimeException::class); |
| 110 | $test->assertThrows(fn() => (new Template('/', $viewDir))->render('nothing'), \RuntimeException::class); |
| 111 | }); |
| 112 | |
| 113 | $test->it('「content」等の名前の変数も使えるはず', function($test) use ($tmpl): void { |
| 114 | // 以前はextract()の後に$content等が上書きされ、テンプレートのソースが表示された |
| 115 | $test->assertEquals('本文/パス', $tmpl('{{ $content }}/{{ $tmplPath }}', ['content' => '本文', 'tmplPath' => 'パス'])); |
| 116 | }); |
| 117 | |
| 118 | $test->it('カスタム関数を呼べるはず', function($test) use ($viewDir): void { |
| 119 | file_put_contents("{$viewDir}/f.maron", '[{@ shout(abc, def) @}]'); |
| 120 | $t = new Template('/', $viewDir); |
| 121 | $t->registerFunction('shout', fn($a, $b) => strtoupper($a.$b)); |
| 122 | $test->assertEquals('[ABCDEF]', $t->fetch('f')); |
| 123 | }); |
| 124 | |
| 125 | $test->it('CSSを追加出来るはず', function($test) use ($viewDir): void { |
| 126 | file_put_contents("{$viewDir}/css.maron", '{@ foreach ($custCss as $c) @}{{{ $c }}}{@ endforeach @}'); |
| 127 | $t = new Template('/', $viewDir); |
| 128 | $t->addCss('code'); |
| 129 | $test->assertStringContains('href="'.ass_url('style-code.css').'"', $t->fetch('css')); |
| 130 | }); |
| 131 | |
| 132 | $test->it('サブディレクトリ(多言語)のテンプレートを使えるはず', function($test) use ($viewDir): void { |
| 133 | mkdir("{$viewDir}/en"); |
| 134 | file_put_contents("{$viewDir}/en/index.maron", 'English'); |
| 135 | $test->assertEquals('English', (new Template('/en', $viewDir))->fetch('index')); |
| 136 | $test->assertEquals('English', (new Template('en/', $viewDir))->fetch('index')); |
| 137 | }); |
| 138 | |
| 139 | $test->it('例外が起きても一時ファイルを残さないはず', function($test) use ($tmpl): void { |
| 140 | $before = count(glob(sys_get_temp_dir().'/tmpl_*') ?: []); |
| 141 | try { |
| 142 | $tmpl('{! throw new \RuntimeException("x"); !}'); |
| 143 | } catch (\RuntimeException $e) {} |
| 144 | $test->assertEquals($before, count(glob(sys_get_temp_dir().'/tmpl_*') ?: [])); |
| 145 | }); |
| 146 | }); |
| 147 | |
| 148 | $rm = function(string $dir) use (&$rm): void { |
| 149 | foreach (scandir($dir) ?: [] as $f) { |
| 150 | if ($f === '.' || $f === '..') continue; |
| 151 | is_dir("{$dir}/{$f}") ? $rm("{$dir}/{$f}") : @unlink("{$dir}/{$f}"); |
| 152 | } |
| 153 | @rmdir($dir); |
| 154 | }; |
| 155 | $rm($viewDir); |
| 156 | |
| 157 | $test->printSummary(); |