MustacheSpecTest.php 4.6 KB

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