test_template.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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 assert_is_html_node($node, $content) {
  84. $this->assertEquals('html', $node->get_name());
  85. $this->assertEquals($content, str_replace("\r\n", "\n", $node->get('content')));
  86. $this->assertEquals(array(), $node->get_children());
  87. }
  88. function assert_is_block_node($node, $block_name, $child_count) {
  89. $this->assertEquals('block', $node->get_name());
  90. $this->assertSame($block_name, $node->get('name'));
  91. $this->assertNull($node->get('content'));
  92. $this->assertEquals($child_count, count($node->get_children()));
  93. }
  94. function assert_is_variable_node($node, $brackets_content) {
  95. $this->assertEquals('variable', $node->get_name());
  96. $this->assertEquals($brackets_content, $node->get('content'));
  97. $this->assertEquals(array(), $node->get_children());
  98. }
  99. function test_parse_blocks_simple() {
  100. $root_block = $this->get_property($this->tpl, 'root_block');
  101. $this->assert_is_block_node($root_block, null, 1);
  102. list($child) = $root_block->get_children();
  103. $this->assert_is_html_node($child, 'test');
  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. * @depends test_replace_variable_helper_functions
  194. */
  195. function test_replace_variable_constant() {
  196. define('FOOCONST', 'foobar');
  197. $this->assert_replaces('foobar', 'FOOCONST');
  198. $this->assert_replaces('FOOBAR', 'FOOCONST:strtoupper');
  199. }
  200. /**
  201. * @expectedException UnexpectedValueException
  202. * @expectedExceptionMessage Helper function "idonotexist" is not callable.
  203. */
  204. function test_replace_variable_non_callable_helper_function() {
  205. $this->assert_replaces(null, 'foo:idonotexist');
  206. }
  207. function test_replace_variable_not_found() {
  208. $this->assert_replaces('{idonotexist}', 'idonotexist');
  209. }
  210. function test_replace_variable_associative_array() {
  211. $this->assert_replaces('bar', 'array.foo');
  212. $this->assert_replaces('baz', 'array.bar');
  213. }
  214. function test_replace_variable_object_property() {
  215. $this->assert_replaces('bar', 'object.foo');
  216. $this->assert_replaces('baz', 'object.bar');
  217. }
  218. function test_replace_variable_if_statement() {
  219. $this->assert_replaces('bar', 'if:true:foo');
  220. $this->assert_replaces('', 'if:false:foo');
  221. $this->assert_replaces('bar', 'if:true:foo:else:bar');
  222. $this->assert_replaces('baz', 'if:false:foo:else:bar');
  223. $this->assert_replaces('Bar', 'if:false:foo:else:FOO:strtolower:ucfirst');
  224. }
  225. /*function assert_block_renders($expected_file, $block, $data) {
  226. $rm = new ReflectionMethod('WebBasics\Template', 'render_block');
  227. $rm->setAccessible(true);
  228. $expected_file = "tests/_files/rendered/$expected_file.html";
  229. $this->assertStringEqualsFile($expected_file, $rm->invoke(null, $block, $data));
  230. }*/
  231. function assert_renders($expected_file, $tpl) {
  232. $expected_file = "tests/_files/rendered/$expected_file.html";
  233. $this->assertStringEqualsFile($expected_file, $tpl->render());
  234. }
  235. function test_render_simple() {
  236. $this->assertEquals('test', $this->tpl->render());
  237. }
  238. /**
  239. * @depends test_replace_variable_helper_functions
  240. */
  241. function test_render_variable() {
  242. $tpl = new Template('variables');
  243. $tpl->set(array(
  244. 'foobar' => 'my_foobar_variable',
  245. 'foobaz' => 'MY_FOOBAZ_VARIABLE'
  246. ));
  247. $this->assert_renders('variables', $tpl);
  248. }
  249. /**
  250. * @depends test_render_simple
  251. */
  252. function test_render_blocks() {
  253. $tpl = new Template('blocks');
  254. $foo = $tpl->add('foo');
  255. $foo->add('bar');
  256. $foo->add('bar');
  257. $tpl->add('foo');
  258. $this->assert_renders('blocks', $tpl);
  259. }
  260. /**
  261. * @depends test_render_variable
  262. * @depends test_render_blocks
  263. */
  264. function test_render_full() {
  265. $tpl = new Template('full');
  266. $first_foo = $tpl->add('foo')->set('foobaz', 'FIRST_FOOBAZ_VAR');
  267. $first_foo->add('bar')->set('foobar', 'first_foobar_var');
  268. $second_foo = $tpl->add('foo')->set('foobaz', 'SECOND_FOOBAZ_VAR');
  269. $second_foo->add('bar')->set('foobar', 'second_foobar_var');
  270. $second_foo->add('bar')->set('foobar', 'third_foobar_var');
  271. $this->assert_renders('full', $tpl);
  272. }
  273. }
  274. ?>