templateTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. /**
  12. * 模板测试
  13. * @author oldrind
  14. */
  15. namespace tests\thinkphp\library\think;
  16. use think\Cache;
  17. use think\Template;
  18. class templateTest extends \PHPUnit_Framework_TestCase
  19. {
  20. /**
  21. * @var Template
  22. */
  23. protected $template;
  24. public function setUp()
  25. {
  26. $this->template = new Template();
  27. }
  28. public function testAssign()
  29. {
  30. $reflectProperty = new \ReflectionProperty(get_class($this->template), 'data');
  31. $reflectProperty->setAccessible(true);
  32. $this->template->assign('version', 'ThinkPHP3.2');
  33. $data = $reflectProperty->getValue($this->template);
  34. $this->assertEquals('ThinkPHP3.2', $data['version']);
  35. $this->template->assign(['name' => 'Gao', 'version' => 'ThinkPHP5']);
  36. $data = $reflectProperty->getValue($this->template);
  37. $this->assertEquals('Gao', $data['name']);
  38. $this->assertEquals('ThinkPHP5', $data['version']);
  39. }
  40. public function testGet()
  41. {
  42. $this->template = new Template();
  43. $data = [
  44. 'project' => 'ThinkPHP',
  45. 'version' => [
  46. 'ThinkPHP5' => ['Think5.0', 'Think5.1']
  47. ]
  48. ];
  49. $this->template->assign($data);
  50. $this->assertSame($data, $this->template->get());
  51. $this->assertSame('ThinkPHP', $this->template->get('project'));
  52. $this->assertSame(['Think5.0', 'Think5.1'], $this->template->get('version.ThinkPHP5'));
  53. $this->assertNull($this->template->get('version.ThinkPHP3.2'));
  54. }
  55. /**
  56. * @dataProvider provideTestParseWithVar
  57. */
  58. public function testParseWithVar($content, $expected)
  59. {
  60. $this->template = new Template();
  61. $this->template->parse($content);
  62. $this->assertEquals($expected, $content);
  63. }
  64. /**
  65. * @dataProvider provideTestParseWithVarFunction
  66. */
  67. public function testParseWithVarFunction($content, $expected)
  68. {
  69. $this->template = new Template();
  70. $this->template->parse($content);
  71. $this->assertEquals($expected, $content);
  72. }
  73. /**
  74. * @dataProvider provideTestParseWithVarIdentify
  75. */
  76. public function testParseWithVarIdentify($content, $expected, $config)
  77. {
  78. $this->template = new Template($config);
  79. $this->template->parse($content);
  80. $this->assertEquals($expected, $content);
  81. }
  82. /**
  83. * @dataProvider provideTestParseWithThinkVar
  84. */
  85. public function testParseWithThinkVar($content, $expected)
  86. {
  87. $config['tpl_begin'] = '{';
  88. $config['tpl_end'] = '}';
  89. $this->template = new Template($config);
  90. $_SERVER['SERVER_NAME'] = 'server_name';
  91. $_GET['action'] = 'action';
  92. $_POST['action'] = 'action';
  93. $_COOKIE['name'] = 'name';
  94. $_SESSION['action'] = ['name' => 'name'];
  95. $this->template->parse($content);
  96. $this->assertEquals($expected, $content);
  97. }
  98. /**
  99. * @expectedException \think\exception\TemplateNotFoundException
  100. */
  101. public function testFetchWithEmptyTemplate()
  102. {
  103. $this->template = new Template();
  104. $this->template->fetch('Foo');
  105. }
  106. /**
  107. * @dataProvider provideTestFetchWithNoCache
  108. */
  109. public function testFetchWithNoCache($data, $expected)
  110. {
  111. $this->template = new Template();
  112. $this->template->fetch($data['template'], $data['vars'], $data['config']);
  113. $this->expectOutputString($expected);
  114. }
  115. public function testFetchWithCache()
  116. {
  117. $this->template = new Template();
  118. $data = [
  119. 'name' => 'value'
  120. ];
  121. $config = [
  122. 'cache_id' => 'TEST_FETCH_WITH_CACHE',
  123. 'display_cache' => true,
  124. ];
  125. $this->template->fetch(APP_PATH . 'views' . DS .'display.html', $data, $config);
  126. $this->expectOutputString('value');
  127. $this->assertEquals('value', Cache::get($config['cache_id']));
  128. }
  129. public function testDisplay()
  130. {
  131. $config = [
  132. 'view_path' => APP_PATH . DS . 'views' . DS,
  133. 'view_suffix' => '.html',
  134. 'layout_on' => true,
  135. 'layout_name' => 'layout'
  136. ];
  137. $this->template = new Template($config);
  138. $this->template->assign('files', ['extend' => 'extend', 'include' => 'include']);
  139. $this->template->assign('user', ['name' => 'name', 'account' => 100]);
  140. $this->template->assign('message', 'message');
  141. $this->template->assign('info', ['value' => 'value']);
  142. $content = <<<EOF
  143. {extend name="\$files.extend" /}
  144. {block name="main"}
  145. main
  146. {block name="side"}
  147. {__BLOCK__}
  148. {include file="\$files.include" name="\$user.name" value="\$user.account" /}
  149. {\$message}{literal}{\$message}{/literal}
  150. {/block}
  151. {block name="mainbody"}
  152. mainbody
  153. {/block}
  154. {/block}
  155. EOF;
  156. $expected = <<<EOF
  157. <nav>
  158. header
  159. <div id="wrap">
  160. <input name="info" value="value">
  161. value:
  162. main
  163. side
  164. <input name="name" value="100">
  165. value:
  166. message{\$message}
  167. mainbody
  168. {\$name}
  169. php code</div>
  170. </nav>
  171. EOF;
  172. $this->template->display($content);
  173. $this->expectOutputString($expected);
  174. }
  175. /**
  176. * @dataProvider provideTestLayout
  177. */
  178. public function testLayout($data, $expected)
  179. {
  180. $this->template = new Template();
  181. $this->template->layout($data['name'], $data['replace']);
  182. $this->assertSame($expected['layout_on'], $this->template->config('layout_on'));
  183. $this->assertSame($expected['layout_name'], $this->template->config('layout_name'));
  184. $this->assertSame($expected['layout_item'], $this->template->config('layout_item'));
  185. }
  186. public function testParseAttr()
  187. {
  188. $attributes = $this->template->parseAttr("<name version='ThinkPHP' name=\"Gao\"></name>");
  189. $this->assertSame(['version' => 'ThinkPHP', 'name' => 'Gao'], $attributes);
  190. $attributes = $this->template->parseAttr("<name version='ThinkPHP' name=\"Gao\">TestCase</name>", 'version');
  191. $this->assertSame('ThinkPHP', $attributes);
  192. }
  193. public function testIsCache()
  194. {
  195. $this->template = new Template();
  196. $config = [
  197. 'cache_id' => rand(0, 10000) . rand(0, 10000) . time(),
  198. 'display_cache' => true
  199. ];
  200. $this->assertFalse($this->template->isCache($config['cache_id']));
  201. $this->template->fetch(APP_PATH . 'views' . DS .'display.html', [], $config);
  202. $this->assertTrue($this->template->isCache($config['cache_id']));
  203. }
  204. public function provideTestParseWithVar()
  205. {
  206. return [
  207. ["{\$name.a.b}", "<?php echo \$name['a']['b']; ?>"],
  208. ["{\$name.a??'test'}", "<?php echo isset(\$name['a'])?\$name['a']:'test'; ?>"],
  209. ["{\$name.a?='test'}", "<?php if(!empty(\$name['a'])) echo 'test'; ?>"],
  210. ["{\$name.a?:'test'}", "<?php echo !empty(\$name['a'])?\$name['a']:'test'; ?>"],
  211. ["{\$name.a?\$name.b:'no'}", "<?php echo !empty(\$name['a'])?\$name['b']:'no'; ?>"],
  212. ["{\$name.a==\$name.b?='test'}", "<?php if(\$name['a']==\$name['b']) echo 'test'; ?>"],
  213. ["{\$name.a==\$name.b?'a':'b'}", "<?php echo \$name['a']==\$name['b']?'a':'b'; ?>"],
  214. ["{\$name.a|default='test'==\$name.b?'a':'b'}", "<?php echo (isset(\$name['a']) && (\$name['a'] !== '')?\$name['a']:'test')==\$name['b']?'a':'b'; ?>"],
  215. ["{\$name.a|trim==\$name.b?='eq'}", "<?php if(trim(\$name['a'])==\$name['b']) echo 'eq'; ?>"],
  216. ["{:ltrim(rtrim(\$name.a))}", "<?php echo ltrim(rtrim(\$name['a'])); ?>"],
  217. ["{~echo(trim(\$name.a))}", "<?php echo(trim(\$name['a'])); ?>"],
  218. ["{++\$name.a}", "<?php echo ++\$name['a']; ?>"],
  219. ["{/*\$name*/}", ""],
  220. ["{\$0a}", "{\$0a}"]
  221. ];
  222. }
  223. public function provideTestParseWithVarFunction()
  224. {
  225. return [
  226. ["{\$name.a.b|default='test'}", "<?php echo (isset(\$name['a']['b']) && (\$name['a']['b'] !== '')?\$name['a']['b']:'test'); ?>"],
  227. ["{\$create_time|date=\"y-m-d\",###}", "<?php echo date(\"y-m-d\",\$create_time); ?>"],
  228. ["{\$name}\n{\$name|trim|substr=0,3}", "<?php echo \$name; ?>\n<?php echo substr(trim(\$name),0,3); ?>"]
  229. ];
  230. }
  231. public function provideTestParseWithVarIdentify()
  232. {
  233. $config['tpl_begin'] = '<#';
  234. $config['tpl_end'] = '#>';
  235. $config['tpl_var_identify'] = '';
  236. return [
  237. [
  238. "<#\$info.a??'test'#>",
  239. "<?php echo ((is_array(\$info)?\$info['a']:\$info->a)) ? (is_array(\$info)?\$info['a']:\$info->a) : 'test'; ?>",
  240. $config
  241. ],
  242. [
  243. "<#\$info.a?='test'#>",
  244. "<?php if((is_array(\$info)?\$info['a']:\$info->a)) echo 'test'; ?>",
  245. $config
  246. ],
  247. [
  248. "<#\$info.a==\$info.b?='test'#>",
  249. "<?php if((is_array(\$info)?\$info['a']:\$info->a)==(is_array(\$info)?\$info['b']:\$info->b)) echo 'test'; ?>",
  250. $config
  251. ],
  252. [
  253. "<#\$info.a|default='test'?'yes':'no'#>",
  254. "<?php echo ((is_array(\$info)?\$info['a']:\$info->a) ?: 'test')?'yes':'no'; ?>",
  255. $config
  256. ],
  257. [
  258. "{\$info2.b|trim?'yes':'no'}",
  259. "<?php echo trim(\$info2->b)?'yes':'no'; ?>",
  260. array_merge(['tpl_var_identify' => 'obj'])
  261. ]
  262. ];
  263. }
  264. public function provideTestParseWithThinkVar()
  265. {
  266. return [
  267. ["{\$Think.SERVER.SERVER_NAME}<br/>", "<?php echo \\think\\Request::instance()->server('SERVER_NAME'); ?><br/>"],
  268. ["{\$Think.GET.action}<br/>", "<?php echo \\think\\Request::instance()->get('action'); ?><br/>"],
  269. ["{\$Think.POST.action}<br/>", "<?php echo \\think\\Request::instance()->post('action'); ?><br/>"],
  270. ["{\$Think.COOKIE.action}<br/>", "<?php echo \\think\\Cookie::get('action'); ?><br/>"],
  271. ["{\$Think.COOKIE.action.name}<br/>", "<?php echo \\think\\Cookie::get('action.name'); ?><br/>"],
  272. ["{\$Think.SESSION.action}<br/>", "<?php echo \\think\\Session::get('action'); ?><br/>"],
  273. ["{\$Think.SESSION.action.name}<br/>", "<?php echo \\think\\Session::get('action.name'); ?><br/>"],
  274. ["{\$Think.ENV.OS}<br/>", "<?php echo \\think\\Request::instance()->env('OS'); ?><br/>"],
  275. ["{\$Think.REQUEST.action}<br/>", "<?php echo \\think\\Request::instance()->request('action'); ?><br/>"],
  276. ["{\$Think.CONST.THINK_VERSION}<br/>", "<?php echo THINK_VERSION; ?><br/>"],
  277. ["{\$Think.LANG.action}<br/>", "<?php echo \\think\\Lang::get('action'); ?><br/>"],
  278. ["{\$Think.CONFIG.action.name}<br/>", "<?php echo \\think\\Config::get('action.name'); ?><br/>"],
  279. ["{\$Think.NOW}<br/>", "<?php echo date('Y-m-d g:i a',time()); ?><br/>"],
  280. ["{\$Think.VERSION}<br/>", "<?php echo THINK_VERSION; ?><br/>"],
  281. ["{\$Think.LDELIM}<br/>", "<?php echo '{'; ?><br/>"],
  282. ["{\$Think.RDELIM}<br/>", "<?php echo '}'; ?><br/>"],
  283. ["{\$Think.THINK_VERSION}<br/>", "<?php echo THINK_VERSION; ?><br/>"],
  284. ["{\$Think.SITE.URL}", "<?php echo ''; ?>"]
  285. ];
  286. }
  287. public function provideTestFetchWithNoCache()
  288. {
  289. $provideData = [];
  290. $this->template = [
  291. 'template' => APP_PATH . 'views' . DS .'display.html',
  292. 'vars' => [],
  293. 'config' => []
  294. ];
  295. $expected = 'default';
  296. $provideData[] = [$this->template, $expected];
  297. $this->template = [
  298. 'template' => APP_PATH . 'views' . DS .'display.html',
  299. 'vars' => ['name' => 'ThinkPHP5'],
  300. 'config' => []
  301. ];
  302. $expected = 'ThinkPHP5';
  303. $provideData[] = [$this->template, $expected];
  304. $this->template = [
  305. 'template' => 'views@display',
  306. 'vars' => [],
  307. 'config' => [
  308. 'view_suffix' => 'html'
  309. ]
  310. ];
  311. $expected = 'default';
  312. $provideData[] = [$this->template, $expected];
  313. $this->template = [
  314. 'template' => 'views@/display',
  315. 'vars' => ['name' => 'ThinkPHP5'],
  316. 'config' => [
  317. 'view_suffix' => 'phtml'
  318. ]
  319. ];
  320. $expected = 'ThinkPHP5';
  321. $provideData[] = [$this->template, $expected];
  322. $this->template = [
  323. 'template' => 'display',
  324. 'vars' => ['name' => 'ThinkPHP5'],
  325. 'config' => [
  326. 'view_suffix' => 'html',
  327. 'view_base' => APP_PATH . 'views' . DS
  328. ]
  329. ];
  330. $expected = 'ThinkPHP5';
  331. $provideData[] = [$this->template, $expected];
  332. return $provideData;
  333. }
  334. public function provideTestLayout()
  335. {
  336. $provideData = [];
  337. $data = ['name' => false, 'replace' => ''];
  338. $expected = ['layout_on' => false, 'layout_name' => 'layout', 'layout_item' => '{__CONTENT__}'];
  339. $provideData[] = [$data, $expected];
  340. $data = ['name' => null, 'replace' => ''];
  341. $expected = ['layout_on' => true, 'layout_name' => 'layout', 'layout_item' => '{__CONTENT__}'];
  342. $provideData[] = [$data, $expected];
  343. $data = ['name' => 'ThinkName', 'replace' => 'ThinkReplace'];
  344. $expected = ['layout_on' => true, 'layout_name' => 'ThinkName', 'layout_item' => 'ThinkReplace'];
  345. $provideData[] = [$data, $expected];
  346. return $provideData;
  347. }
  348. }