ExamplesTest.php 3.1 KB

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