StrictCallablesTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. /*
  3. * This file is part of Mustache.php.
  4. *
  5. * (c) 2013 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 testStrictCallablesDisabled($name, $section, $expected)
  20. {
  21. $mustache = new Mustache_Engine(array('strict_callables' => false));
  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. array($this, 'instanceName'),
  37. $lambda,
  38. 'YOSHI',
  39. ),
  40. array(
  41. array(__CLASS__, 'staticName'),
  42. $lambda,
  43. 'YOSHI',
  44. ),
  45. array(
  46. function() { return 'Yoshi'; },
  47. $lambda,
  48. 'YOSHI',
  49. ),
  50. // Section lambdas
  51. array(
  52. 'Yoshi',
  53. array($this, 'instanceCallable'),
  54. 'YOSHI',
  55. ),
  56. array(
  57. 'Yoshi',
  58. array(__CLASS__, 'staticCallable'),
  59. 'YOSHI',
  60. ),
  61. array(
  62. 'Yoshi',
  63. $lambda,
  64. 'YOSHI',
  65. ),
  66. );
  67. }
  68. /**
  69. * @group wip
  70. * @dataProvider strictCallables
  71. */
  72. public function testStrictCallablesEnabled($name, $section, $expected)
  73. {
  74. $mustache = new Mustache_Engine(array('strict_callables' => true));
  75. $tpl = $mustache->loadTemplate('{{# section }}{{ name }}{{/ section }}');
  76. $data = new StdClass;
  77. $data->name = $name;
  78. $data->section = $section;
  79. $this->assertEquals($expected, $tpl->render($data));
  80. }
  81. public function strictCallables()
  82. {
  83. $lambda = function($tpl, $mustache) {
  84. return strtoupper($mustache->render($tpl));
  85. };
  86. return array(
  87. // Interpolation lambdas
  88. array(
  89. function() { return 'Yoshi'; },
  90. $lambda,
  91. 'YOSHI',
  92. ),
  93. // Section lambdas
  94. array(
  95. 'Yoshi',
  96. array($this, 'instanceCallable'),
  97. 'YoshiYoshi',
  98. ),
  99. array(
  100. 'Yoshi',
  101. array(__CLASS__, 'staticCallable'),
  102. 'YoshiYoshi',
  103. ),
  104. array(
  105. 'Yoshi',
  106. function($tpl, $mustache) {
  107. return strtoupper($mustache->render($tpl));
  108. },
  109. 'YOSHI',
  110. ),
  111. );
  112. }
  113. public function instanceCallable($tpl, $mustache)
  114. {
  115. return strtoupper($mustache->render($tpl));
  116. }
  117. public static function staticCallable($tpl, $mustache)
  118. {
  119. return strtoupper($mustache->render($tpl));
  120. }
  121. public function instanceName()
  122. {
  123. return 'Yoshi';
  124. }
  125. public static function staticName()
  126. {
  127. return 'Yoshi';
  128. }
  129. }