MustacheSpecTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. require_once '../Mustache.php';
  3. require_once './lib/yaml/lib/sfYamlParser.php';
  4. /**
  5. * A PHPUnit test case wrapping the Mustache Spec
  6. *
  7. * @group mustache-spec
  8. */
  9. class MustacheSpecTest extends PHPUnit_Framework_TestCase {
  10. /**
  11. * For some reason data providers can't mark tests skipped, so this test exists
  12. * simply to provide a 'skipped' test if the `spec` submodule isn't initialized.
  13. */
  14. public function testSpecInitialized() {
  15. $spec_dir = dirname(__FILE__) . '/spec/specs/';
  16. if (!file_exists($spec_dir)) {
  17. $this->markTestSkipped('Mustache spec submodule not initialized: run "git submodule update --init"');
  18. }
  19. }
  20. /**
  21. * @group comments
  22. * @dataProvider loadCommentSpec
  23. */
  24. public function testCommentSpec($template, $data, $partials, $expected, $desc) {
  25. $m = new Mustache($template, $data, $partials);
  26. $this->assertEquals($expected, $m->render(), $desc);
  27. }
  28. /**
  29. * @group delimiters
  30. * @dataProvider loadDelimitersSpec
  31. */
  32. public function testDelimitersSpec($template, $data, $partials, $expected, $desc) {
  33. $m = new Mustache($template, $data, $partials);
  34. $this->assertEquals($expected, $m->render(), $desc);
  35. }
  36. /**
  37. * @group interpolation
  38. * @dataProvider loadInterpolationSpec
  39. */
  40. public function testInterpolationSpec($template, $data, $partials, $expected, $desc) {
  41. $m = new Mustache($template, $data, $partials);
  42. $this->assertEquals($expected, $m->render(), $desc);
  43. }
  44. /**
  45. * @group inverted-sections
  46. * @dataProvider loadInvertedSpec
  47. */
  48. public function testInvertedSpec($template, $data, $partials, $expected, $desc) {
  49. $m = new Mustache($template, $data, $partials);
  50. $this->assertEquals($expected, $m->render(), $desc);
  51. }
  52. /**
  53. * @group lambdas
  54. * @dataProvider loadLambdasSpec
  55. */
  56. public function testLambdasSpec($template, $data, $partials, $expected, $desc) {
  57. if (!version_compare(PHP_VERSION, '5.3.0', '>=')) {
  58. $this->markTestSkipped('Unable to test Lambdas spec with PHP < 5.3.');
  59. }
  60. $data = $this->prepareLambdasSpec($data);
  61. $m = new Mustache($template, $data, $partials);
  62. $this->assertEquals($expected, $m->render(), $desc);
  63. }
  64. /**
  65. * Extract and lambdafy any 'lambda' values found in the $data array.
  66. */
  67. protected function prepareLambdasSpec($data) {
  68. foreach ($data as $key => $val) {
  69. if ($key === 'lambda') {
  70. if (!isset($val['php'])) {
  71. $this->markTestSkipped(sprintf('PHP lambda test not implemented for this test.'));
  72. }
  73. $func = $val['php'];
  74. $data[$key] = function($text = null) use ($func) { return eval($func); };
  75. } else if (is_array($val)) {
  76. $data[$key] = $this->prepareLambdasSpec($val);
  77. }
  78. }
  79. return $data;
  80. }
  81. /**
  82. * @group partials
  83. * @dataProvider loadPartialsSpec
  84. */
  85. public function testPartialsSpec($template, $data, $partials, $expected, $desc) {
  86. $m = new Mustache($template, $data, $partials);
  87. $this->assertEquals($expected, $m->render(), $desc);
  88. }
  89. /**
  90. * @group sections
  91. * @dataProvider loadSectionsSpec
  92. */
  93. public function testSectionsSpec($template, $data, $partials, $expected, $desc) {
  94. $m = new Mustache($template, $data, $partials);
  95. $this->assertEquals($expected, $m->render(), $desc);
  96. }
  97. public function loadCommentSpec() {
  98. return $this->loadSpec('comments');
  99. }
  100. public function loadDelimitersSpec() {
  101. return $this->loadSpec('delimiters');
  102. }
  103. public function loadInterpolationSpec() {
  104. return $this->loadSpec('interpolation');
  105. }
  106. public function loadInvertedSpec() {
  107. return $this->loadSpec('inverted');
  108. }
  109. public function loadLambdasSpec() {
  110. return $this->loadSpec('~lambdas');
  111. }
  112. public function loadPartialsSpec() {
  113. return $this->loadSpec('partials');
  114. }
  115. public function loadSectionsSpec() {
  116. return $this->loadSpec('sections');
  117. }
  118. /**
  119. * Data provider for the mustache spec test.
  120. *
  121. * Loads YAML files from the spec and converts them to PHPisms.
  122. *
  123. * @access public
  124. * @return array
  125. */
  126. protected function loadSpec($name) {
  127. $filename = dirname(__FILE__) . '/spec/specs/' . $name . '.yml';
  128. if (!file_exists($filename)) {
  129. return array();
  130. }
  131. $data = array();
  132. $yaml = new sfYamlParser();
  133. $file = file_get_contents($filename);
  134. // @hack: pre-process the 'lambdas' spec so the Symfony YAML parser doesn't complain.
  135. if ($name === '~lambdas') {
  136. $file = str_replace(" !code\n", "\n", $file);
  137. }
  138. $spec = $yaml->parse($file);
  139. foreach ($spec['tests'] as $test) {
  140. $data[] = array(
  141. $test['template'],
  142. $test['data'],
  143. isset($test['partials']) ? $test['partials'] : array(),
  144. $test['expected'],
  145. $test['desc']
  146. );
  147. }
  148. return $data;
  149. }
  150. }