TemplateTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <?php
  2. namespace think;
  3. use PHPUnit\Framework\TestCase;
  4. use tag\Demo;
  5. class TemplateTest extends TestCase
  6. {
  7. public function getTemplate()
  8. {
  9. $config = [
  10. 'view_path' => __DIR__ . '/../template' . DIRECTORY_SEPARATOR,
  11. 'cache_path' => __DIR__ . '/../cache' . DIRECTORY_SEPARATOR,
  12. 'tpl_cache' => false,
  13. 'tpl_replace_string' => ['__STATIC__' => '/static'],
  14. 'taglib_pre_load' => Demo::class,
  15. ];
  16. return new Template($config);
  17. }
  18. // 直接渲染
  19. public function testDisplay()
  20. {
  21. $this->expectOutputString('hello-thinkphp');
  22. $template = $this->getTemplate();
  23. $content = '{$name}-{$email}';
  24. $template->display($content, ['name' => 'hello', 'email' => 'thinkphp']);
  25. }
  26. // 渲染文件
  27. public function testFetch()
  28. {
  29. $this->expectOutputString('success');
  30. $template = $this->getTemplate();
  31. $template->fetch('fetch');
  32. }
  33. // 布局
  34. public function testLayout()
  35. {
  36. $this->expectOutputString('startsuccessend');
  37. $template = $this->getTemplate();
  38. $template->layout('layout');
  39. $template->fetch('fetch');
  40. $template->layout(false);
  41. }
  42. // 扩展解析
  43. public function testExtend()
  44. {
  45. $this->expectOutputString('test.name');
  46. $template = $this->getTemplate();
  47. $template->extend('$Cms', function (array $vars) {
  48. return '\'' . implode('.', $vars) . '\'';
  49. });
  50. $content = '{$Cms.test.name}';
  51. $template->display($content);
  52. }
  53. // 变量
  54. public function testParseVar()
  55. {
  56. $this->expectOutputString('e10adc3949ba59abbe56e057f20f883e');
  57. $template = $this->getTemplate();
  58. $content = '{:md5("123456")}';
  59. $template->display($content, ['password' => '123456']);
  60. }
  61. // 变量使用函数
  62. public function testParseVarFunction()
  63. {
  64. $this->expectOutputString('e10adc3949ba59abbe56e057f20f883e-123456-666456');
  65. $template = $this->getTemplate();
  66. $content = '{$password|md5}-{$password|raw}-{$password|str_replace=123,666,###}';
  67. $template->display($content, ['password' => '123456']);
  68. }
  69. // 默认值
  70. public function testParseDefaultFunction()
  71. {
  72. $this->expectOutputString('test');
  73. $template = $this->getTemplate();
  74. $content = '{$default|default="test"}';
  75. $template->display($content);
  76. }
  77. // 系统变量
  78. public function testParseThinkVar()
  79. {
  80. $this->expectOutputString($_SERVER['PHP_SELF'] . '-' . PHP_VERSION . '-' . PHP_VERSION);
  81. $template = $this->getTemplate();
  82. $content = '{$Request.server.PHP_SELF}-{$Think.const.PHP_VERSION}-{$Think.PHP_VERSION}';
  83. $template->display($content);
  84. }
  85. // 数组
  86. public function testParseArrayVar()
  87. {
  88. $this->expectOutputString('thinkphp<br/>thinkphp');
  89. $template = $this->getTemplate();
  90. $content = '{$data.name}<br/>{$data["name"]}';
  91. $template->display($content, ['data' => ['name' => 'thinkphp']]);
  92. }
  93. // 对象
  94. public function testParseObjectVar()
  95. {
  96. $this->expectOutputString('a-b-c-d');
  97. $object = new class {
  98. public string $a = 'a';
  99. public const b = 'b';
  100. public function c($str) {
  101. return $str;
  102. }
  103. static public function d($str) {
  104. return $str;
  105. }
  106. };
  107. $template = $this->getTemplate();
  108. $content = '{$data->a}-{$data::b}-{$data->c("c")}-{$data::d("d")}';
  109. $template->display($content, ['data' => $object]);
  110. }
  111. // 运算符
  112. public function testParseVarOperator()
  113. {
  114. $this->expectOutputString('2-0-2-0.5-1-1-1-4');
  115. $template = $this->getTemplate();
  116. $content = '{$a+1}-{$a-1}-{$a*$b}-{$a/$b}-{$a%$b}-{$a++}-{--$b}-{$a+$b+abs(-1)}';
  117. $template->display($content, ['a' => 1, 'b' => 2]);
  118. }
  119. // 三元运算符
  120. public function testParseTernaryOperator()
  121. {
  122. $this->expectOutputString('真-默认值-有值-NO');
  123. $template = $this->getTemplate();
  124. $content = '{$true?"真":"假"}-{$null ?? "默认值"}-{$one ?= "有值"}-{$zero ?: "NO"}';
  125. $template->display($content, ['null' => null, 'zero' => 0, 'true' => true, 'one' => 1]);
  126. }
  127. // 单行注释
  128. public function testParseSimpleNote()
  129. {
  130. $this->expectOutputString('123');
  131. $template = $this->getTemplate();
  132. $content = '123{// 注释内容 }';
  133. $template->display($content);
  134. }
  135. // 多行注释
  136. public function testParseMoreNote()
  137. {
  138. $this->expectOutputString('123');
  139. $template = $this->getTemplate();
  140. $content = "123{/* 这是模板\r\n注释内容*/ }";
  141. $template->display($content);
  142. }
  143. // 引用标签
  144. public function testParseInclude()
  145. {
  146. $this->expectOutputString('include');
  147. $template = $this->getTemplate();
  148. $content = '{include file="include"}';
  149. $template->display($content);
  150. }
  151. // 继承标签
  152. public function testParseExtend()
  153. {
  154. $this->expectOutputString("title\r\n主内容main\r\n");
  155. $template = $this->getTemplate();
  156. $content = "{extend name='extend' /}\r\n{block name='title'}title{/block}\r\n{block name='main'}{__block__}main{/block}";
  157. $template->display($content);
  158. }
  159. // 输出替换
  160. public function testParseReplaceString()
  161. {
  162. $this->expectOutputString("start/staticend");
  163. $template = $this->getTemplate();
  164. $content = "start__STATIC__end";
  165. $template->display($content);
  166. }
  167. // 标签扩展
  168. public function testParseDemoTag()
  169. {
  170. $this->expectOutputString(<<<'HTML'
  171. <h1>闭合标签</h1>
  172. 2022-12-31 16:00:00<hr>
  173. <h1>开放标签</h1>
  174. 0=>1<br>
  175. 1=>3<br>
  176. 2=>5<br>
  177. 3=>7<br>
  178. 4=>9<br>
  179. <br>
  180. 0=>2<br>
  181. 1=>4<br>
  182. 2=>6<br>
  183. 3=>8<br>
  184. 4=>10<br>
  185. HTML);
  186. $template = $this->getTemplate();
  187. $content = <<<'HTML'
  188. <h1>闭合标签</h1>
  189. {demo:close time='$demo_time'/}
  190. <hr>
  191. <h1>开放标签</h1>
  192. {demo:open name='demo_name'}
  193. {$key}=>{$demo_name}<br>
  194. {/demo:open}
  195. <br>
  196. {demo:open name='demo_name' type='1'}
  197. {$key}=>{$demo_name}<br>
  198. {/demo:open}
  199. HTML;
  200. $template->display($content, ['demo_time' => 1672502400]);
  201. }
  202. }