StrictCallablesTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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_StrictCallablesTest extends PHPUnit_Framework_TestCase
  15. {
  16. /**
  17. * @dataProvider callables
  18. */
  19. public function testStrictCallables($strict, $name, $section, $expected)
  20. {
  21. $mustache = new Mustache_Engine(array('strict_callables' => $strict));
  22. $tpl = $mustache->loadTemplate('{{# section }}{{ name }}{{/ section }}');
  23. $data = new StdClass();
  24. $data->name = $name;
  25. $data->section = $section;
  26. $this->assertEquals($expected, $tpl->render($data));
  27. }
  28. public function callables()
  29. {
  30. $lambda = function ($tpl, $mustache) {
  31. return strtoupper($mustache->render($tpl));
  32. };
  33. return array(
  34. // Interpolation lambdas
  35. array(
  36. false,
  37. array($this, 'instanceName'),
  38. $lambda,
  39. 'YOSHI',
  40. ),
  41. array(
  42. false,
  43. array(__CLASS__, 'staticName'),
  44. $lambda,
  45. 'YOSHI',
  46. ),
  47. array(
  48. false,
  49. function () {
  50. return 'Yoshi';
  51. },
  52. $lambda,
  53. 'YOSHI',
  54. ),
  55. // Section lambdas
  56. array(
  57. false,
  58. 'Yoshi',
  59. array($this, 'instanceCallable'),
  60. 'YOSHI',
  61. ),
  62. array(
  63. false,
  64. 'Yoshi',
  65. array(__CLASS__, 'staticCallable'),
  66. 'YOSHI',
  67. ),
  68. array(
  69. false,
  70. 'Yoshi',
  71. $lambda,
  72. 'YOSHI',
  73. ),
  74. // Strict interpolation lambdas
  75. array(
  76. true,
  77. function () {
  78. return 'Yoshi';
  79. },
  80. $lambda,
  81. 'YOSHI',
  82. ),
  83. // Strict section lambdas
  84. array(
  85. true,
  86. 'Yoshi',
  87. array($this, 'instanceCallable'),
  88. 'YoshiYoshi',
  89. ),
  90. array(
  91. true,
  92. 'Yoshi',
  93. array(__CLASS__, 'staticCallable'),
  94. 'YoshiYoshi',
  95. ),
  96. array(
  97. true,
  98. 'Yoshi',
  99. function ($tpl, $mustache) {
  100. return strtoupper($mustache->render($tpl));
  101. },
  102. 'YOSHI',
  103. ),
  104. );
  105. }
  106. public function instanceCallable($tpl, $mustache)
  107. {
  108. return strtoupper($mustache->render($tpl));
  109. }
  110. public static function staticCallable($tpl, $mustache)
  111. {
  112. return strtoupper($mustache->render($tpl));
  113. }
  114. public function instanceName()
  115. {
  116. return 'Yoshi';
  117. }
  118. public static function staticName()
  119. {
  120. return 'Yoshi';
  121. }
  122. }