MustacheHigherOrderSectionsTest.php 3.3 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 testStaticSectionCallback() {
  32. $this->foo->trimmer = array(get_class($this->foo), 'staticTrim');
  33. $this->assertEquals($this->foo->name, $this->foo->render('{{#trimmer}} {{name}} {{/trimmer}}'));
  34. $this->foo->trimmer = 'Foo::staticTrim';
  35. $this->assertEquals($this->foo->lorem, $this->foo->render('{{#trimmer}} {{lorem}} {{/trimmer}}'));
  36. }
  37. public function testViewArraySectionCallback() {
  38. $data = array(
  39. 'name' => 'Bob',
  40. 'trim' => array(get_class($this->foo), 'staticTrim'),
  41. );
  42. $this->assertEquals($data['name'], $this->foo->render('{{#trim}} {{name}} {{/trim}}', $data));
  43. }
  44. public function testViewArrayAnonymousSectionCallback() {
  45. if (version_compare(PHP_VERSION, '5.3.0', '<')) {
  46. $this->markTestSkipped('Unable to test anonymous function section callbacks in PHP < 5.3');
  47. return;
  48. }
  49. $data = array(
  50. 'name' => 'Bob',
  51. 'wrap' => function($text) {
  52. return sprintf('[[%s]]', $text);
  53. }
  54. );
  55. $this->assertEquals(
  56. sprintf('[[%s]]', $data['name']),
  57. $this->foo->render('{{#wrap}}{{name}}{{/wrap}}', $data)
  58. );
  59. }
  60. public function testMonsters() {
  61. $frank = new Monster();
  62. $frank->title = 'Dr.';
  63. $frank->name = 'Frankenstein';
  64. $this->assertEquals('Dr. Frankenstein', $frank->render());
  65. $dracula = new Monster();
  66. $dracula->title = 'Count';
  67. $dracula->name = 'Dracula';
  68. $this->assertEquals('Count Dracula', $dracula->render());
  69. }
  70. }
  71. class Foo extends Mustache {
  72. public $name = 'Justin';
  73. public $lorem = 'Lorem ipsum dolor sit amet,';
  74. public $wrap;
  75. public function __construct($template = null, $view = null, $partials = null) {
  76. $this->wrap = array($this, 'wrapWithEm');
  77. parent::__construct($template, $view, $partials);
  78. }
  79. public function wrapWithEm($text) {
  80. return sprintf('<em>%s</em>', $text);
  81. }
  82. public function wrapWithStrong($text) {
  83. return sprintf('<strong>%s</strong>', $text);
  84. }
  85. public function wrapWithBoth($text) {
  86. return self::wrapWithStrong(self::wrapWithEm($text));
  87. }
  88. public static function staticTrim($text) {
  89. return trim($text);
  90. }
  91. }
  92. class Monster extends Mustache {
  93. public $_template = '{{#title}}{{title}} {{/title}}{{name}}';
  94. public $title;
  95. public $name;
  96. }