HigherOrderSectionsTest.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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_FiveThree_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 testAnonymousFunctionSectionCallback()
  22. {
  23. $tpl = $this->mustache->loadTemplate('{{#wrapper}}{{name}}{{/wrapper}}');
  24. $foo = new Mustache_Test_FiveThree_Functional_Foo();
  25. $foo->name = 'Mario';
  26. $foo->wrapper = function ($text) {
  27. return sprintf('<div class="anonymous">%s</div>', $text);
  28. };
  29. $this->assertEquals(sprintf('<div class="anonymous">%s</div>', $foo->name), $tpl->render($foo));
  30. }
  31. public function testSectionCallback()
  32. {
  33. $one = $this->mustache->loadTemplate('{{name}}');
  34. $two = $this->mustache->loadTemplate('{{#wrap}}{{name}}{{/wrap}}');
  35. $foo = new Mustache_Test_FiveThree_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 testViewArrayAnonymousSectionCallback()
  41. {
  42. $tpl = $this->mustache->loadTemplate('{{#wrap}}{{name}}{{/wrap}}');
  43. $data = array(
  44. 'name' => 'Bob',
  45. 'wrap' => function ($text) {
  46. return sprintf('[[%s]]', $text);
  47. },
  48. );
  49. $this->assertEquals(sprintf('[[%s]]', $data['name']), $tpl->render($data));
  50. }
  51. }
  52. class Mustache_Test_FiveThree_Functional_Foo
  53. {
  54. public $name = 'Justin';
  55. public $lorem = 'Lorem ipsum dolor sit amet,';
  56. public $wrap;
  57. public function __construct()
  58. {
  59. $this->wrap = function ($text) {
  60. return sprintf('<em>%s</em>', $text);
  61. };
  62. }
  63. }