HigherOrderSectionsTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. namespace Mustache\Test\Functional;
  11. use Mustache\Mustache;
  12. /**
  13. * @group lambdas
  14. * @group functional
  15. */
  16. class HigherOrderSectionsTest extends \PHPUnit_Framework_TestCase {
  17. private $mustache;
  18. public function setUp() {
  19. $this->mustache = new Mustache;
  20. }
  21. public function testAnonymousFunctionSectionCallback() {
  22. $tpl = $this->mustache->loadTemplate('{{#wrapper}}{{name}}{{/wrapper}}');
  23. $foo = new Foo;
  24. $foo->name = 'Mario';
  25. $foo->wrapper = function($text) {
  26. return sprintf('<div class="anonymous">%s</div>', $text);
  27. };
  28. $this->assertEquals(sprintf('<div class="anonymous">%s</div>', $foo->name), $tpl->render($foo));
  29. }
  30. public function testSectionCallback() {
  31. $one = $this->mustache->loadTemplate('{{name}}');
  32. $two = $this->mustache->loadTemplate('{{#wrap}}{{name}}{{/wrap}}');
  33. $foo = new Foo;
  34. $foo->name = 'Luigi';
  35. $this->assertEquals($foo->name, $one->render($foo));
  36. $this->assertEquals(sprintf('<em>%s</em>', $foo->name), $two->render($foo));
  37. }
  38. public function testRuntimeSectionCallback() {
  39. $tpl = $this->mustache->loadTemplate('{{#doublewrap}}{{name}}{{/doublewrap}}');
  40. $foo = new Foo;
  41. $foo->doublewrap = array($foo, 'wrapWithBoth');
  42. $this->assertEquals(sprintf('<strong><em>%s</em></strong>', $foo->name), $tpl->render($foo));
  43. }
  44. public function testStaticSectionCallback() {
  45. $tpl = $this->mustache->loadTemplate('{{#trimmer}} {{name}} {{/trimmer}}');
  46. $foo = new Foo;
  47. $foo->trimmer = array(get_class($foo), 'staticTrim');
  48. $this->assertEquals($foo->name, $tpl->render($foo));
  49. }
  50. public function testViewArraySectionCallback() {
  51. $tpl = $this->mustache->loadTemplate('{{#trim}} {{name}} {{/trim}}');
  52. $foo = new Foo;
  53. $data = array(
  54. 'name' => 'Bob',
  55. 'trim' => array(get_class($foo), 'staticTrim'),
  56. );
  57. $this->assertEquals($data['name'], $tpl->render($data));
  58. }
  59. public function testViewArrayAnonymousSectionCallback() {
  60. $tpl = $this->mustache->loadTemplate('{{#wrap}}{{name}}{{/wrap}}');
  61. $data = array(
  62. 'name' => 'Bob',
  63. 'wrap' => function($text) {
  64. return sprintf('[[%s]]', $text);
  65. }
  66. );
  67. $this->assertEquals(sprintf('[[%s]]', $data['name']), $tpl->render($data));
  68. }
  69. public function testMonsters() {
  70. $tpl = $this->mustache->loadTemplate('{{#title}}{{title}} {{/title}}{{name}}');
  71. $frank = new Monster();
  72. $frank->title = 'Dr.';
  73. $frank->name = 'Frankenstein';
  74. $this->assertEquals('Dr. Frankenstein', $tpl->render($frank));
  75. $dracula = new Monster();
  76. $dracula->title = 'Count';
  77. $dracula->name = 'Dracula';
  78. $this->assertEquals('Count Dracula', $tpl->render($dracula));
  79. }
  80. }
  81. class Foo {
  82. public $name = 'Justin';
  83. public $lorem = 'Lorem ipsum dolor sit amet,';
  84. public $wrap;
  85. public function __construct() {
  86. $this->wrap = function($text) {
  87. return sprintf('<em>%s</em>', $text);
  88. };
  89. }
  90. public function wrapWithEm($text) {
  91. return sprintf('<em>%s</em>', $text);
  92. }
  93. public function wrapWithStrong($text) {
  94. return sprintf('<strong>%s</strong>', $text);
  95. }
  96. public function wrapWithBoth($text) {
  97. return self::wrapWithStrong(self::wrapWithEm($text));
  98. }
  99. public static function staticTrim($text) {
  100. return trim($text);
  101. }
  102. }
  103. class Monster {
  104. public $title;
  105. public $name;
  106. }