| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341 |
- <?php
- /**
- * @group inheritance
- * @group functional
- */
- class Mustache_Test_Functional_InheritanceTest extends PHPUnit_Framework_TestCase
- {
- private $mustache;
- public function setUp()
- {
- $this->mustache = new Mustache_Engine;
- }
- public function testDefaultContent()
- {
- $tpl = $this->mustache->loadTemplate('{{$title}}Default title{{/title}}');
- $data = array();
- $this->assertEquals('Default title', $tpl->render($data));
- }
- public function testDefaultContentRendersVariables()
- {
- $tpl = $this->mustache->loadTemplate('{{$foo}}default {{bar}} content{{/foo}}');
- $data = array(
- 'bar' => 'baz'
- );
- $this->assertEquals('default baz content', $tpl->render($data));
- }
- public function testDefaultContentRendersTripleMustacheVariables()
- {
- $tpl = $this->mustache->loadTemplate('{{$foo}}default {{{bar}}} content{{/foo}}');
- $data = array(
- 'bar' => '<baz>'
- );
- $this->assertEquals('default <baz> content', $tpl->render($data));
- }
- public function testDefaultContentRendersSections()
- {
- $tpl = $this->mustache->loadTemplate(
- '{{$foo}}default {{#bar}}{{baz}}{{/bar}} content{{/foo}}'
- );
- $data = array(
- 'bar' => array('baz' => 'qux')
- );
- $this->assertEquals('default qux content', $tpl->render($data));
- }
- public function testDefaultContentRendersNegativeSections()
- {
- $tpl = $this->mustache->loadTemplate(
- '{{$foo}}default {{^bar}}{{baz}}{{/bar}} content{{/foo}}'
- );
- $data = array(
- 'foo' => array('bar' => 'qux'),
- 'baz' => 'three'
- );
- $this->assertEquals('default three content', $tpl->render($data));
- }
- public function testMustacheInjectionInDefaultContent()
- {
- $tpl = $this->mustache->loadTemplate(
- '{{$foo}}default {{#bar}}{{baz}}{{/bar}} content{{/foo}}'
- );
- $data = array(
- 'bar' => array('baz' => '{{qux}}')
- );
- $this->assertEquals('default {{qux}} content', $tpl->render($data));
- }
- public function testDefaultContentRenderedInsideIncludedTemplates()
- {
- $partials = array(
- 'include' => '{{$foo}}default content{{/foo}}'
- );
- $this->mustache->setPartials($partials);
- $tpl = $this->mustache->loadTemplate(
- '{{<include}}{{/include}}'
- );
- $data = array();
- $this->assertEquals('default content', $tpl->render($data));
- }
- public function testOverriddenContent()
- {
- $partials = array(
- 'super' => '...{{$title}}Default title{{/title}}...'
- );
- $this->mustache->setPartials($partials);
- $tpl = $this->mustache->loadTemplate(
- '{{<super}}{{$title}}sub template title{{/title}}{{/super}}'
- );
- $data = array();
- $this->assertEquals('...sub template title...', $tpl->render($data));
- }
- public function testOverriddenPartial()
- {
- $partials = array(
- 'partial' => '|{{$stuff}}...{{/stuff}}{{$default}} default{{/default}}|'
- );
- $this->mustache->setPartials($partials);
- $tpl = $this->mustache->loadTemplate(
- 'test {{<partial}}{{$stuff}}override1{{/stuff}}{{/partial}} {{<partial}}{{$stuff}}override2{{/stuff}}{{/partial}}'
- );
- $data = array();
- $this->assertEquals('test |override1 default| |override2 default|', $tpl->render($data));
- }
- public function testDataDoesNotOverrideBlock()
- {
- $partials = array(
- 'include' => '{{$var}}var in include{{/var}}'
- );
- $this->mustache->setPartials($partials);
- $tpl = $this->mustache->loadTemplate(
- '{{<include}}{{$var}}var in template{{/var}}{{/include}}'
- );
- $data = array(
- 'var' => 'var in data'
- );
- $this->assertEquals('var in template', $tpl->render($data));
- }
- public function testDataDoesNotOverrideDefaultBlockValue()
- {
- $partials = array(
- 'include' => '{{$var}}var in include{{/var}}'
- );
- $this->mustache->setPartials($partials);
- $tpl = $this->mustache->loadTemplate(
- '{{<include}}{{/include}}'
- );
- $data = array(
- 'var' => 'var in data'
- );
- $this->assertEquals('var in include', $tpl->render($data));
- }
- public function testOverridePartialWithNewlines()
- {
- $partials = array(
- 'partial' => '{{$ballmer}}peaking{{/ballmer}}'
- );
- $this->mustache->setPartials($partials);
- $tpl = $this->mustache->loadTemplate(
- "{{<partial}}{{\$ballmer}}\npeaked\n\n:(\n{{/ballmer}}{{/partial}}"
- );
- $data = array();
- $this->assertEquals("peaked\n\n:(\n", $tpl->render($data));
- }
- public function testInheritIndentationWhenOverridingAPartial()
- {
- $partials = array(
- 'partial' =>
- 'stop:
- {{$nineties}}collaborate and listen{{/nineties}}'
- );
- $this->mustache->setPartials($partials);
- $tpl = $this->mustache->loadTemplate(
- '{{<partial}}{{$nineties}}hammer time{{/nineties}}{{/partial}}'
- );
- $data = array();
- $this->assertEquals(
- 'stop:
- hammer time',
- $tpl->render($data)
- );
- }
- public function testOverrideOneSubstitutionButNotTheOther()
- {
- $partials = array(
- 'partial' => '{{$stuff}}default one{{/stuff}}, {{$stuff2}}default two{{/stuff2}}'
- );
- $this->mustache->setPartials($partials);
- $tpl = $this->mustache->loadTemplate(
- '{{<partial}}{{$stuff2}}override two{{/stuff2}}{{/partial}}'
- );
- $data = array();
- $this->assertEquals('default one, override two', $tpl->render($data));
- }
- public function testSuperTemplatesWithNoParameters()
- {
- $partials = array(
- 'include' => '{{$foo}}default content{{/foo}}'
- );
- $this->mustache->setPartials($partials);
- $tpl = $this->mustache->loadTemplate(
- '{{>include}}|{{<include}}{{/include}}'
- );
- $data = array();
- $this->assertEquals('default content|default content', $tpl->render($data));
- }
- public function testRecursionInInheritedTemplates()
- {
- $partials = array(
- 'include' => '{{$foo}}default content{{/foo}} {{$bar}}{{<include2}}{{/include2}}{{/bar}}',
- 'include2' => '{{$foo}}include2 default content{{/foo}} {{<include}}{{$bar}}don\'t recurse{{/bar}}{{/include}}'
- );
- $this->mustache->setPartials($partials);
- $tpl = $this->mustache->loadTemplate(
- '{{<include}}{{$foo}}override{{/foo}}{{/include}}'
- );
- $data = array();
- $this->assertEquals('override override override don\'t recurse', $tpl->render($data));
- }
- public function testTopLevelSubstitutionsTakePrecedenceInMultilevelInheritance()
- {
- $partials = array(
- 'parent' => '{{<older}}{{$a}}p{{/a}}{{/older}}',
- 'older' => '{{<grandParent}}{{$a}}o{{/a}}{{/grandParent}}',
- 'grandParent' => '{{$a}}g{{/a}}'
- );
- $this->mustache->setPartials($partials);
- $tpl = $this->mustache->loadTemplate(
- '{{<parent}}{{$a}}c{{/a}}{{/parent}}'
- );
- $data = array();
- $this->assertEquals('c', $tpl->render($data));
- }
- public function testMultiLevelInheritanceNoSubChild()
- {
- $partials = array(
- 'parent' => '{{<older}}{{$a}}p{{/a}}{{/older}}',
- 'older' => '{{<grandParent}}{{$a}}o{{/a}}{{/grandParent}}',
- 'grandParent' => '{{$a}}g{{/a}}'
- );
- $this->mustache->setPartials($partials);
- $tpl = $this->mustache->loadTemplate(
- '{{<parent}}{{/parent}}'
- );
- $data = array();
- $this->assertEquals('p', $tpl->render($data));
- }
- public function testIgnoreTextInsideSuperTemplatesButParseArgs()
- {
- $partials = array(
- 'include' => '{{$foo}}default content{{/foo}}'
- );
- $this->mustache->setPartials($partials);
- $tpl = $this->mustache->loadTemplate(
- '{{<include}} asdfasd {{$foo}}hmm{{/foo}} asdfasdfasdf {{/include}}'
- );
- $data = array();
- $this->assertEquals('hmm', $tpl->render($data));
- }
- public function IgnoreTextInsideSuperTemplates()
- {
- $partials = array(
- 'include' => '{{$foo}}default content{{/foo}}'
- );
- $this->mustache->setPartials($partials);
- $tpl = $this->mustache->loadTemplate(
- '{{<include}} asdfasd asdfasdfasdf {{/include}}'
- );
- $data = array();
- $this->assertEquals('default content', $tpl->render($data));
- }
- }
|