test_template.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. function assert_is_html_node($node, $content) {
  95. $this->assertEquals('html', $node->get_name());
  96. $this->assertEquals($content, str_replace("\r\n", "\n", $node->get('content')));
  97. $this->assertEquals(array(), $node->get_children());
  98. }
  99. function assert_is_block_node($node, $block_name, $child_count) {
  100. $this->assertEquals('block', $node->get_name());
  101. $this->assertSame($block_name, $node->get('name'));
  102. $this->assertNull($node->get('content'));
  103. $this->assertEquals($child_count, count($node->get_children()));
  104. }
  105. function assert_is_exp_node($node, $brackets_content) {
  106. $this->assertEquals('expression', $node->get_name());
  107. $this->assertEquals($brackets_content, $node->get('content'));
  108. $this->assertEquals(array(), $node->get_children());
  109. }
  110. function test_parse_blocks_simple() {
  111. $root_block = $this->get_property($this->tpl, 'root_block');
  112. $this->assert_is_block_node($root_block, null, 1);
  113. list($child) = $root_block->get_children();
  114. $this->assert_is_html_node($child, 'test');
  115. }
  116. /**
  117. * @depends test_parse_blocks_simple
  118. */
  119. function test_parse_blocks_blocks() {
  120. $tpl = new Template('blocks');
  121. $root_block = $this->get_property($tpl, 'root_block');
  122. $this->assert_is_block_node($root_block, null, 3);
  123. list($before, $foo, $after) = $root_block->get_children();
  124. $this->assert_is_html_node($before, '');
  125. $this->assert_is_block_node($foo, 'foo', 3);
  126. $this->assert_is_html_node($after, '');
  127. list($foofoo, $bar, $foobaz) = $foo->get_children();
  128. $this->assert_is_html_node($foofoo, "\nfoofoo\n\t");
  129. $this->assert_is_block_node($bar, 'bar', 1);
  130. $this->assert_is_html_node($foobaz, "\nfoobaz\n");
  131. list($foobar) = $bar->get_children();
  132. $this->assert_is_html_node($foobar, "\n\tfoobar\n\t");
  133. }
  134. /**
  135. * @depends test_parse_blocks_blocks
  136. * @expectedException WebBasics\ParseError
  137. * @expectedExceptionMessage Parse error in file tests/_files/templates/unexpected_end.tpl, line 5: unexpected {end}
  138. */
  139. function test_parse_blocks_unexpected_end() {
  140. new Template('unexpected_end');
  141. }
  142. /**
  143. * @depends test_parse_blocks_blocks
  144. * @expectedException WebBasics\ParseError
  145. * @expectedExceptionMessage Parse error in file tests/_files/templates/missing_end.tpl, line 6: missing {end}
  146. */
  147. function test_parse_blocks_missing_end() {
  148. new Template('missing_end');
  149. }
  150. /**
  151. * @depends test_parse_blocks_simple
  152. */
  153. function test_parse_blocks_variables() {
  154. $tpl = new Template('variables');
  155. $root_block = $this->get_property($tpl, 'root_block');
  156. $this->assert_is_block_node($root_block, null, 5);
  157. list($foo, $foobar, $bar, $foobaz, $baz) = $root_block->get_children();
  158. $this->assert_is_html_node($foo, "foo\n");
  159. $this->assert_is_exp_node($foobar, '$foobar');
  160. $this->assert_is_html_node($bar, "\nbar\n");
  161. $this->assert_is_exp_node($foobaz, 'strtolower($foobaz)');
  162. $this->assert_is_html_node($baz, "\nbaz");
  163. }
  164. /**
  165. * @depends test_parse_blocks_blocks
  166. * @depends test_parse_blocks_variables
  167. */
  168. function test_parse_blocks_full() {
  169. $tpl = new Template('full');
  170. $root_block = $this->get_property($tpl, 'root_block');
  171. $this->assert_is_block_node($root_block, null, 3);
  172. list($bar, $foo, $baz) = $root_block->get_children();
  173. $this->assert_is_html_node($bar, "bar\n");
  174. $this->assert_is_block_node($foo, 'foo', 5);
  175. $this->assert_is_html_node($baz, "\nbaz");
  176. list($foofoo, $bar, $first_space, $foobaz, $second_space) = $foo->get_children();
  177. $this->assert_is_html_node($foofoo, "\nfoofoo\n\t");
  178. $this->assert_is_block_node($bar, 'bar', 3);
  179. $this->assert_is_html_node($first_space, "\n");
  180. $this->assert_is_exp_node($foobaz, 'strtolower($foobaz)');
  181. $this->assert_is_html_node($second_space, "\n");
  182. list($space_before, $foobar, $space_after) = $bar->get_children();
  183. $this->assert_is_html_node($space_before, "\n\t");
  184. $this->assert_is_exp_node($foobar, '$foobar');
  185. $this->assert_is_html_node($space_after, "\n\t");
  186. }
  187. function evaluate_expression() {
  188. $args = func_get_args();
  189. $eval = new ReflectionMethod('WebBasics\Template', 'evaluate_expression');
  190. $eval->setAccessible(true);
  191. return $eval->invokeArgs(null, $args);
  192. }
  193. function assert_evaluates($expected, $expression) {
  194. $this->assertEquals($expected, $this->evaluate_expression($expression, $this->data));
  195. }
  196. /**
  197. * @expectedException \UnexpectedValueException
  198. */
  199. function test_evaluate_variable_attribute_null() {
  200. $this->evaluate_expression('$foobarbaz.foo', $this->data);
  201. }
  202. /**
  203. * @expectedException \UnexpectedValueException
  204. */
  205. function test_evaluate_variable_attribute_no_such_attribute() {
  206. $this->evaluate_expression('$object.foobar', $this->data);
  207. }
  208. /**
  209. * @expectedException \UnexpectedValueException
  210. */
  211. function test_evaluate_variable_attribute_no_array_or_object() {
  212. $this->evaluate_expression('$foo.bar', $this->data);
  213. }
  214. /**
  215. * @expectedException \UnexpectedValueException
  216. */
  217. function test_evaluate_variable_method_null() {
  218. $this->evaluate_expression('$foobarbaz.foo()', $this->data);
  219. }
  220. /**
  221. * @expectedException \BadMethodCallException
  222. */
  223. function test_evaluate_variable_method_no_such_method() {
  224. $this->evaluate_expression('$object.foo()', $this->data);
  225. }
  226. /**
  227. * @expectedException \BadMethodCallException
  228. */
  229. function test_evaluate_variable_method_no_object() {
  230. $this->evaluate_expression('$foo.bar()', $this->data);
  231. }
  232. function test_evaluate_variable_success() {
  233. $this->assert_evaluates('bar', '$array.foo');
  234. $this->assert_evaluates('bar', '$foo');
  235. $this->assert_evaluates('baz', '$bar');
  236. $this->assert_evaluates('bar', '$object.foo');
  237. $this->assert_evaluates('baz', '$object.bar');
  238. $this->assert_evaluates('foobar', '$object.baz()');
  239. }
  240. function test_evaluate_constant() {
  241. $this->assert_evaluates('foobar_const', 'FOOBAR');
  242. $this->assert_evaluates('{NON_DEFINED_CONST}', 'NON_DEFINED_CONST');
  243. }
  244. function test_evaluate_no_expression() {
  245. $this->assert_evaluates('{foo}', 'foo');
  246. }
  247. function test_evaluate_condition_if() {
  248. $this->assert_evaluates('bar', '$true?bar');
  249. $this->assert_evaluates('', '$false?bar');
  250. }
  251. function test_evaluate_condition_if_else() {
  252. $this->assert_evaluates('bar', '$true?bar:baz');
  253. $this->assert_evaluates('baz', '$false?bar:baz');
  254. }
  255. /**
  256. * @depends test_evaluate_condition_if
  257. * @depends test_evaluate_condition_if_else
  258. */
  259. function test_evaluate_condition_spaces() {
  260. $this->assert_evaluates(' bar ', '$true? bar : baz');
  261. $this->assert_evaluates(' baz', '$false? bar : baz');
  262. $this->assert_evaluates(' bar ', '$true ? bar : baz');
  263. $this->assert_evaluates(' baz', '$false ? bar : baz');
  264. $this->assert_evaluates(' Foo bar ', '$true ? Foo bar : Baz foo');
  265. $this->assert_evaluates(' Baz foo', '$false ? Foo bar : Baz foo');
  266. }
  267. /**
  268. * @expectedException \BadFunctionCallException
  269. */
  270. function test_evaluate_function_error() {
  271. $this->evaluate_expression('undefined_function($foo)', $this->data);
  272. }
  273. function test_evaluate_function_success() {
  274. $this->assert_evaluates('Bar', 'ucfirst($foo)');
  275. $this->assert_evaluates('Bar', 'DataObject::foobar($foo)');
  276. }
  277. /**
  278. * @depends test_evaluate_function_success
  279. */
  280. function test_evaluate_function_nested() {
  281. $this->assert_evaluates('Bar', 'ucfirst(strtolower($FOO))');
  282. }
  283. /**
  284. * @depends test_evaluate_variable_success
  285. * @depends test_evaluate_no_expression
  286. * @depends test_evaluate_condition_spaces
  287. * @depends test_evaluate_function_success
  288. */
  289. function test_evaluate_expression_combined() {
  290. $this->assert_evaluates('Bar', '$true?ucfirst($foo)');
  291. $this->assert_evaluates('', '$false?ucfirst($foo)');
  292. $this->assert_evaluates('Bar', '$true?ucfirst($foo):baz');
  293. $this->assert_evaluates('baz', '$false?ucfirst($foo):baz');
  294. $this->assert_evaluates('Baz', 'ucfirst($array.bar)');
  295. }
  296. function assert_renders($expected_file, $tpl) {
  297. $expected_file = "tests/_files/rendered/$expected_file.html";
  298. $this->assertStringEqualsFile($expected_file, $tpl->render());
  299. }
  300. function test_render_simple() {
  301. $this->assertEquals('test', $this->tpl->render());
  302. }
  303. /**
  304. * @depends test_evaluate_expression_combined
  305. */
  306. function test_render_variable() {
  307. $tpl = new Template('variables');
  308. $tpl->set(array(
  309. 'foobar' => 'my_foobar_variable',
  310. 'foobaz' => 'MY_FOOBAZ_VARIABLE'
  311. ));
  312. $this->assert_renders('variables', $tpl);
  313. }
  314. /**
  315. * @depends test_render_simple
  316. */
  317. function test_render_blocks() {
  318. $tpl = new Template('blocks');
  319. $foo = $tpl->add('foo');
  320. $foo->add('bar');
  321. $foo->add('bar');
  322. $tpl->add('foo');
  323. $this->assert_renders('blocks', $tpl);
  324. }
  325. /**
  326. * @depends test_render_variable
  327. * @depends test_render_blocks
  328. */
  329. function test_render_full() {
  330. $tpl = new Template('full');
  331. $first_foo = $tpl->add('foo')->set('foobaz', 'FIRST_FOOBAZ_VAR');
  332. $first_foo->add('bar')->set('foobar', 'first_foobar_var');
  333. $second_foo = $tpl->add('foo')->set('foobaz', 'SECOND_FOOBAZ_VAR');
  334. $second_foo->add('bar')->set('foobar', 'second_foobar_var');
  335. $second_foo->add('bar')->set('foobar', 'third_foobar_var');
  336. $this->assert_renders('full', $tpl);
  337. }
  338. }
  339. ?>