MustacheSpecTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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($desc, $template, $data, $partials, $expected) {
  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($desc, $template, $data, $partials, $expected) {
  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($desc, $template, $data, $partials, $expected) {
  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($desc, $template, $data, $partials, $expected) {
  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($desc, $template, $data, $partials, $expected) {
  57. // $this->markTestSkipped("Lambdas for PHP haven't made it into the spec yet, so we'll skip them to avoid a bajillion failed tests.");
  58. //
  59. // if (!version_compare(PHP_VERSION, '5.3.0', '>=')) {
  60. // $this->markTestSkipped('Unable to test Lambdas spec with PHP < 5.3.');
  61. // }
  62. //
  63. // $m = new Mustache($template, $data, $partials);
  64. // $this->assertEquals($expected, $m->render(), $desc);
  65. // }
  66. /**
  67. * @group partials
  68. * @dataProvider loadPartialsSpec
  69. */
  70. public function testPartialsSpec($desc, $template, $data, $partials, $expected) {
  71. $m = new Mustache($template, $data, $partials);
  72. $this->assertEquals($expected, $m->render(), $desc);
  73. }
  74. /**
  75. * @group sections
  76. * @dataProvider loadSectionsSpec
  77. */
  78. public function testSectionsSpec($desc, $template, $data, $partials, $expected) {
  79. $m = new Mustache($template, $data, $partials);
  80. $this->assertEquals($expected, $m->render(), $desc);
  81. }
  82. public function loadCommentSpec() {
  83. return $this->loadSpec('comments');
  84. }
  85. public function loadDelimitersSpec() {
  86. return $this->loadSpec('delimiters');
  87. }
  88. public function loadInterpolationSpec() {
  89. return $this->loadSpec('interpolation');
  90. }
  91. public function loadInvertedSpec() {
  92. return $this->loadSpec('inverted');
  93. }
  94. // public function loadLambdasSpec() {
  95. // return $this->loadSpec('lambdas');
  96. // }
  97. public function loadPartialsSpec() {
  98. return $this->loadSpec('partials');
  99. }
  100. public function loadSectionsSpec() {
  101. return $this->loadSpec('sections');
  102. }
  103. /**
  104. * Data provider for the mustache spec test.
  105. *
  106. * Loads YAML files from the spec and converts them to PHPisms.
  107. *
  108. * @access public
  109. * @return array
  110. */
  111. protected function loadSpec($name) {
  112. $filename = dirname(__FILE__) . '/spec/specs/' . $name . '.yml';
  113. if (!file_exists($filename)) {
  114. return array();
  115. }
  116. $data = array();
  117. $yaml = new sfYamlParser();
  118. $spec = $yaml->parse(file_get_contents($filename));
  119. foreach ($spec['tests'] as $test) {
  120. $data[] = array($test['name'] . ': ' . $test['desc'], $test['template'], $test['data'], isset($test['partials']) ? $test['partials'] : array(), $test['expected']);
  121. }
  122. return $data;
  123. }
  124. }