HigherOrderSectionsTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. /*
  3. * This file is part of Mustache.php.
  4. *
  5. * (c) 2010-2014 Justin Hileman
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. /**
  11. * @group lambdas
  12. * @group functional
  13. */
  14. class Mustache_Test_Functional_HigherOrderSectionsTest extends Mustache_Test_FunctionalTestCase
  15. {
  16. private $mustache;
  17. public function setUp()
  18. {
  19. $this->mustache = new Mustache_Engine;
  20. }
  21. /**
  22. * @dataProvider sectionCallbackData
  23. */
  24. public function testSectionCallback($data, $tpl, $expect)
  25. {
  26. $this->assertEquals($expect, $this->mustache->render($tpl, $data));
  27. }
  28. public function sectionCallbackData()
  29. {
  30. $foo = new Mustache_Test_Functional_Foo;
  31. $foo->doublewrap = array($foo, 'wrapWithBoth');
  32. $bar = new Mustache_Test_Functional_Foo;
  33. $bar->trimmer = array(get_class($bar), 'staticTrim');
  34. return array(
  35. array($foo, '{{#doublewrap}}{{name}}{{/doublewrap}}', sprintf('<strong><em>%s</em></strong>', $foo->name)),
  36. array($bar, '{{#trimmer}} {{name}} {{/trimmer}}', $bar->name),
  37. );
  38. }
  39. public function testViewArraySectionCallback()
  40. {
  41. $tpl = $this->mustache->loadTemplate('{{#trim}} {{name}} {{/trim}}');
  42. $foo = new Mustache_Test_Functional_Foo;
  43. $data = array(
  44. 'name' => 'Bob',
  45. 'trim' => array(get_class($foo), 'staticTrim'),
  46. );
  47. $this->assertEquals($data['name'], $tpl->render($data));
  48. }
  49. public function testMonsters()
  50. {
  51. $tpl = $this->mustache->loadTemplate('{{#title}}{{title}} {{/title}}{{name}}');
  52. $frank = new Mustache_Test_Functional_Monster();
  53. $frank->title = 'Dr.';
  54. $frank->name = 'Frankenstein';
  55. $this->assertEquals('Dr. Frankenstein', $tpl->render($frank));
  56. $dracula = new Mustache_Test_Functional_Monster();
  57. $dracula->title = 'Count';
  58. $dracula->name = 'Dracula';
  59. $this->assertEquals('Count Dracula', $tpl->render($dracula));
  60. }
  61. public function testPassthroughOptimization()
  62. {
  63. $mustache = $this->getMock('Mustache_Engine', array('loadLambda'));
  64. $mustache->expects($this->never())
  65. ->method('loadLambda');
  66. $tpl = $mustache->loadTemplate('{{#wrap}}NAME{{/wrap}}');
  67. $foo = new Mustache_Test_Functional_Foo;
  68. $foo->wrap = array($foo, 'wrapWithEm');
  69. $this->assertEquals('<em>NAME</em>', $tpl->render($foo));
  70. }
  71. public function testWithoutPassthroughOptimization()
  72. {
  73. $mustache = $this->getMock('Mustache_Engine', array('loadLambda'));
  74. $mustache->expects($this->once())
  75. ->method('loadLambda')
  76. ->will($this->returnValue($mustache->loadTemplate('<em>{{ name }}</em>')));
  77. $tpl = $mustache->loadTemplate('{{#wrap}}{{name}}{{/wrap}}');
  78. $foo = new Mustache_Test_Functional_Foo;
  79. $foo->wrap = array($foo, 'wrapWithEm');
  80. $this->assertEquals('<em>' . $foo->name . '</em>', $tpl->render($foo));
  81. }
  82. /**
  83. * @dataProvider cacheLambdaTemplatesData
  84. */
  85. public function testCacheLambdaTemplatesOptionWorks($dirName, $tplPrefix, $enable, $expect)
  86. {
  87. $cacheDir = $this->setUpCacheDir($dirName);
  88. $mustache = new Mustache_Engine(array(
  89. 'template_class_prefix' => $tplPrefix,
  90. 'cache' => $cacheDir,
  91. 'cache_lambda_templates' => $enable,
  92. ));
  93. $tpl = $mustache->loadTemplate('{{#wrap}}{{name}}{{/wrap}}');
  94. $foo = new Mustache_Test_Functional_Foo;
  95. $foo->wrap = array($foo, 'wrapWithEm');
  96. $this->assertEquals('<em>' . $foo->name . '</em>', $tpl->render($foo));
  97. $this->assertCount($expect, glob($cacheDir . '/*.php'));
  98. }
  99. public function cacheLambdaTemplatesData()
  100. {
  101. return array(
  102. array('test_enabling_lambda_cache', '_TestEnablingLambdaCache_', true, 2),
  103. array('test_disabling_lambda_cache', '_TestDisablingLambdaCache_', false, 1),
  104. );
  105. }
  106. protected function setUpCacheDir($name)
  107. {
  108. $cacheDir = self::$tempDir . '/' . $name;
  109. if (file_exists($cacheDir)) {
  110. self::rmdir($cacheDir);
  111. }
  112. mkdir($cacheDir, 0777, true);
  113. return $cacheDir;
  114. }
  115. }
  116. class Mustache_Test_Functional_Foo
  117. {
  118. public $name = 'Justin';
  119. public $lorem = 'Lorem ipsum dolor sit amet,';
  120. public function wrapWithEm($text)
  121. {
  122. return sprintf('<em>%s</em>', $text);
  123. }
  124. /**
  125. * @param string $text
  126. */
  127. public function wrapWithStrong($text)
  128. {
  129. return sprintf('<strong>%s</strong>', $text);
  130. }
  131. public function wrapWithBoth($text)
  132. {
  133. return self::wrapWithStrong(self::wrapWithEm($text));
  134. }
  135. public static function staticTrim($text)
  136. {
  137. return trim($text);
  138. }
  139. }
  140. class Mustache_Test_Functional_Monster
  141. {
  142. public $title;
  143. public $name;
  144. }