MustacheSpecTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. require_once '../Mustache.php';
  3. require_once './lib/spyc.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. // skip partial recursion tests, see: http://hile.mn/bYIIYt
  70. if (strpos($desc, 'recurse') !== false) {
  71. $this->markTestSkipped('Mustache.php has an issue with recursive partials. See http://hile.mn/bYIIYt');
  72. }
  73. $m = new Mustache($template, $data, $partials);
  74. $this->assertEquals($expected, $m->render(), $desc);
  75. }
  76. /**
  77. * @group sections
  78. * @dataProvider loadSectionsSpec
  79. */
  80. public function testSectionsSpec($template, $data, $partials, $expected, $desc) {
  81. $m = new Mustache($template, $data, $partials);
  82. $this->assertEquals($expected, $m->render(), $desc);
  83. }
  84. public function loadCommentSpec() {
  85. return $this->loadSpec('comments');
  86. }
  87. public function loadDelimitersSpec() {
  88. return $this->loadSpec('delimiters');
  89. }
  90. public function loadInterpolationSpec() {
  91. return $this->loadSpec('interpolation');
  92. }
  93. public function loadInvertedSpec() {
  94. return $this->loadSpec('inverted');
  95. }
  96. public function loadLambdasSpec() {
  97. return $this->loadSpec('lambdas');
  98. }
  99. public function loadPartialsSpec() {
  100. return $this->loadSpec('partials');
  101. }
  102. public function loadSectionsSpec() {
  103. return $this->loadSpec('sections');
  104. }
  105. /**
  106. * Data provider for the mustache spec test.
  107. *
  108. * Loads YAML files from the spec and converts them to PHPisms.
  109. *
  110. * @access public
  111. * @return array
  112. */
  113. protected function loadSpec($name) {
  114. $filename = dirname(__FILE__) . '/spec/specs/' . $name . '.yml';
  115. if (!file_exists($filename)) {
  116. return array();
  117. }
  118. $data = array();
  119. $spec = spyc_load_file($filename);
  120. foreach ($spec['tests'] as $test) {
  121. $data[] = array($test['template'], $test['data'], isset($test['partials']) ? $test['partials'] : array(), $test['expected'], $test['desc']);
  122. }
  123. return $data;
  124. }
  125. }