HigherOrderSectionsTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /*
  3. * This file is part of Mustache.php.
  4. *
  5. * (c) 2013 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 PHPUnit_Framework_TestCase
  15. {
  16. private $mustache;
  17. public function setUp()
  18. {
  19. $this->mustache = new Mustache_Engine;
  20. }
  21. public function testRuntimeSectionCallback()
  22. {
  23. $tpl = $this->mustache->loadTemplate('{{#doublewrap}}{{name}}{{/doublewrap}}');
  24. $foo = new Mustache_Test_Functional_Foo;
  25. $foo->doublewrap = array($foo, 'wrapWithBoth');
  26. $this->assertEquals(sprintf('<strong><em>%s</em></strong>', $foo->name), $tpl->render($foo));
  27. }
  28. public function testStaticSectionCallback()
  29. {
  30. $tpl = $this->mustache->loadTemplate('{{#trimmer}} {{name}} {{/trimmer}}');
  31. $foo = new Mustache_Test_Functional_Foo;
  32. $foo->trimmer = array(get_class($foo), 'staticTrim');
  33. $this->assertEquals($foo->name, $tpl->render($foo));
  34. }
  35. public function testViewArraySectionCallback()
  36. {
  37. $tpl = $this->mustache->loadTemplate('{{#trim}} {{name}} {{/trim}}');
  38. $foo = new Mustache_Test_Functional_Foo;
  39. $data = array(
  40. 'name' => 'Bob',
  41. 'trim' => array(get_class($foo), 'staticTrim'),
  42. );
  43. $this->assertEquals($data['name'], $tpl->render($data));
  44. }
  45. public function testMonsters()
  46. {
  47. $tpl = $this->mustache->loadTemplate('{{#title}}{{title}} {{/title}}{{name}}');
  48. $frank = new Mustache_Test_Functional_Monster();
  49. $frank->title = 'Dr.';
  50. $frank->name = 'Frankenstein';
  51. $this->assertEquals('Dr. Frankenstein', $tpl->render($frank));
  52. $dracula = new Mustache_Test_Functional_Monster();
  53. $dracula->title = 'Count';
  54. $dracula->name = 'Dracula';
  55. $this->assertEquals('Count Dracula', $tpl->render($dracula));
  56. }
  57. public function testPassthroughOptimization()
  58. {
  59. $mustache = $this->getMock('Mustache_Engine', array('loadLambda'));
  60. $mustache->expects($this->never())
  61. ->method('loadLambda');
  62. $tpl = $mustache->loadTemplate('{{#wrap}}NAME{{/wrap}}');
  63. $foo = new Mustache_Test_Functional_Foo;
  64. $foo->wrap = array($foo, 'wrapWithEm');
  65. $this->assertEquals('<em>NAME</em>', $tpl->render($foo));
  66. }
  67. public function testWithoutPassthroughOptimization()
  68. {
  69. $mustache = $this->getMock('Mustache_Engine', array('loadLambda'));
  70. $mustache->expects($this->once())
  71. ->method('loadLambda')
  72. ->will($this->returnValue($mustache->loadTemplate('<em>{{ name }}</em>')));
  73. $tpl = $mustache->loadTemplate('{{#wrap}}{{name}}{{/wrap}}');
  74. $foo = new Mustache_Test_Functional_Foo;
  75. $foo->wrap = array($foo, 'wrapWithEm');
  76. $this->assertEquals('<em>' . $foo->name . '</em>', $tpl->render($foo));
  77. }
  78. }
  79. class Mustache_Test_Functional_Foo
  80. {
  81. public $name = 'Justin';
  82. public $lorem = 'Lorem ipsum dolor sit amet,';
  83. public function wrapWithEm($text)
  84. {
  85. return sprintf('<em>%s</em>', $text);
  86. }
  87. public function wrapWithStrong($text)
  88. {
  89. return sprintf('<strong>%s</strong>', $text);
  90. }
  91. public function wrapWithBoth($text)
  92. {
  93. return self::wrapWithStrong(self::wrapWithEm($text));
  94. }
  95. public static function staticTrim($text)
  96. {
  97. return trim($text);
  98. }
  99. }
  100. class Mustache_Test_Functional_Monster
  101. {
  102. public $title;
  103. public $name;
  104. }