test_template.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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 test_add_root_success
  21. */
  22. function setUp() {
  23. Template::set_root(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 test_add_root_failure() {
  48. Template::add_root('non_existing_folder');
  49. }
  50. function assert_include_path_equals($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 test_clear_include_path() {
  56. Template::clear_include_path();
  57. $this->assert_include_path_equals(array());
  58. }
  59. /**
  60. * @depends test_clear_include_path
  61. */
  62. function test_add_root_success() {
  63. Template::clear_include_path();
  64. Template::add_root(TEMPLATES_DIR);
  65. $this->assert_include_path_equals(array(TEMPLATES_DIR));
  66. Template::add_root('tests/_files');
  67. $this->assert_include_path_equals(array(TEMPLATES_DIR, 'tests/_files/'));
  68. }
  69. /**
  70. * @depends test_add_root_success
  71. */
  72. function test_set_root() {
  73. Template::clear_include_path();
  74. Template::add_root(TEMPLATES_DIR);
  75. Template::add_root('tests/_files');
  76. Template::set_root(TEMPLATES_DIR);
  77. $this->assert_include_path_equals(array(TEMPLATES_DIR));
  78. }
  79. /**
  80. * @expectedException RuntimeException
  81. */
  82. function test_non_existing_template() {
  83. $bar = new Template('bar');
  84. }
  85. function test_other_root() {
  86. Template::add_root('tests/_files/other_templates');
  87. new Template('bar');
  88. }
  89. function test_get_path() {
  90. $this->assertEquals(TEMPLATES_DIR.'foo.tpl', $this->tpl->get_path());
  91. }
  92. function get_property($object, $property_name) {
  93. $rp = new ReflectionProperty($object, $property_name);
  94. $rp->setAccessible(true);
  95. return $rp->getValue($object);
  96. }
  97. static function strip_newlines($html) {
  98. return str_replace("\r\n", "\n", $html);
  99. }
  100. function assert_is_html_node($node, $content) {
  101. $this->assertEquals('html', $node->get_name());
  102. $this->assertEquals($content, self::strip_newlines($node->get('content')));
  103. $this->assertEquals(array(), $node->get_children());
  104. }
  105. function assert_is_block_node($node, $block_name, $child_count) {
  106. $this->assertEquals('block', $node->get_name());
  107. $this->assertSame($block_name, $node->get('name'));
  108. $this->assertNull($node->get('content'));
  109. $this->assertEquals($child_count, count($node->get_children()));
  110. }
  111. function assert_is_exp_node($node, $brackets_content) {
  112. $this->assertEquals('expression', $node->get_name());
  113. $this->assertEquals($brackets_content, $node->get('content'));
  114. $this->assertEquals(array(), $node->get_children());
  115. }
  116. function test_parse_blocks_simple() {
  117. $root_block = $this->get_property($this->tpl, 'root_block');
  118. $this->assert_is_block_node($root_block, null, 1);
  119. list($child) = $root_block->get_children();
  120. $this->assert_is_html_node($child, 'test');
  121. }
  122. /**
  123. * @depends test_parse_blocks_simple
  124. */
  125. function test_parse_blocks_blocks() {
  126. $tpl = new Template('blocks');
  127. $root_block = $this->get_property($tpl, 'root_block');
  128. $this->assert_is_block_node($root_block, null, 2);
  129. list($before, $foo) = $root_block->get_children();
  130. $this->assert_is_html_node($before, '');
  131. $this->assert_is_block_node($foo, 'foo', 3);
  132. list($foofoo, $bar, $foobaz) = $foo->get_children();
  133. $this->assert_is_html_node($foofoo, "\nfoofoo\n\t");
  134. $this->assert_is_block_node($bar, 'bar', 1);
  135. $this->assert_is_html_node($foobaz, "\nfoobaz\n");
  136. list($foobar) = $bar->get_children();
  137. $this->assert_is_html_node($foobar, "\n\tfoobar\n\t");
  138. }
  139. /**
  140. * @depends test_parse_blocks_blocks
  141. * @expectedException WebBasics\ParseError
  142. * @expectedExceptionMessage Parse error in file tests/_files/templates/unexpected_end.tpl, line 5: unexpected {end}
  143. */
  144. function test_parse_blocks_unexpected_end() {
  145. new Template('unexpected_end');
  146. }
  147. /**
  148. * @depends test_parse_blocks_blocks
  149. * @expectedException WebBasics\ParseError
  150. * @expectedExceptionMessage Parse error in file tests/_files/templates/missing_end.tpl, line 6: missing {end}
  151. */
  152. function test_parse_blocks_missing_end() {
  153. new Template('missing_end');
  154. }
  155. /**
  156. * @depends test_parse_blocks_simple
  157. */
  158. function test_parse_blocks_variables() {
  159. $tpl = new Template('variables');
  160. $root_block = $this->get_property($tpl, 'root_block');
  161. $this->assert_is_block_node($root_block, null, 5);
  162. list($foo, $foobar, $bar, $foobaz, $baz) = $root_block->get_children();
  163. $this->assert_is_html_node($foo, "foo\n");
  164. $this->assert_is_exp_node($foobar, '$foobar');
  165. $this->assert_is_html_node($bar, "\nbar\n");
  166. $this->assert_is_exp_node($foobaz, 'strtolower($foobaz)');
  167. $this->assert_is_html_node($baz, "\nbaz\n{\nno_variable\n}");
  168. }
  169. /**
  170. * @depends test_parse_blocks_blocks
  171. * @depends test_parse_blocks_variables
  172. */
  173. function test_parse_blocks_full() {
  174. $tpl = new Template('full');
  175. $root_block = $this->get_property($tpl, 'root_block');
  176. $this->assert_is_block_node($root_block, null, 3);
  177. list($bar, $foo, $baz) = $root_block->get_children();
  178. $this->assert_is_html_node($bar, "bar\n");
  179. $this->assert_is_block_node($foo, 'foo', 5);
  180. $this->assert_is_html_node($baz, "\nbaz");
  181. list($foofoo, $bar, $first_space, $foobaz, $second_space) = $foo->get_children();
  182. $this->assert_is_html_node($foofoo, "\nfoofoo\n\t");
  183. $this->assert_is_block_node($bar, 'bar', 3);
  184. $this->assert_is_html_node($first_space, "\n");
  185. $this->assert_is_exp_node($foobaz, 'strtolower($foobaz)');
  186. $this->assert_is_html_node($second_space, "\n");
  187. list($space_before, $foobar, $space_after) = $bar->get_children();
  188. $this->assert_is_html_node($space_before, "\n\t");
  189. $this->assert_is_exp_node($foobar, '$foobar');
  190. $this->assert_is_html_node($space_after, "\n\t");
  191. }
  192. function evaluate_expression() {
  193. $args = func_get_args();
  194. $eval = new ReflectionMethod('WebBasics\Template', 'evaluate_expression');
  195. $eval->setAccessible(true);
  196. return $eval->invokeArgs(null, $args);
  197. }
  198. function assert_evaluates($expected, $expression) {
  199. $this->assertEquals($expected, $this->evaluate_expression($expression, $this->data));
  200. }
  201. /**
  202. * @expectedException \UnexpectedValueException
  203. */
  204. function test_evaluate_variable_attribute_null() {
  205. $this->evaluate_expression('$foobarbaz.foo', $this->data);
  206. }
  207. /**
  208. * @expectedException \UnexpectedValueException
  209. */
  210. function test_evaluate_variable_attribute_no_such_attribute() {
  211. $this->evaluate_expression('$object.foobar', $this->data);
  212. }
  213. /**
  214. * @expectedException \UnexpectedValueException
  215. */
  216. function test_evaluate_variable_attribute_no_array_or_object() {
  217. $this->evaluate_expression('$foo.bar', $this->data);
  218. }
  219. /**
  220. * @expectedException \UnexpectedValueException
  221. */
  222. function test_evaluate_variable_method_null() {
  223. $this->evaluate_expression('$foobarbaz.foo()', $this->data);
  224. }
  225. /**
  226. * @expectedException \BadMethodCallException
  227. */
  228. function test_evaluate_variable_method_no_such_method() {
  229. $this->evaluate_expression('$object.foo()', $this->data);
  230. }
  231. /**
  232. * @expectedException \BadMethodCallException
  233. */
  234. function test_evaluate_variable_method_no_object() {
  235. $this->evaluate_expression('$foo.bar()', $this->data);
  236. }
  237. function test_evaluate_variable_success() {
  238. $this->assert_evaluates('bar', '$array.foo');
  239. $this->assert_evaluates('bar', '$foo');
  240. $this->assert_evaluates('baz', '$bar');
  241. $this->assert_evaluates('bar', '$object.foo');
  242. $this->assert_evaluates('baz', '$object.bar');
  243. $this->assert_evaluates('foobar', '$object.baz()');
  244. }
  245. /**
  246. * @depends test_evaluate_variable_success
  247. */
  248. function test_evaluate_variable_escape() {
  249. $this->assert_evaluates('&lt;script&gt;&lt;/script&gt;', '$html');
  250. $this->assert_evaluates('I&ntilde;t&euml;rn&acirc;ti&ocirc;n&agrave;liz&aelig;ti&oslash;n', '$internationalization');
  251. }
  252. /**
  253. * @depends test_evaluate_variable_success
  254. */
  255. function test_evaluate_variable_noescape() {
  256. $this->assert_evaluates('<script></script>', '$$html');
  257. $this->assert_evaluates('Iñtërnâtiônàlizætiøn', '$$internationalization');
  258. }
  259. function test_evaluate_constant() {
  260. $this->assert_evaluates('foobar_const', 'FOOBAR');
  261. $this->assert_evaluates('{NON_DEFINED_CONST}', 'NON_DEFINED_CONST');
  262. }
  263. function test_evaluate_no_expression() {
  264. $this->assert_evaluates('{foo}', 'foo');
  265. }
  266. function test_evaluate_condition_if() {
  267. $this->assert_evaluates('bar', '$true?bar');
  268. $this->assert_evaluates('', '$false?bar');
  269. }
  270. function test_evaluate_condition_if_else() {
  271. $this->assert_evaluates('bar', '$true?bar:baz');
  272. $this->assert_evaluates('baz', '$false?bar:baz');
  273. }
  274. /**
  275. * @depends test_evaluate_condition_if
  276. * @depends test_evaluate_condition_if_else
  277. */
  278. function test_evaluate_condition_extended() {
  279. $this->assert_evaluates(' bar ', '$true? bar : baz');
  280. $this->assert_evaluates(' baz', '$false? bar : baz');
  281. $this->assert_evaluates(' bar ', '$true ? bar : baz');
  282. $this->assert_evaluates(' baz', '$false ? bar : baz');
  283. $this->assert_evaluates(' Foo bar ', '$true ? Foo bar : Baz foo');
  284. $this->assert_evaluates(' Baz foo', '$false ? Foo bar : Baz foo');
  285. $this->assert_evaluates('| bar', '$true ?| $foo');
  286. }
  287. /**
  288. * @expectedException \BadFunctionCallException
  289. */
  290. function test_evaluate_function_error() {
  291. $this->evaluate_expression('undefined_function($foo)', $this->data);
  292. }
  293. function test_evaluate_function_success() {
  294. $this->assert_evaluates('Bar', 'ucfirst($foo)');
  295. $this->assert_evaluates('Bar', 'DataObject::foobar($foo)');
  296. }
  297. /**
  298. * @depends test_evaluate_function_success
  299. */
  300. function test_evaluate_function_nested() {
  301. $this->assert_evaluates('Bar', 'ucfirst(strtolower($FOO))');
  302. }
  303. function test_evaluate_default_value() {
  304. $this->assert_evaluates('bar', '$foo||fallback');
  305. $this->assert_evaluates('fallback', '$foo.bar||fallback');
  306. $this->assert_evaluates('', '$foo.bar||');
  307. }
  308. /**
  309. * @depends test_evaluate_variable_success
  310. * @depends test_evaluate_no_expression
  311. * @depends test_evaluate_condition_extended
  312. * @depends test_evaluate_function_success
  313. * @depends test_evaluate_default_value
  314. */
  315. function test_evaluate_expression_combined() {
  316. $this->assert_evaluates('Bar', '$true?ucfirst($foo)');
  317. $this->assert_evaluates('', '$false?ucfirst($foo)');
  318. $this->assert_evaluates('Bar', '$true?ucfirst($foo):baz');
  319. $this->assert_evaluates('baz', '$false?ucfirst($foo):baz');
  320. $this->assert_evaluates('Baz', 'ucfirst($array.bar)');
  321. }
  322. function assert_renders($expected_file, $tpl) {
  323. $expected_file = "tests/_files/rendered/$expected_file.html";
  324. $this->assertEquals(self::strip_newlines(file_get_contents($expected_file)),
  325. self::strip_newlines($tpl->render()));
  326. }
  327. function test_render_simple() {
  328. $this->assertEquals('test', $this->tpl->render());
  329. }
  330. /**
  331. * @depends test_evaluate_expression_combined
  332. */
  333. function test_render_variable() {
  334. $tpl = new Template('variables');
  335. $tpl->set(array(
  336. 'foobar' => 'my_foobar_variable',
  337. 'foobaz' => 'MY_FOOBAZ_VARIABLE'
  338. ));
  339. $this->assert_renders('variables', $tpl);
  340. }
  341. /**
  342. * @depends test_render_simple
  343. */
  344. function test_render_blocks() {
  345. $tpl = new Template('blocks');
  346. $foo = $tpl->add('foo');
  347. $foo->add('bar');
  348. $foo->add('bar');
  349. $tpl->add('foo');
  350. $this->assert_renders('blocks', $tpl);
  351. }
  352. /**
  353. * @depends test_render_variable
  354. * @depends test_render_blocks
  355. */
  356. function test_render_full() {
  357. $tpl = new Template('full');
  358. $first_foo = $tpl->add('foo')->set('foobaz', 'FIRST_FOOBAZ_VAR');
  359. $first_foo->add('bar')->set('foobar', 'first_foobar_var');
  360. $second_foo = $tpl->add('foo')->set('foobaz', 'SECOND_FOOBAZ_VAR');
  361. $second_foo->add('bar')->set('foobar', 'second_foobar_var');
  362. $second_foo->add('bar')->set('foobar', 'third_foobar_var');
  363. $this->assert_renders('full', $tpl);
  364. }
  365. }
  366. ?>