MustacheSpecTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. * A PHPUnit test case wrapping the Mustache Spec
  12. *
  13. * @group mustache-spec
  14. * @group functional
  15. */
  16. class Mustache_Test_FiveThree_Functional_MustacheSpecTest extends PHPUnit_Framework_TestCase
  17. {
  18. private static $mustache;
  19. public static function setUpBeforeClass()
  20. {
  21. self::$mustache = new Mustache_Engine;
  22. }
  23. /**
  24. * For some reason data providers can't mark tests skipped, so this test exists
  25. * simply to provide a 'skipped' test if the `spec` submodule isn't initialized.
  26. */
  27. public function testSpecInitialized()
  28. {
  29. if (!file_exists(dirname(__FILE__).'/../../../../../vendor/spec/specs/')) {
  30. $this->markTestSkipped('Mustache spec submodule not initialized: run "git submodule update --init"');
  31. }
  32. }
  33. /**
  34. * @group lambdas
  35. * @dataProvider loadLambdasSpec
  36. */
  37. public function testLambdasSpec($desc, $source, $partials, $data, $expected)
  38. {
  39. $template = self::loadTemplate($source, $partials);
  40. $this->assertEquals($expected, $template($this->prepareLambdasSpec($data)), $desc);
  41. }
  42. public function loadLambdasSpec()
  43. {
  44. return $this->loadSpec('~lambdas');
  45. }
  46. /**
  47. * Extract and lambdafy any 'lambda' values found in the $data array.
  48. */
  49. private function prepareLambdasSpec($data)
  50. {
  51. foreach ($data as $key => $val) {
  52. if ($key === 'lambda') {
  53. if (!isset($val['php'])) {
  54. $this->markTestSkipped(sprintf('PHP lambda test not implemented for this test.'));
  55. }
  56. $func = $val['php'];
  57. $data[$key] = function($text = null) use ($func) {
  58. return eval($func);
  59. };
  60. } elseif (is_array($val)) {
  61. $data[$key] = $this->prepareLambdasSpec($val);
  62. }
  63. }
  64. return $data;
  65. }
  66. /**
  67. * Data provider for the mustache spec test.
  68. *
  69. * Loads YAML files from the spec and converts them to PHPisms.
  70. *
  71. * @access public
  72. * @return array
  73. */
  74. private function loadSpec($name)
  75. {
  76. $filename = dirname(__FILE__) . '/../../../../../vendor/spec/specs/' . $name . '.yml';
  77. if (!file_exists($filename)) {
  78. return array();
  79. }
  80. $data = array();
  81. $yaml = new sfYamlParser;
  82. $file = file_get_contents($filename);
  83. // @hack: pre-process the 'lambdas' spec so the Symfony YAML parser doesn't complain.
  84. if ($name === '~lambdas') {
  85. $file = str_replace(" !code\n", "\n", $file);
  86. }
  87. $spec = $yaml->parse($file);
  88. foreach ($spec['tests'] as $test) {
  89. $data[] = array(
  90. $test['name'] . ': ' . $test['desc'],
  91. $test['template'],
  92. isset($test['partials']) ? $test['partials'] : array(),
  93. $test['data'],
  94. $test['expected'],
  95. );
  96. }
  97. return $data;
  98. }
  99. private static function loadTemplate($source, $partials)
  100. {
  101. self::$mustache->setPartials($partials);
  102. return self::$mustache->loadTemplate($source);
  103. }
  104. }