MustacheHigherOrderSectionsTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. public function testMonsters() {
  74. $frank = new Monster();
  75. $frank->title = 'Dr.';
  76. $frank->name = 'Frankenstein';
  77. $this->assertEquals('Dr. Frankenstein', $frank->render());
  78. $dracula = new Monster();
  79. $dracula->title = 'Count';
  80. $dracula->name = 'Dracula';
  81. $this->assertEquals('Count Dracula', $dracula->render());
  82. }
  83. }
  84. class Foo extends Mustache {
  85. public $name = 'Justin';
  86. public $lorem = 'Lorem ipsum dolor sit amet,';
  87. public $wrap;
  88. public function __construct($template = null, $view = null, $partials = null) {
  89. $this->wrap = array($this, 'wrapWithEm');
  90. parent::__construct($template, $view, $partials);
  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. function make_my_logo_bigger($text) {
  106. return sprintf('<h1>%s</h1>', $text);
  107. }
  108. class Monster extends Mustache {
  109. public $_template = '{{#title}}{{title}} {{/title}}{{name}}';
  110. public $title;
  111. public $name;
  112. }