test_template.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. <?php
  2. require_once 'template.php';
  3. use WebBasics\Template;
  4. use WebBasics\Node;
  5. define('TEMPLATES_DIR', 'tests/_files/templates/');
  6. class TemplateTest extends PHPUnit_Framework_TestCase {
  7. /**
  8. * @depends test_add_root_success
  9. */
  10. function setUp() {
  11. Template::set_root(TEMPLATES_DIR);
  12. $this->tpl = new Template('foo');
  13. $this->data = new Node();
  14. $object = new stdClass();
  15. $object->foo = 'bar';
  16. $object->bar = 'baz';
  17. $this->data->set(array(
  18. 'foo' => 'bar',
  19. 'bar' => 'baz',
  20. 'FOO' => 'BAR',
  21. 'true' => true,
  22. 'false' => false,
  23. 'array' => array('foo' => 'bar', 'bar' => 'baz'),
  24. 'object' => $object,
  25. 'foobar' => 'my_foobar_variable',
  26. 'foobaz' => 'MY_FOOBAZ_VARIABLE',
  27. ));
  28. }
  29. /**
  30. * @expectedException WebBasics\FileNotFoundError
  31. * @expectedExceptionMessage Directory "non_existing_folder/" does not exist.
  32. */
  33. function test_add_root_failure() {
  34. Template::add_root('non_existing_folder');
  35. }
  36. function assert_include_path_equals($expected) {
  37. $include_path = new ReflectionProperty('WebBasics\Template', 'include_path');
  38. $include_path->setAccessible(true);
  39. $this->assertEquals($expected, $include_path->getValue());
  40. }
  41. function test_clear_include_path() {
  42. Template::clear_include_path();
  43. $this->assert_include_path_equals(array());
  44. }
  45. /**
  46. * @depends test_clear_include_path
  47. */
  48. function test_add_root_success() {
  49. Template::clear_include_path();
  50. Template::add_root(TEMPLATES_DIR);
  51. $this->assert_include_path_equals(array(TEMPLATES_DIR));
  52. Template::add_root('tests/_files');
  53. $this->assert_include_path_equals(array(TEMPLATES_DIR, 'tests/_files/'));
  54. }
  55. /**
  56. * @depends test_add_root_success
  57. */
  58. function test_set_root() {
  59. Template::clear_include_path();
  60. Template::add_root(TEMPLATES_DIR);
  61. Template::add_root('tests/_files');
  62. Template::set_root(TEMPLATES_DIR);
  63. $this->assert_include_path_equals(array(TEMPLATES_DIR));
  64. }
  65. /**
  66. * @expectedException RuntimeException
  67. */
  68. function test_non_existing_template() {
  69. $bar = new Template('bar');
  70. }
  71. function test_other_root() {
  72. Template::add_root('tests/_files/other_templates');
  73. new Template('bar');
  74. }
  75. function test_get_path() {
  76. $this->assertEquals(TEMPLATES_DIR.'foo.tpl', $this->tpl->get_path());
  77. }
  78. function get_property($object, $property_name) {
  79. $rp = new ReflectionProperty($object, $property_name);
  80. $rp->setAccessible(true);
  81. return $rp->getValue($object);
  82. }
  83. function test_parse_blocks_simple() {
  84. $root_block = $this->get_property($this->tpl, 'root_block');
  85. $this->assert_is_block_node($root_block, null, 1);
  86. list($child) = $root_block->get_children();
  87. $this->assert_is_html_node($child, 'test');
  88. }
  89. function assert_is_html_node($node, $content) {
  90. $this->assertEquals('html', $node->get_name());
  91. $this->assertEquals($content, $node->get('content'));
  92. $this->assertEquals(array(), $node->get_children());
  93. }
  94. function assert_is_block_node($node, $block_name, $child_count) {
  95. $this->assertEquals('block', $node->get_name());
  96. $this->assertSame($block_name, $node->get('name'));
  97. $this->assertNull($node->get('content'));
  98. $this->assertEquals($child_count, count($node->get_children()));
  99. }
  100. function assert_is_variable_node($node, $brackets_content) {
  101. $this->assertEquals('variable', $node->get_name());
  102. $this->assertEquals($brackets_content, $node->get('content'));
  103. $this->assertEquals(array(), $node->get_children());
  104. }
  105. /**
  106. * @depends test_parse_blocks_simple
  107. */
  108. function test_parse_blocks_blocks() {
  109. $tpl = new Template('blocks');
  110. $root_block = $this->get_property($tpl, 'root_block');
  111. $this->assert_is_block_node($root_block, null, 3);
  112. list($before, $foo, $after) = $root_block->get_children();
  113. $this->assert_is_html_node($before, '');
  114. $this->assert_is_block_node($foo, 'foo', 3);
  115. $this->assert_is_html_node($after, '');
  116. list($foofoo, $bar, $foobaz) = $foo->get_children();
  117. $this->assert_is_html_node($foofoo, "\nfoofoo\n\t");
  118. $this->assert_is_block_node($bar, 'bar', 1);
  119. $this->assert_is_html_node($foobaz, "\nfoobaz\n");
  120. list($foobar) = $bar->get_children();
  121. $this->assert_is_html_node($foobar, "\n\tfoobar\n\t");
  122. }
  123. /**
  124. * @depends test_parse_blocks_blocks
  125. * @expectedException WebBasics\ParseError
  126. * @expectedExceptionMessage Parse error in file tests/_files/templates/unexpected_end.tpl, line 5: unexpected {end}
  127. */
  128. function test_parse_blocks_unexpected_end() {
  129. new Template('unexpected_end');
  130. }
  131. /**
  132. * @depends test_parse_blocks_blocks
  133. * @expectedException WebBasics\ParseError
  134. * @expectedExceptionMessage Parse error in file tests/_files/templates/missing_end.tpl, line 6: missing {end}
  135. */
  136. function test_parse_blocks_missing_end() {
  137. new Template('missing_end');
  138. }
  139. /**
  140. * @depends test_parse_blocks_simple
  141. */
  142. function test_parse_blocks_variables() {
  143. $tpl = new Template('variables');
  144. $root_block = $this->get_property($tpl, 'root_block');
  145. $this->assert_is_block_node($root_block, null, 5);
  146. list($foo, $foobar, $bar, $foobaz, $baz) = $root_block->get_children();
  147. $this->assert_is_html_node($foo, "foo\n");
  148. $this->assert_is_variable_node($foobar, 'foobar');
  149. $this->assert_is_html_node($bar, "\nbar\n");
  150. $this->assert_is_variable_node($foobaz, 'foobaz:strtolower');
  151. $this->assert_is_html_node($baz, "\nbaz");
  152. }
  153. /**
  154. * @depends test_parse_blocks_blocks
  155. * @depends test_parse_blocks_variables
  156. */
  157. function test_parse_blocks_full() {
  158. $tpl = new Template('full');
  159. $root_block = $this->get_property($tpl, 'root_block');
  160. $this->assert_is_block_node($root_block, null, 3);
  161. list($bar, $foo, $baz) = $root_block->get_children();
  162. $this->assert_is_html_node($bar, "bar\n");
  163. $this->assert_is_block_node($foo, 'foo', 5);
  164. $this->assert_is_html_node($baz, "\nbaz");
  165. list($foofoo, $bar, $first_space, $foobaz, $second_space) = $foo->get_children();
  166. $this->assert_is_html_node($foofoo, "\nfoofoo\n\t");
  167. $this->assert_is_block_node($bar, 'bar', 3);
  168. $this->assert_is_html_node($first_space, "\n");
  169. $this->assert_is_variable_node($foobaz, 'foobaz:strtolower');
  170. $this->assert_is_html_node($second_space, "\n");
  171. list($space_before, $foobar, $space_after) = $bar->get_children();
  172. $this->assert_is_html_node($space_before, "\n\t");
  173. $this->assert_is_variable_node($foobar, 'foobar');
  174. $this->assert_is_html_node($space_after, "\n\t");
  175. }
  176. function assert_replaces($expected, $variable) {
  177. $rm = new ReflectionMethod('WebBasics\Template', 'replace_variable');
  178. $rm->setAccessible(true);
  179. $this->assertEquals($expected, $rm->invoke(null, $variable, $this->data));
  180. }
  181. function test_replace_variable_simple() {
  182. $this->assert_replaces('bar', 'foo');
  183. }
  184. /**
  185. * @depends test_replace_variable_simple
  186. */
  187. function test_replace_variable_helper_functions() {
  188. $this->assert_replaces('Bar', 'foo:ucfirst');
  189. $this->assert_replaces('bar', 'FOO:strtolower');
  190. $this->assert_replaces('Bar', 'FOO:strtolower:ucfirst');
  191. }
  192. /**
  193. * @expectedException UnexpectedValueException
  194. * @expectedExceptionMessage Helper function "idonotexist" is not callable.
  195. */
  196. function test_replace_variable_non_callable_helper_function() {
  197. $this->assert_replaces(null, 'foo:idonotexist');
  198. }
  199. function test_replace_variable_not_found() {
  200. $this->assert_replaces('{idonotexist}', 'idonotexist');
  201. }
  202. function test_replace_variable_associative_array() {
  203. $this->assert_replaces('bar', 'array.foo');
  204. $this->assert_replaces('baz', 'array.bar');
  205. }
  206. function test_replace_variable_object_property() {
  207. $this->assert_replaces('bar', 'object.foo');
  208. $this->assert_replaces('baz', 'object.bar');
  209. }
  210. function test_replace_variable_if_statement() {
  211. $this->assert_replaces('bar', 'if:true:foo');
  212. $this->assert_replaces('', 'if:false:foo');
  213. $this->assert_replaces('bar', 'if:true:foo:else:bar');
  214. $this->assert_replaces('baz', 'if:false:foo:else:bar');
  215. $this->assert_replaces('Bar', 'if:false:foo:else:FOO:strtolower:ucfirst');
  216. }
  217. /*function assert_block_renders($expected_file, $block, $data) {
  218. $rm = new ReflectionMethod('WebBasics\Template', 'render_block');
  219. $rm->setAccessible(true);
  220. $expected_file = "tests/_files/rendered/$expected_file.html";
  221. $this->assertStringEqualsFile($expected_file, $rm->invoke(null, $block, $data));
  222. }*/
  223. function assert_renders($expected_file, $tpl) {
  224. $expected_file = "tests/_files/rendered/$expected_file.html";
  225. $this->assertStringEqualsFile($expected_file, $tpl->render());
  226. }
  227. function test_render_simple() {
  228. $this->assertEquals('test', $this->tpl->render());
  229. }
  230. /**
  231. * @depends test_replace_variable_helper_functions
  232. */
  233. function test_render_variable() {
  234. $tpl = new Template('variables');
  235. $tpl->set(array(
  236. 'foobar' => 'my_foobar_variable',
  237. 'foobaz' => 'MY_FOOBAZ_VARIABLE'
  238. ));
  239. $this->assert_renders('variables', $tpl);
  240. }
  241. /**
  242. * @depends test_render_simple
  243. */
  244. function test_render_blocks() {
  245. $tpl = new Template('blocks');
  246. $foo = $tpl->add('foo');
  247. $foo->add('bar');
  248. $foo->add('bar');
  249. $tpl->add('foo');
  250. $this->assert_renders('blocks', $tpl);
  251. }
  252. /**
  253. * @depends test_render_variable
  254. * @depends test_render_blocks
  255. */
  256. function test_render_full() {
  257. $tpl = new Template('full');
  258. $first_foo = $tpl->add('foo')->set('foobaz', 'FIRST_FOOBAZ_VAR');
  259. $first_foo->add('bar')->set('foobar', 'first_foobar_var');
  260. $second_foo = $tpl->add('foo')->set('foobaz', 'SECOND_FOOBAZ_VAR');
  261. $second_foo->add('bar')->set('foobar', 'second_foobar_var');
  262. $second_foo->add('bar')->set('foobar', 'third_foobar_var');
  263. $this->assert_renders('full', $tpl);
  264. }
  265. }
  266. ?>