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