ExamplesTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /*
  3. * This file is part of Mustache.php.
  4. *
  5. * (c) 2012 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. namespace Mustache\Test\Functional;
  11. use Mustache\Mustache;
  12. /**
  13. * @group examples
  14. * @group functional
  15. */
  16. class ExamplesTest extends \PHPUnit_Framework_TestCase {
  17. /**
  18. * Test everything in the `examples` directory.
  19. *
  20. * @dataProvider getExamples
  21. *
  22. * @param string $context
  23. * @param string $source
  24. * @param array $partials
  25. * @param string $expected
  26. */
  27. public function testExamples($context, $source, $partials, $expected) {
  28. $mustache = new Mustache(array(
  29. 'partials' => $partials
  30. ));
  31. $this->assertEquals($expected, $mustache->loadTemplate($source)->render($context));
  32. }
  33. /**
  34. * Data provider for testExamples method.
  35. *
  36. * Assumes that an `examples` directory exists inside parent directory.
  37. * This examples directory should contain any number of subdirectories, each of which contains
  38. * three files: one Mustache class (.php), one Mustache template (.mustache), and one output file
  39. * (.txt).
  40. *
  41. * This whole mess will be refined later to be more intuitive and less prescriptive, but it'll
  42. * do for now. Especially since it means we can have unit tests :)
  43. *
  44. * @return array
  45. */
  46. public function getExamples() {
  47. $path = realpath(__DIR__.'/../../../../examples');
  48. $examples = array();
  49. $handle = opendir($path);
  50. while (($file = readdir($handle)) !== false) {
  51. if ($file == '.' || $file == '..') {
  52. continue;
  53. }
  54. $fullpath = $path.'/'.$file;
  55. if (is_dir($fullpath)) {
  56. $examples[$file] = $this->loadExample($fullpath);
  57. }
  58. }
  59. closedir($handle);
  60. return $examples;
  61. }
  62. /**
  63. * Helper method to load an example given the full path.
  64. *
  65. * @param string $path
  66. *
  67. * @return array arguments for testExamples
  68. */
  69. private function loadExample($path) {
  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. $context = new $info['filename'];
  87. break;
  88. case 'mustache':
  89. $source = file_get_contents($fullpath);
  90. break;
  91. case 'txt':
  92. $expected = file_get_contents($fullpath);
  93. break;
  94. }
  95. }
  96. }
  97. closedir($handle);
  98. return array($context, $source, $partials, $expected);
  99. }
  100. /**
  101. * Helper method to load partials given an example directory.
  102. *
  103. * @param string $path
  104. *
  105. * @return array $partials
  106. */
  107. private function loadPartials($path) {
  108. $partials = array();
  109. $handle = opendir($path);
  110. while (($file = readdir($handle)) !== false) {
  111. if ($file == '.' || $file == '..') {
  112. continue;
  113. }
  114. $fullpath = $path.'/'.$file;
  115. $info = pathinfo($fullpath);
  116. if ($info['extension'] === 'mustache') {
  117. $partials[$info['filename']] = file_get_contents($fullpath);
  118. }
  119. }
  120. closedir($handle);
  121. return $partials;
  122. }
  123. }