InheritanceTest.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. <?php
  2. /**
  3. * @group inheritance
  4. * @group functional
  5. */
  6. class Mustache_Test_Functional_InheritanceTest extends PHPUnit_Framework_TestCase
  7. {
  8. private $mustache;
  9. public function setUp()
  10. {
  11. $this->mustache = new Mustache_Engine;
  12. }
  13. public function testDefaultContent()
  14. {
  15. $tpl = $this->mustache->loadTemplate('{{$title}}Default title{{/title}}');
  16. $data = array();
  17. $this->assertEquals('Default title', $tpl->render($data));
  18. }
  19. public function testDefaultContentRendersVariables()
  20. {
  21. $tpl = $this->mustache->loadTemplate('{{$foo}}default {{bar}} content{{/foo}}');
  22. $data = array(
  23. 'bar' => 'baz'
  24. );
  25. $this->assertEquals('default baz content', $tpl->render($data));
  26. }
  27. public function testDefaultContentRendersTripleMustacheVariables()
  28. {
  29. $tpl = $this->mustache->loadTemplate('{{$foo}}default {{{bar}}} content{{/foo}}');
  30. $data = array(
  31. 'bar' => '<baz>'
  32. );
  33. $this->assertEquals('default <baz> content', $tpl->render($data));
  34. }
  35. public function testDefaultContentRendersSections()
  36. {
  37. $tpl = $this->mustache->loadTemplate(
  38. '{{$foo}}default {{#bar}}{{baz}}{{/bar}} content{{/foo}}'
  39. );
  40. $data = array(
  41. 'bar' => array('baz' => 'qux')
  42. );
  43. $this->assertEquals('default qux content', $tpl->render($data));
  44. }
  45. public function testDefaultContentRendersNegativeSections()
  46. {
  47. $tpl = $this->mustache->loadTemplate(
  48. '{{$foo}}default {{^bar}}{{baz}}{{/bar}} content{{/foo}}'
  49. );
  50. $data = array(
  51. 'foo' => array('bar' => 'qux'),
  52. 'baz' => 'three'
  53. );
  54. $this->assertEquals('default three content', $tpl->render($data));
  55. }
  56. public function testMustacheInjectionInDefaultContent()
  57. {
  58. $tpl = $this->mustache->loadTemplate(
  59. '{{$foo}}default {{#bar}}{{baz}}{{/bar}} content{{/foo}}'
  60. );
  61. $data = array(
  62. 'bar' => array('baz' => '{{qux}}')
  63. );
  64. $this->assertEquals('default {{qux}} content', $tpl->render($data));
  65. }
  66. public function testDefaultContentRenderedInsideIncludedTemplates()
  67. {
  68. $partials = array(
  69. 'include' => '{{$foo}}default content{{/foo}}'
  70. );
  71. $this->mustache->setPartials($partials);
  72. $tpl = $this->mustache->loadTemplate(
  73. '{{<include}}{{/include}}'
  74. );
  75. $data = array();
  76. $this->assertEquals('default content', $tpl->render($data));
  77. }
  78. public function testOverriddenContent()
  79. {
  80. $partials = array(
  81. 'super' => '...{{$title}}Default title{{/title}}...'
  82. );
  83. $this->mustache->setPartials($partials);
  84. $tpl = $this->mustache->loadTemplate(
  85. '{{<super}}{{$title}}sub template title{{/title}}{{/super}}'
  86. );
  87. $data = array();
  88. $this->assertEquals('...sub template title...', $tpl->render($data));
  89. }
  90. public function testOverriddenPartial()
  91. {
  92. $partials = array(
  93. 'partial' => '|{{$stuff}}...{{/stuff}}{{$default}} default{{/default}}|'
  94. );
  95. $this->mustache->setPartials($partials);
  96. $tpl = $this->mustache->loadTemplate(
  97. 'test {{<partial}}{{$stuff}}override1{{/stuff}}{{/partial}} {{<partial}}{{$stuff}}override2{{/stuff}}{{/partial}}'
  98. );
  99. $data = array();
  100. $this->assertEquals('test |override1 default| |override2 default|', $tpl->render($data));
  101. }
  102. public function testDataDoesNotOverrideBlock()
  103. {
  104. $partials = array(
  105. 'include' => '{{$var}}var in include{{/var}}'
  106. );
  107. $this->mustache->setPartials($partials);
  108. $tpl = $this->mustache->loadTemplate(
  109. '{{<include}}{{$var}}var in template{{/var}}{{/include}}'
  110. );
  111. $data = array(
  112. 'var' => 'var in data'
  113. );
  114. $this->assertEquals('var in template', $tpl->render($data));
  115. }
  116. public function testDataDoesNotOverrideDefaultBlockValue()
  117. {
  118. $partials = array(
  119. 'include' => '{{$var}}var in include{{/var}}'
  120. );
  121. $this->mustache->setPartials($partials);
  122. $tpl = $this->mustache->loadTemplate(
  123. '{{<include}}{{/include}}'
  124. );
  125. $data = array(
  126. 'var' => 'var in data'
  127. );
  128. $this->assertEquals('var in include', $tpl->render($data));
  129. }
  130. public function testOverridePartialWithNewlines()
  131. {
  132. $partials = array(
  133. 'partial' => '{{$ballmer}}peaking{{/ballmer}}'
  134. );
  135. $this->mustache->setPartials($partials);
  136. $tpl = $this->mustache->loadTemplate(
  137. "{{<partial}}{{\$ballmer}}\npeaked\n\n:(\n{{/ballmer}}{{/partial}}"
  138. );
  139. $data = array();
  140. $this->assertEquals("peaked\n\n:(\n", $tpl->render($data));
  141. }
  142. public function testInheritIndentationWhenOverridingAPartial()
  143. {
  144. $partials = array(
  145. 'partial' =>
  146. 'stop:
  147. {{$nineties}}collaborate and listen{{/nineties}}'
  148. );
  149. $this->mustache->setPartials($partials);
  150. $tpl = $this->mustache->loadTemplate(
  151. '{{<partial}}{{$nineties}}hammer time{{/nineties}}{{/partial}}'
  152. );
  153. $data = array();
  154. $this->assertEquals(
  155. 'stop:
  156. hammer time',
  157. $tpl->render($data)
  158. );
  159. }
  160. public function testOverrideOneSubstitutionButNotTheOther()
  161. {
  162. $partials = array(
  163. 'partial' => '{{$stuff}}default one{{/stuff}}, {{$stuff2}}default two{{/stuff2}}'
  164. );
  165. $this->mustache->setPartials($partials);
  166. $tpl = $this->mustache->loadTemplate(
  167. '{{<partial}}{{$stuff2}}override two{{/stuff2}}{{/partial}}'
  168. );
  169. $data = array();
  170. $this->assertEquals('default one, override two', $tpl->render($data));
  171. }
  172. public function testSuperTemplatesWithNoParameters()
  173. {
  174. $partials = array(
  175. 'include' => '{{$foo}}default content{{/foo}}'
  176. );
  177. $this->mustache->setPartials($partials);
  178. $tpl = $this->mustache->loadTemplate(
  179. '{{>include}}|{{<include}}{{/include}}'
  180. );
  181. $data = array();
  182. $this->assertEquals('default content|default content', $tpl->render($data));
  183. }
  184. public function testRecursionInInheritedTemplates()
  185. {
  186. $partials = array(
  187. 'include' => '{{$foo}}default content{{/foo}} {{$bar}}{{<include2}}{{/include2}}{{/bar}}',
  188. 'include2' => '{{$foo}}include2 default content{{/foo}} {{<include}}{{$bar}}don\'t recurse{{/bar}}{{/include}}'
  189. );
  190. $this->mustache->setPartials($partials);
  191. $tpl = $this->mustache->loadTemplate(
  192. '{{<include}}{{$foo}}override{{/foo}}{{/include}}'
  193. );
  194. $data = array();
  195. $this->assertEquals('override override override don\'t recurse', $tpl->render($data));
  196. }
  197. public function testTopLevelSubstitutionsTakePrecedenceInMultilevelInheritance()
  198. {
  199. $partials = array(
  200. 'parent' => '{{<older}}{{$a}}p{{/a}}{{/older}}',
  201. 'older' => '{{<grandParent}}{{$a}}o{{/a}}{{/grandParent}}',
  202. 'grandParent' => '{{$a}}g{{/a}}'
  203. );
  204. $this->mustache->setPartials($partials);
  205. $tpl = $this->mustache->loadTemplate(
  206. '{{<parent}}{{$a}}c{{/a}}{{/parent}}'
  207. );
  208. $data = array();
  209. $this->assertEquals('c', $tpl->render($data));
  210. }
  211. public function testMultiLevelInheritanceNoSubChild()
  212. {
  213. $partials = array(
  214. 'parent' => '{{<older}}{{$a}}p{{/a}}{{/older}}',
  215. 'older' => '{{<grandParent}}{{$a}}o{{/a}}{{/grandParent}}',
  216. 'grandParent' => '{{$a}}g{{/a}}'
  217. );
  218. $this->mustache->setPartials($partials);
  219. $tpl = $this->mustache->loadTemplate(
  220. '{{<parent}}{{/parent}}'
  221. );
  222. $data = array();
  223. $this->assertEquals('p', $tpl->render($data));
  224. }
  225. public function testIgnoreTextInsideSuperTemplatesButParseArgs()
  226. {
  227. $partials = array(
  228. 'include' => '{{$foo}}default content{{/foo}}'
  229. );
  230. $this->mustache->setPartials($partials);
  231. $tpl = $this->mustache->loadTemplate(
  232. '{{<include}} asdfasd {{$foo}}hmm{{/foo}} asdfasdfasdf {{/include}}'
  233. );
  234. $data = array();
  235. $this->assertEquals('hmm', $tpl->render($data));
  236. }
  237. public function testIgnoreTextInsideSuperTemplates()
  238. {
  239. $partials = array(
  240. 'include' => '{{$foo}}default content{{/foo}}'
  241. );
  242. $this->mustache->setPartials($partials);
  243. $tpl = $this->mustache->loadTemplate(
  244. '{{<include}} asdfasd asdfasdfasdf {{/include}}'
  245. );
  246. $data = array();
  247. $this->assertEquals('default content', $tpl->render($data));
  248. }
  249. /**
  250. * @expectedException Mustache_Exception_SyntaxException
  251. * @expectedExceptionMessage Illegal content in < parent tag
  252. */
  253. public function testOnlyBlockTagsAllowedInParent()
  254. {
  255. $partials = array(
  256. 'foo' => '{{$baz}}default content{{/baz}}'
  257. );
  258. $this->mustache->setPartials($partials);
  259. $tpl = $this->mustache->loadTemplate(
  260. '{{< foo }}{{# bar }}{{$ baz }}{{/ baz }}{{/ bar }}{{/ foo }}'
  261. );
  262. $data = array(
  263. 'bar' => 'set by user'
  264. );
  265. $tpl->render($data);
  266. }
  267. }