MustacheSpecTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. class MustacheSpecTest extends PHPUnit_Framework_TestCase {
  8. /**
  9. * For some reason data providers can't mark tests skipped, so this test exists
  10. * simply to provide a 'skipped' test if the `spec` submodule isn't initialized.
  11. */
  12. public function testSpecInitialized() {
  13. $spec_dir = dirname(__FILE__) . '/spec/specs/';
  14. if (!file_exists($spec_dir)) {
  15. $this->markTestSkipped('Mustache spec submodule not initialized: run "git submodule update --init"');
  16. }
  17. }
  18. /**
  19. * @dataProvider loadCommentSpec
  20. */
  21. public function testCommentSpec($template, $data, $partials, $expected, $desc) {
  22. $m = new Mustache($template, $data, $partials);
  23. $this->assertEquals($expected, $m->render(), $desc);
  24. }
  25. /**
  26. * @dataProvider loadDelimitersSpec
  27. */
  28. public function testDelimitersSpec($template, $data, $partials, $expected, $desc) {
  29. $m = new Mustache($template, $data, $partials);
  30. $this->assertEquals($expected, $m->render(), $desc);
  31. }
  32. /**
  33. * @dataProvider loadInterpolationSpec
  34. */
  35. public function testInterpolationSpec($template, $data, $partials, $expected, $desc) {
  36. $m = new Mustache($template, $data, $partials);
  37. $this->assertEquals($expected, $m->render(), $desc);
  38. }
  39. /**
  40. * @dataProvider loadInvertedSpec
  41. */
  42. public function testInvertedSpec($template, $data, $partials, $expected, $desc) {
  43. $m = new Mustache($template, $data, $partials);
  44. $this->assertEquals($expected, $m->render(), $desc);
  45. }
  46. /**
  47. * @dataProvider loadLambdasSpec
  48. */
  49. public function testLambdasSpec($template, $data, $partials, $expected, $desc) {
  50. $this->markTestSkipped("Lambdas for PHP haven't made it into the spec yet, so we'll skip them to avoid a bajillion failed tests.");
  51. if (!version_compare(PHP_VERSION, '5.3.0', '>=')) {
  52. $this->markTestSkipped('Unable to test Lambdas spec with PHP < 5.3.');
  53. }
  54. $m = new Mustache($template, $data, $partials);
  55. $this->assertEquals($expected, $m->render(), $desc);
  56. }
  57. /**
  58. * @dataProvider loadPartialsSpec
  59. */
  60. public function testPartialsSpec($template, $data, $partials, $expected, $desc) {
  61. // skip partial recursion tests, see: http://hile.mn/bYIIYt
  62. if (strpos($desc, 'recurse') !== false) {
  63. $this->markTestSkipped('Mustache.php has an issue with recursive partials. See http://hile.mn/bYIIYt');
  64. }
  65. $m = new Mustache($template, $data, $partials);
  66. $this->assertEquals($expected, $m->render(), $desc);
  67. }
  68. /**
  69. * @dataProvider loadSectionsSpec
  70. */
  71. public function testSectionsSpec($template, $data, $partials, $expected, $desc) {
  72. $m = new Mustache($template, $data, $partials);
  73. $this->assertEquals($expected, $m->render(), $desc);
  74. }
  75. public function loadCommentSpec() {
  76. return $this->loadSpec('comments');
  77. }
  78. public function loadDelimitersSpec() {
  79. return $this->loadSpec('delimiters');
  80. }
  81. public function loadInterpolationSpec() {
  82. return $this->loadSpec('interpolation');
  83. }
  84. public function loadInvertedSpec() {
  85. return $this->loadSpec('inverted');
  86. }
  87. public function loadLambdasSpec() {
  88. return $this->loadSpec('lambdas');
  89. }
  90. public function loadPartialsSpec() {
  91. return $this->loadSpec('partials');
  92. }
  93. public function loadSectionsSpec() {
  94. return $this->loadSpec('sections');
  95. }
  96. /**
  97. * Data provider for the mustache spec test.
  98. *
  99. * Loads YAML files from the spec and converts them to PHPisms.
  100. *
  101. * @access public
  102. * @return array
  103. */
  104. protected function loadSpec($name) {
  105. $filename = dirname(__FILE__) . '/spec/specs/' . $name . '.yml';
  106. if (!file_exists($filename)) {
  107. return array();
  108. }
  109. $data = array();
  110. $spec = spyc_load_file($filename);
  111. foreach ($spec['tests'] as $test) {
  112. $data[] = array($test['template'], $test['data'], isset($test['partials']) ? $test['partials'] : array(), $test['expected'], $test['desc']);
  113. }
  114. return $data;
  115. }
  116. }