test_template.php 12 KB

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