test_template.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. <?php
  2. require_once 'template.php';
  3. use webbasics\Template;
  4. use webbasics\Node;
  5. define('TEMPLATES_DIR', 'tests/_files/templates/');
  6. define('FOOBAR', 'foobar_const');
  7. class DataObject {
  8. var $foo = 'bar';
  9. var $bar = 'baz';
  10. function baz() {
  11. return 'foobar';
  12. }
  13. static function foobar($param) {
  14. return ucfirst($param);
  15. }
  16. }
  17. class TemplateTest extends PHPUnit_Framework_TestCase {
  18. const INTERNATIONALIZATION_STRING = 'Iñtërnâtiônàlizætiøn';
  19. /**
  20. * @depends testAddRootSuccess
  21. */
  22. function setUp() {
  23. Template::setRoot(TEMPLATES_DIR);
  24. $this->tpl = new Template('foo');
  25. $this->data = new Node();
  26. $object = new stdClass();
  27. $object->foo = 'bar';
  28. $object->bar = 'baz';
  29. $this->data->set(array(
  30. 'foo' => 'bar',
  31. 'bar' => 'baz',
  32. 'FOO' => 'BAR',
  33. 'true' => true,
  34. 'false' => false,
  35. 'array' => array('foo' => 'bar', 'bar' => 'baz'),
  36. 'object' => new DataObject,
  37. 'foobar' => 'my_foobar_variable',
  38. 'foobaz' => 'MY_FOOBAZ_VARIABLE',
  39. 'html' => '<script></script>',
  40. 'internationalization' => self::INTERNATIONALIZATION_STRING,
  41. ));
  42. }
  43. /**
  44. * @expectedException webbasics\FileNotFoundError
  45. * @expectedExceptionMessage Directory "non_existing_folder/" does not exist.
  46. */
  47. function testAddRootFailure() {
  48. Template::addRoot('non_existing_folder');
  49. }
  50. function assertIncludePathEquals($expected) {
  51. $include_path = new ReflectionProperty('webbasics\Template', 'include_path');
  52. $include_path->setAccessible(true);
  53. $this->assertEquals($expected, $include_path->getValue());
  54. }
  55. function testClearIncludePath() {
  56. Template::clearIncludePath();
  57. $this->assertIncludePathEquals(array());
  58. }
  59. /**
  60. * @depends testClearIncludePath
  61. */
  62. function testAddRootSuccess() {
  63. Template::clearIncludePath();
  64. Template::addRoot(TEMPLATES_DIR);
  65. $this->assertIncludePathEquals(array(TEMPLATES_DIR));
  66. Template::addRoot('tests/_files');
  67. $this->assertIncludePathEquals(array(TEMPLATES_DIR, 'tests/_files/'));
  68. }
  69. /**
  70. * @depends testAddRootSuccess
  71. */
  72. function testSetRoot() {
  73. Template::clearIncludePath();
  74. Template::addRoot(TEMPLATES_DIR);
  75. Template::addRoot('tests/_files');
  76. Template::setRoot(TEMPLATES_DIR);
  77. $this->assertIncludePathEquals(array(TEMPLATES_DIR));
  78. }
  79. /**
  80. * @expectedException webbasics\FormattedException
  81. */
  82. function testNonExistingTemplate() {
  83. $bar = new Template('bar');
  84. }
  85. function testOtherRoot() {
  86. Template::addRoot('tests/_files/other_templates');
  87. new Template('bar');
  88. }
  89. function testGetPath() {
  90. $this->assertEquals(TEMPLATES_DIR.'foo.tpl', $this->tpl->getPath());
  91. }
  92. function getProperty($object, $property_name) {
  93. $rp = new ReflectionProperty($object, $property_name);
  94. $rp->setAccessible(true);
  95. return $rp->getValue($object);
  96. }
  97. static function stripNewlines($html) {
  98. return str_replace("\r\n", "\n", $html);
  99. }
  100. function assertIsHtmlNode($node, $content) {
  101. $this->assertEquals('html', $node->getName());
  102. $this->assertEquals($content, self::stripNewlines($node->get('content')));
  103. $this->assertEquals(array(), $node->getChildren());
  104. }
  105. function assertIsBlockNode($node, $block_name, $child_count) {
  106. $this->assertEquals('block', $node->getName());
  107. $this->assertSame($block_name, $node->get('name'));
  108. $this->assertNull($node->get('content'));
  109. $this->assertEquals($child_count, count($node->getChildren()));
  110. }
  111. function assertIsExpNode($node, $brackets_content) {
  112. $this->assertEquals('expression', $node->getName());
  113. $this->assertEquals($brackets_content, $node->get('content'));
  114. $this->assertEquals(array(), $node->getChildren());
  115. }
  116. function testParseBlocksSimple() {
  117. $root_block = $this->getProperty($this->tpl, 'root_block');
  118. $this->assertIsBlockNode($root_block, null, 1);
  119. list($child) = $root_block->getChildren();
  120. $this->assertIsHtmlNode($child, 'test');
  121. }
  122. /**
  123. * @depends testParseBlocksSimple
  124. */
  125. function testParseBlocksBlocks() {
  126. $tpl = new Template('blocks');
  127. $root_block = $this->getProperty($tpl, 'root_block');
  128. $this->assertIsBlockNode($root_block, null, 2);
  129. list($before, $foo) = $root_block->getChildren();
  130. $this->assertIsHtmlNode($before, '');
  131. $this->assertIsBlockNode($foo, 'foo', 3);
  132. list($foofoo, $bar, $foobaz) = $foo->getChildren();
  133. $this->assertIsHtmlNode($foofoo, "\nfoofoo\n\t");
  134. $this->assertIsBlockNode($bar, 'bar', 1);
  135. $this->assertIsHtmlNode($foobaz, "\nfoobaz\n");
  136. list($foobar) = $bar->getChildren();
  137. $this->assertIsHtmlNode($foobar, "\n\tfoobar\n\t");
  138. }
  139. /**
  140. * @depends testParseBlocksBlocks
  141. * @expectedException webbasics\ParseError
  142. * @expectedExceptionMessage Parse error in file tests/_files/templates/unexpected_end.tpl, line 5: unexpected {end}
  143. */
  144. function testParseBlocksUnexpectedEnd() {
  145. new Template('unexpected_end');
  146. }
  147. /**
  148. * @depends testParseBlocksBlocks
  149. * @expectedException webbasics\ParseError
  150. * @expectedExceptionMessage Parse error in file tests/_files/templates/missing_end.tpl, line 6: missing {end}
  151. */
  152. function testParseBlocksMissingEnd() {
  153. new Template('missing_end');
  154. }
  155. /**
  156. * @depends testParseBlocksSimple
  157. */
  158. function testParseBlocksVariables() {
  159. $tpl = new Template('variables');
  160. $root_block = $this->getProperty($tpl, 'root_block');
  161. $this->assertIsBlockNode($root_block, null, 5);
  162. list($foo, $foobar, $bar, $foobaz, $baz) = $root_block->getChildren();
  163. $this->assertIsHtmlNode($foo, "foo\n");
  164. $this->assertIsExpNode($foobar, '$foobar');
  165. $this->assertIsHtmlNode($bar, "\nbar\n");
  166. $this->assertIsExpNode($foobaz, 'strtolower($foobaz)');
  167. $this->assertIsHtmlNode($baz, "\nbaz\n{\nno_variable\n}");
  168. }
  169. /**
  170. * @depends testParseBlocksBlocks
  171. * @depends testParseBlocksVariables
  172. */
  173. function testParseBlocksFull() {
  174. $tpl = new Template('full');
  175. $root_block = $this->getProperty($tpl, 'root_block');
  176. $this->assertIsBlockNode($root_block, null, 3);
  177. list($bar, $foo, $baz) = $root_block->getChildren();
  178. $this->assertIsHtmlNode($bar, "bar\n");
  179. $this->assertIsBlockNode($foo, 'foo', 5);
  180. $this->assertIsHtmlNode($baz, "\nbaz");
  181. list($foofoo, $bar, $first_space, $foobaz, $second_space) = $foo->getChildren();
  182. $this->assertIsHtmlNode($foofoo, "\nfoofoo\n\t");
  183. $this->assertIsBlockNode($bar, 'bar', 3);
  184. $this->assertIsHtmlNode($first_space, "\n");
  185. $this->assertIsExpNode($foobaz, 'strtolower($foobaz)');
  186. $this->assertIsHtmlNode($second_space, "\n");
  187. list($space_before, $foobar, $space_after) = $bar->getChildren();
  188. $this->assertIsHtmlNode($space_before, "\n\t");
  189. $this->assertIsExpNode($foobar, '$foobar');
  190. $this->assertIsHtmlNode($space_after, "\n\t");
  191. }
  192. function evaluateExpression() {
  193. $args = func_get_args();
  194. $eval = new ReflectionMethod('webbasics\Template', 'evaluateExpression');
  195. $eval->setAccessible(true);
  196. return $eval->invokeArgs(null, $args);
  197. }
  198. function assertEvaluates($expected, $expression) {
  199. $this->assertEquals($expected, $this->evaluateExpression($expression, $this->data));
  200. }
  201. /**
  202. * @expectedException \UnexpectedValueException
  203. */
  204. function testEvaluateVariableAttributeNull() {
  205. $this->evaluateExpression('$foobarbaz.foo', $this->data);
  206. }
  207. /**
  208. * @expectedException \UnexpectedValueException
  209. */
  210. function testEvaluateVariableAttributeNoSuchAttribute() {
  211. $this->evaluateExpression('$object.foobar', $this->data);
  212. }
  213. /**
  214. * @expectedException \UnexpectedValueException
  215. */
  216. function testEvaluateVariableAttributeNoArrayOrObject() {
  217. $this->evaluateExpression('$foo.bar', $this->data);
  218. }
  219. /**
  220. * @expectedException \UnexpectedValueException
  221. */
  222. function testEvaluateVariableMethodNull() {
  223. $this->evaluateExpression('$foobarbaz.foo()', $this->data);
  224. }
  225. /**
  226. * @expectedException \BadMethodCallException
  227. */
  228. function testEvaluateVariableMethodNoSuchMethod() {
  229. $this->evaluateExpression('$object.foo()', $this->data);
  230. }
  231. /**
  232. * @expectedException \BadMethodCallException
  233. */
  234. function testEvaluateVariableMethodNoObject() {
  235. $this->evaluateExpression('$foo.bar()', $this->data);
  236. }
  237. function testEvaluateVariableSuccess() {
  238. $this->assertEvaluates('bar', '$array.foo');
  239. $this->assertEvaluates('bar', '$foo');
  240. $this->assertEvaluates('baz', '$bar');
  241. $this->assertEvaluates('bar', '$object.foo');
  242. $this->assertEvaluates('baz', '$object.bar');
  243. $this->assertEvaluates('foobar', '$object.baz()');
  244. }
  245. /**
  246. * @depends testEvaluateVariableSuccess
  247. */
  248. function testEvaluateVariableEscape() {
  249. $this->assertEvaluates('&lt;script&gt;&lt;/script&gt;', '$html');
  250. $this->assertEvaluates('Iñtërnâtiônàlizætiøn', '$internationalization');
  251. //$this->assertEvaluates('I&ntilde;t&euml;rn&acirc;ti&ocirc;n&agrave;liz&aelig;ti&oslash;n', '$internationalization');
  252. }
  253. /**
  254. * @depends testEvaluateVariableSuccess
  255. */
  256. function testEvaluateVariableNoescape() {
  257. $this->assertEvaluates('<script></script>', '$$html');
  258. $this->assertEvaluates('Iñtërnâtiônàlizætiøn', '$$internationalization');
  259. }
  260. function testEvaluateConstant() {
  261. $this->assertEvaluates('foobar_const', 'FOOBAR');
  262. $this->assertEvaluates('{NON_DEFINED_CONST}', 'NON_DEFINED_CONST');
  263. }
  264. function testEvaluateNoExpression() {
  265. $this->assertEvaluates('{foo}', 'foo');
  266. }
  267. function testEvaluateConditionIf() {
  268. $this->assertEvaluates('bar', '$true?bar');
  269. $this->assertEvaluates('', '$false?bar');
  270. }
  271. function testEvaluateConditionIfElse() {
  272. $this->assertEvaluates('bar', '$true?bar:baz');
  273. $this->assertEvaluates('baz', '$false?bar:baz');
  274. }
  275. /**
  276. * @depends testEvaluateConditionIf
  277. * @depends testEvaluateConditionIfElse
  278. */
  279. function testEvaluateConditionExtended() {
  280. $this->assertEvaluates(' bar ', '$true? bar : baz');
  281. $this->assertEvaluates(' baz', '$false? bar : baz');
  282. $this->assertEvaluates(' bar ', '$true ? bar : baz');
  283. $this->assertEvaluates(' baz', '$false ? bar : baz');
  284. $this->assertEvaluates(' Foo bar ', '$true ? Foo bar : Baz foo');
  285. $this->assertEvaluates(' Baz foo', '$false ? Foo bar : Baz foo');
  286. $this->assertEvaluates('| bar', '$true ?| $foo');
  287. }
  288. /**
  289. * @expectedException \BadFunctionCallException
  290. */
  291. function testEvaluateFunctionError() {
  292. $this->evaluateExpression('undefined_function($foo)', $this->data);
  293. }
  294. function testEvaluateFunctionSuccess() {
  295. $this->assertEvaluates('Bar', 'ucfirst($foo)');
  296. $this->assertEvaluates('Bar', 'DataObject::foobar($foo)');
  297. }
  298. /**
  299. * @depends testEvaluateFunctionSuccess
  300. */
  301. function testEvaluateFunctionNested() {
  302. $this->assertEvaluates('Bar', 'ucfirst(strtolower($FOO))');
  303. }
  304. function testEvaluateDefaultValue() {
  305. $this->assertEvaluates('bar', '$foo||fallback');
  306. $this->assertEvaluates('fallback', '$foo.bar||fallback');
  307. $this->assertEvaluates('', '$foo.bar||');
  308. }
  309. /**
  310. * @depends testEvaluateVariableSuccess
  311. * @depends testEvaluateNoExpression
  312. * @depends testEvaluateConditionExtended
  313. * @depends testEvaluateFunctionSuccess
  314. * @depends testEvaluateDefaultValue
  315. */
  316. function testEvaluateExpressionCombined() {
  317. $this->assertEvaluates('Bar', '$true?ucfirst($foo)');
  318. $this->assertEvaluates('', '$false?ucfirst($foo)');
  319. $this->assertEvaluates('Bar', '$true?ucfirst($foo):baz');
  320. $this->assertEvaluates('baz', '$false?ucfirst($foo):baz');
  321. $this->assertEvaluates('Baz', 'ucfirst($array.bar)');
  322. }
  323. function assertRenders($expected_file, $tpl) {
  324. $expected_file = "tests/_files/rendered/$expected_file.html";
  325. $this->assertEquals(self::stripNewlines(file_get_contents($expected_file)),
  326. self::stripNewlines($tpl->render()));
  327. }
  328. function testRenderSimple() {
  329. $this->assertEquals('test', $this->tpl->render());
  330. }
  331. /**
  332. * @depends testEvaluateExpressionCombined
  333. */
  334. function testRenderVariable() {
  335. $tpl = new Template('variables');
  336. $tpl->set(array(
  337. 'foobar' => 'my_foobar_variable',
  338. 'foobaz' => 'MY_FOOBAZ_VARIABLE'
  339. ));
  340. $this->assertRenders('variables', $tpl);
  341. }
  342. /**
  343. * @depends testRenderSimple
  344. */
  345. function testRenderBlocks() {
  346. $tpl = new Template('blocks');
  347. $foo = $tpl->add('foo');
  348. $foo->add('bar');
  349. $foo->add('bar');
  350. $tpl->add('foo');
  351. $this->assertRenders('blocks', $tpl);
  352. }
  353. /**
  354. * @depends testRenderVariable
  355. * @depends testRenderBlocks
  356. */
  357. function testRenderFull() {
  358. $tpl = new Template('full');
  359. $first_foo = $tpl->add('foo')->set('foobaz', 'FIRST_FOOBAZ_VAR');
  360. $first_foo->add('bar')->set('foobar', 'first_foobar_var');
  361. $second_foo = $tpl->add('foo')->set('foobaz', 'SECOND_FOOBAZ_VAR');
  362. $second_foo->add('bar')->set('foobar', 'second_foobar_var');
  363. $second_foo->add('bar')->set('foobar', 'third_foobar_var');
  364. $this->assertRenders('full', $tpl);
  365. }
  366. }
  367. ?>