| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- <?php
- /*
- * This file is part of Mustache.php.
- *
- * (c) 2010-2014 Justin Hileman
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- /**
- * @group lambdas
- * @group functional
- */
- class Mustache_Test_Functional_HigherOrderSectionsTest extends Mustache_Test_FunctionalTestCase
- {
- private $mustache;
- public function setUp()
- {
- $this->mustache = new Mustache_Engine;
- }
- /**
- * @dataProvider sectionCallbackData
- */
- public function testSectionCallback($data, $tpl, $expect)
- {
- $this->assertEquals($expect, $this->mustache->render($tpl, $data));
- }
- public function sectionCallbackData()
- {
- $foo = new Mustache_Test_Functional_Foo;
- $foo->doublewrap = array($foo, 'wrapWithBoth');
- $bar = new Mustache_Test_Functional_Foo;
- $bar->trimmer = array(get_class($bar), 'staticTrim');
- return array(
- array($foo, '{{#doublewrap}}{{name}}{{/doublewrap}}', sprintf('<strong><em>%s</em></strong>', $foo->name)),
- array($bar, '{{#trimmer}} {{name}} {{/trimmer}}', $bar->name),
- );
- }
- public function testViewArraySectionCallback()
- {
- $tpl = $this->mustache->loadTemplate('{{#trim}} {{name}} {{/trim}}');
- $foo = new Mustache_Test_Functional_Foo;
- $data = array(
- 'name' => 'Bob',
- 'trim' => array(get_class($foo), 'staticTrim'),
- );
- $this->assertEquals($data['name'], $tpl->render($data));
- }
- public function testMonsters()
- {
- $tpl = $this->mustache->loadTemplate('{{#title}}{{title}} {{/title}}{{name}}');
- $frank = new Mustache_Test_Functional_Monster();
- $frank->title = 'Dr.';
- $frank->name = 'Frankenstein';
- $this->assertEquals('Dr. Frankenstein', $tpl->render($frank));
- $dracula = new Mustache_Test_Functional_Monster();
- $dracula->title = 'Count';
- $dracula->name = 'Dracula';
- $this->assertEquals('Count Dracula', $tpl->render($dracula));
- }
- public function testPassthroughOptimization()
- {
- $mustache = $this->getMock('Mustache_Engine', array('loadLambda'));
- $mustache->expects($this->never())
- ->method('loadLambda');
- $tpl = $mustache->loadTemplate('{{#wrap}}NAME{{/wrap}}');
- $foo = new Mustache_Test_Functional_Foo;
- $foo->wrap = array($foo, 'wrapWithEm');
- $this->assertEquals('<em>NAME</em>', $tpl->render($foo));
- }
- public function testWithoutPassthroughOptimization()
- {
- $mustache = $this->getMock('Mustache_Engine', array('loadLambda'));
- $mustache->expects($this->once())
- ->method('loadLambda')
- ->will($this->returnValue($mustache->loadTemplate('<em>{{ name }}</em>')));
- $tpl = $mustache->loadTemplate('{{#wrap}}{{name}}{{/wrap}}');
- $foo = new Mustache_Test_Functional_Foo;
- $foo->wrap = array($foo, 'wrapWithEm');
- $this->assertEquals('<em>' . $foo->name . '</em>', $tpl->render($foo));
- }
- /**
- * @dataProvider cacheLambdaTemplatesData
- */
- public function testCacheLambdaTemplatesOptionWorks($dirName, $tplPrefix, $enable, $expect)
- {
- $cacheDir = $this->setUpCacheDir($dirName);
- $mustache = new Mustache_Engine(array(
- 'template_class_prefix' => $tplPrefix,
- 'cache' => $cacheDir,
- 'cache_lambda_templates' => $enable,
- ));
- $tpl = $mustache->loadTemplate('{{#wrap}}{{name}}{{/wrap}}');
- $foo = new Mustache_Test_Functional_Foo;
- $foo->wrap = array($foo, 'wrapWithEm');
- $this->assertEquals('<em>' . $foo->name . '</em>', $tpl->render($foo));
- $this->assertCount($expect, glob($cacheDir . '/*.php'));
- }
- public function cacheLambdaTemplatesData()
- {
- return array(
- array('test_enabling_lambda_cache', '_TestEnablingLambdaCache_', true, 2),
- array('test_disabling_lambda_cache', '_TestDisablingLambdaCache_', false, 1),
- );
- }
- protected function setUpCacheDir($name)
- {
- $cacheDir = self::$tempDir . '/' . $name;
- if (file_exists($cacheDir)) {
- self::rmdir($cacheDir);
- }
- mkdir($cacheDir, 0777, true);
- return $cacheDir;
- }
- }
- class Mustache_Test_Functional_Foo
- {
- public $name = 'Justin';
- public $lorem = 'Lorem ipsum dolor sit amet,';
- public function wrapWithEm($text)
- {
- return sprintf('<em>%s</em>', $text);
- }
- /**
- * @param string $text
- */
- public function wrapWithStrong($text)
- {
- return sprintf('<strong>%s</strong>', $text);
- }
- public function wrapWithBoth($text)
- {
- return self::wrapWithStrong(self::wrapWithEm($text));
- }
- public static function staticTrim($text)
- {
- return trim($text);
- }
- }
- class Mustache_Test_Functional_Monster
- {
- public $title;
- public $name;
- }
|