ExamplesTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /*
  3. * This file is part of Mustache.php.
  4. *
  5. * (c) 2010-2017 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. * @group examples
  12. * @group functional
  13. */
  14. class Mustache_Test_Functional_ExamplesTest extends PHPUnit_Framework_TestCase
  15. {
  16. /**
  17. * Test everything in the `examples` directory.
  18. *
  19. * @dataProvider getExamples
  20. *
  21. * @param string $context
  22. * @param string $source
  23. * @param array $partials
  24. * @param string $expected
  25. */
  26. public function testExamples($context, $source, $partials, $expected)
  27. {
  28. $mustache = new Mustache_Engine(array(
  29. 'partials' => $partials,
  30. ));
  31. $this->assertEquals($expected, $mustache->loadTemplate($source)->render($context));
  32. }
  33. /**
  34. * Data provider for testExamples method.
  35. *
  36. * Loads examples from the test fixtures directory.
  37. *
  38. * This examples directory should contain any number of subdirectories, each of which contains
  39. * three files: one Mustache class (.php), one Mustache template (.mustache), and one output file
  40. * (.txt). Optionally, the directory may contain a folder full of partials.
  41. *
  42. * @return array
  43. */
  44. public function getExamples()
  45. {
  46. $path = realpath(dirname(__FILE__) . '/../../../fixtures/examples');
  47. $examples = array();
  48. $handle = opendir($path);
  49. while (($file = readdir($handle)) !== false) {
  50. if ($file === '.' || $file === '..') {
  51. continue;
  52. }
  53. $fullpath = $path . '/' . $file;
  54. if (is_dir($fullpath)) {
  55. $examples[$file] = $this->loadExample($fullpath);
  56. }
  57. }
  58. closedir($handle);
  59. return $examples;
  60. }
  61. /**
  62. * Helper method to load an example given the full path.
  63. *
  64. * @param string $path
  65. *
  66. * @return array arguments for testExamples
  67. */
  68. private function loadExample($path)
  69. {
  70. $context = null;
  71. $source = null;
  72. $partials = array();
  73. $expected = null;
  74. $handle = opendir($path);
  75. while (($file = readdir($handle)) !== false) {
  76. $fullpath = $path . '/' . $file;
  77. $info = pathinfo($fullpath);
  78. if (is_dir($fullpath) && $info['basename'] === 'partials') {
  79. // load partials
  80. $partials = $this->loadPartials($fullpath);
  81. } elseif (is_file($fullpath)) {
  82. // load other files
  83. switch ($info['extension']) {
  84. case 'php':
  85. require_once $fullpath;
  86. $className = $info['filename'];
  87. $context = new $className();
  88. break;
  89. case 'mustache':
  90. $source = file_get_contents($fullpath);
  91. break;
  92. case 'txt':
  93. $expected = file_get_contents($fullpath);
  94. break;
  95. }
  96. }
  97. }
  98. closedir($handle);
  99. return array($context, $source, $partials, $expected);
  100. }
  101. /**
  102. * Helper method to load partials given an example directory.
  103. *
  104. * @param string $path
  105. *
  106. * @return array $partials
  107. */
  108. private function loadPartials($path)
  109. {
  110. $partials = array();
  111. $handle = opendir($path);
  112. while (($file = readdir($handle)) !== false) {
  113. if ($file === '.' || $file === '..') {
  114. continue;
  115. }
  116. $fullpath = $path . '/' . $file;
  117. $info = pathinfo($fullpath);
  118. if ($info['extension'] === 'mustache') {
  119. $partials[$info['filename']] = file_get_contents($fullpath);
  120. }
  121. }
  122. closedir($handle);
  123. return $partials;
  124. }
  125. }