LambdaHelperTest.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. /*
  3. * This file is part of Mustache.php.
  4. *
  5. * (c) 2010-2017 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_LambdaHelperTest extends PHPUnit_Framework_TestCase
  15. {
  16. private $mustache;
  17. public function setUp()
  18. {
  19. $this->mustache = new Mustache_Engine();
  20. }
  21. public function testSectionLambdaHelper()
  22. {
  23. $one = $this->mustache->loadTemplate('{{name}}');
  24. $two = $this->mustache->loadTemplate('{{#lambda}}{{name}}{{/lambda}}');
  25. $foo = new StdClass();
  26. $foo->name = 'Mario';
  27. $foo->lambda = function ($text, $mustache) {
  28. return strtoupper($mustache->render($text));
  29. };
  30. $this->assertEquals('Mario', $one->render($foo));
  31. $this->assertEquals('MARIO', $two->render($foo));
  32. }
  33. public function testSectionLambdaHelperRespectsDelimiterChanges()
  34. {
  35. $tpl = $this->mustache->loadTemplate("{{=<% %>=}}\n<%# bang %><% value %><%/ bang %>");
  36. $data = new StdClass();
  37. $data->value = 'hello world';
  38. $data->bang = function ($text, $mustache) {
  39. return $mustache->render($text) . '!';
  40. };
  41. $this->assertEquals('hello world!', $tpl->render($data));
  42. }
  43. public function testLambdaHelperIsInvokable()
  44. {
  45. $one = $this->mustache->loadTemplate('{{name}}');
  46. $two = $this->mustache->loadTemplate('{{#lambda}}{{name}}{{/lambda}}');
  47. $foo = new StdClass();
  48. $foo->name = 'Mario';
  49. $foo->lambda = function ($text, $render) {
  50. return strtoupper($render($text));
  51. };
  52. $this->assertEquals('Mario', $one->render($foo));
  53. $this->assertEquals('MARIO', $two->render($foo));
  54. }
  55. }