MustacheInjectionTest.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /*
  3. * This file is part of Mustache.php.
  4. *
  5. * (c) 2010-2014 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 mustache_injection
  12. * @group functional
  13. */
  14. class Mustache_Test_Functional_MustacheInjectionTest extends PHPUnit_Framework_TestCase
  15. {
  16. private $mustache;
  17. public function setUp()
  18. {
  19. $this->mustache = new Mustache_Engine();
  20. }
  21. /**
  22. * @dataProvider injectionData
  23. */
  24. public function testInjection($tpl, $data, $partials, $expect)
  25. {
  26. $this->mustache->setPartials($partials);
  27. $this->assertEquals($expect, $this->mustache->render($tpl, $data));
  28. }
  29. public function injectionData()
  30. {
  31. $interpolationData = array(
  32. 'a' => '{{ b }}',
  33. 'b' => 'FAIL'
  34. );
  35. $sectionData = array(
  36. 'a' => true,
  37. 'b' => '{{ c }}',
  38. 'c' => 'FAIL'
  39. );
  40. $lambdaInterpolationData = array(
  41. 'a' => array($this, 'lambdaInterpolationCallback'),
  42. 'b' => '{{ c }}',
  43. 'c' => 'FAIL'
  44. );
  45. $lambdaSectionData = array(
  46. 'a' => array($this, 'lambdaSectionCallback'),
  47. 'b' => '{{ c }}',
  48. 'c' => 'FAIL'
  49. );
  50. return array(
  51. array('{{ a }}', $interpolationData, array(), '{{ b }}'),
  52. array('{{{ a }}}', $interpolationData, array(), '{{ b }}'),
  53. array('{{# a }}{{ b }}{{/ a }}', $sectionData, array(), '{{ c }}'),
  54. array('{{# a }}{{{ b }}}{{/ a }}', $sectionData, array(), '{{ c }}'),
  55. array('{{> partial }}', $interpolationData, array('partial' => '{{ a }}'), '{{ b }}'),
  56. array('{{> partial }}', $interpolationData, array('partial' => '{{{ a }}}'), '{{ b }}'),
  57. array('{{ a }}', $lambdaInterpolationData, array(), '{{ c }}'),
  58. array('{{# a }}b{{/ a }}', $lambdaSectionData, array(), '{{ c }}'),
  59. );
  60. }
  61. public static function lambdaInterpolationCallback()
  62. {
  63. return '{{ b }}';
  64. }
  65. public static function lambdaSectionCallback($text)
  66. {
  67. return '{{ ' . $text . ' }}';
  68. }
  69. }