HigherOrderSectionsTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /*
  3. * This file is part of Mustache.php.
  4. *
  5. * (c) 2012 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. private $mustache;
  16. public function setUp() {
  17. $this->mustache = new Mustache_Mustache;
  18. }
  19. public function testAnonymousFunctionSectionCallback() {
  20. if (version_compare(PHP_VERSION, '5.3.0', '<')) {
  21. $this->markTestSkipped('Unable to test anonymous function section callbacks in PHP < 5.3');
  22. return;
  23. }
  24. $tpl = $this->mustache->loadTemplate('{{#wrapper}}{{name}}{{/wrapper}}');
  25. $foo = new Mustache_Test_Functional_Foo;
  26. $foo->name = 'Mario';
  27. $foo->wrapper = function($text) {
  28. return sprintf('<div class="anonymous">%s</div>', $text);
  29. };
  30. $this->assertEquals(sprintf('<div class="anonymous">%s</div>', $foo->name), $tpl->render($foo));
  31. }
  32. public function testSectionCallback() {
  33. $one = $this->mustache->loadTemplate('{{name}}');
  34. $two = $this->mustache->loadTemplate('{{#wrap}}{{name}}{{/wrap}}');
  35. $foo = new Mustache_Test_Functional_Foo;
  36. $foo->name = 'Luigi';
  37. $this->assertEquals($foo->name, $one->render($foo));
  38. $this->assertEquals(sprintf('<em>%s</em>', $foo->name), $two->render($foo));
  39. }
  40. public function testRuntimeSectionCallback() {
  41. $tpl = $this->mustache->loadTemplate('{{#doublewrap}}{{name}}{{/doublewrap}}');
  42. $foo = new Mustache_Test_Functional_Foo;
  43. $foo->doublewrap = array($foo, 'wrapWithBoth');
  44. $this->assertEquals(sprintf('<strong><em>%s</em></strong>', $foo->name), $tpl->render($foo));
  45. }
  46. public function testStaticSectionCallback() {
  47. $tpl = $this->mustache->loadTemplate('{{#trimmer}} {{name}} {{/trimmer}}');
  48. $foo = new Mustache_Test_Functional_Foo;
  49. $foo->trimmer = array(get_class($foo), 'staticTrim');
  50. $this->assertEquals($foo->name, $tpl->render($foo));
  51. }
  52. public function testViewArraySectionCallback() {
  53. $tpl = $this->mustache->loadTemplate('{{#trim}} {{name}} {{/trim}}');
  54. $foo = new Mustache_Test_Functional_Foo;
  55. $data = array(
  56. 'name' => 'Bob',
  57. 'trim' => array(get_class($foo), 'staticTrim'),
  58. );
  59. $this->assertEquals($data['name'], $tpl->render($data));
  60. }
  61. public function testViewArrayAnonymousSectionCallback() {
  62. $tpl = $this->mustache->loadTemplate('{{#wrap}}{{name}}{{/wrap}}');
  63. $data = array(
  64. 'name' => 'Bob',
  65. 'wrap' => function($text) {
  66. return sprintf('[[%s]]', $text);
  67. }
  68. );
  69. $this->assertEquals(sprintf('[[%s]]', $data['name']), $tpl->render($data));
  70. }
  71. public function testMonsters() {
  72. $tpl = $this->mustache->loadTemplate('{{#title}}{{title}} {{/title}}{{name}}');
  73. $frank = new Mustache_Test_Functional_Monster();
  74. $frank->title = 'Dr.';
  75. $frank->name = 'Frankenstein';
  76. $this->assertEquals('Dr. Frankenstein', $tpl->render($frank));
  77. $dracula = new Mustache_Test_Functional_Monster();
  78. $dracula->title = 'Count';
  79. $dracula->name = 'Dracula';
  80. $this->assertEquals('Count Dracula', $tpl->render($dracula));
  81. }
  82. }
  83. class Mustache_Test_Functional_Foo {
  84. public $name = 'Justin';
  85. public $lorem = 'Lorem ipsum dolor sit amet,';
  86. public $wrap;
  87. public function __construct() {
  88. $this->wrap = function($text) {
  89. return sprintf('<em>%s</em>', $text);
  90. };
  91. }
  92. public function wrapWithEm($text) {
  93. return sprintf('<em>%s</em>', $text);
  94. }
  95. public function wrapWithStrong($text) {
  96. return sprintf('<strong>%s</strong>', $text);
  97. }
  98. public function wrapWithBoth($text) {
  99. return self::wrapWithStrong(self::wrapWithEm($text));
  100. }
  101. public static function staticTrim($text) {
  102. return trim($text);
  103. }
  104. }
  105. class Mustache_Test_Functional_Monster {
  106. public $title;
  107. public $name;
  108. }