MustacheHigherOrderSectionsTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. require_once '../Mustache.php';
  3. class MustacheHigherOrderSectionsTest extends PHPUnit_Framework_TestCase {
  4. public function setUp() {
  5. $this->foo = new Foo();
  6. }
  7. public function testAnonymousFunctionSectionCallback() {
  8. if (version_compare(PHP_VERSION, '5.3.0', '<')) {
  9. $this->markTestSkipped('Unable to test anonymous function section callbacks in PHP < 5.3');
  10. return;
  11. }
  12. $this->foo->wrapper = function($text) {
  13. return sprintf('<div class="anonymous">%s</div>', $text);
  14. };
  15. $this->assertEquals(
  16. sprintf('<div class="anonymous">%s</div>', $this->foo->name),
  17. $this->foo->render('{{#wrapper}}{{name}}{{/wrapper}}')
  18. );
  19. }
  20. public function testSectionCallback() {
  21. $this->assertEquals(sprintf('%s', $this->foo->name), $this->foo->render('{{name}}'));
  22. $this->assertEquals(sprintf('<em>%s</em>', $this->foo->name), $this->foo->render('{{#wrap}}{{name}}{{/wrap}}'));
  23. }
  24. public function testRuntimeSectionCallback() {
  25. $this->foo->double_wrap = array($this->foo, 'wrapWithBoth');
  26. $this->assertEquals(
  27. sprintf('<strong><em>%s</em></strong>', $this->foo->name),
  28. $this->foo->render('{{#double_wrap}}{{name}}{{/double_wrap}}')
  29. );
  30. }
  31. public function testFunctionSectionCallback() {
  32. $this->foo->wrapper = 'make_my_logo_bigger';
  33. $this->assertEquals(
  34. sprintf('<h1>%s</h1>', $this->foo->name),
  35. $this->foo->render('{{#wrapper}}{{name}}{{/wrapper}}')
  36. );
  37. }
  38. public function testStaticSectionCallback() {
  39. $this->foo->trimmer = array(get_class($this->foo), 'staticTrim');
  40. $this->assertEquals($this->foo->name, $this->foo->render('{{#trimmer}} {{name}} {{/trimmer}}'));
  41. $this->foo->trimmer = 'Foo::staticTrim';
  42. $this->assertEquals($this->foo->lorem, $this->foo->render('{{#trimmer}} {{lorem}} {{/trimmer}}'));
  43. }
  44. public function testViewArraySectionCallback() {
  45. $data = array(
  46. 'name' => 'Bob',
  47. 'wrap' => 'make_my_logo_bigger',
  48. 'trim' => array(get_class($this->foo), 'staticTrim'),
  49. );
  50. $this->assertEquals(
  51. sprintf('<h1>%s</h1>', $data['name']),
  52. $this->foo->render('{{#wrap}}{{name}}{{/wrap}}', $data)
  53. );
  54. $this->assertEquals($data['name'], $this->foo->render('{{#trim}} {{name}} {{/trim}}', $data));
  55. }
  56. public function testViewArrayAnonymousSectionCallback() {
  57. if (version_compare(PHP_VERSION, '5.3.0', '<')) {
  58. $this->markTestSkipped('Unable to test anonymous function section callbacks in PHP < 5.3');
  59. return;
  60. }
  61. $data = array(
  62. 'name' => 'Bob',
  63. 'wrap' => 'make_my_logo_bigger',
  64. 'anonywrap' => function($text) {
  65. return array('[[%s]]', $text);
  66. }
  67. );
  68. $this->assertEquals(
  69. sprintf('<h1>%s</h1>', $data['name']),
  70. $this->foo->render('{{#wrap}}{{name}}{{/wrap}}', $data)
  71. );
  72. }
  73. }
  74. class Foo extends Mustache {
  75. public $name = 'Justin';
  76. public $lorem = 'Lorem ipsum dolor sit amet,';
  77. public $wrap;
  78. public function __construct($template = null, $view = null, $partials = null) {
  79. $this->wrap = array($this, 'wrapWithEm');
  80. parent::__construct($template, $view, $partials);
  81. }
  82. public function wrapWithEm($text) {
  83. return sprintf('<em>%s</em>', $text);
  84. }
  85. public function wrapWithStrong($text) {
  86. return sprintf('<strong>%s</strong>', $text);
  87. }
  88. public function wrapWithBoth($text) {
  89. return self::wrapWithStrong(self::wrapWithEm($text));
  90. }
  91. public static function staticTrim($text) {
  92. return trim($text);
  93. }
  94. }
  95. function make_my_logo_bigger($text) {
  96. return sprintf('<h1>%s</h1>', $text);
  97. }