MustacheTest.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. require_once '../Mustache.php';
  3. require_once 'PHPUnit/Framework.php';
  4. /**
  5. * A PHPUnit test case for Mustache.php.
  6. *
  7. * This is a very basic, very rudimentary unit test case. It's probably more important to have tests
  8. * than to have elegant tests, so let's bear with it for a bit.
  9. *
  10. * This class assumes an example directory exists at `../examples` with the following structure:
  11. *
  12. * @code
  13. * examples
  14. * foo
  15. * Foo.php
  16. * foo.mustache
  17. * foo.txt
  18. * bar
  19. * Bar.php
  20. * bar.mustache
  21. * bar.txt
  22. * @endcode
  23. *
  24. * To use this test:
  25. *
  26. * 1. {@link http://www.phpunit.de/manual/current/en/installation.html Install PHPUnit}
  27. * 2. run phpunit from the `test` directory:
  28. * `phpunit MustacheTest`
  29. * 3. Fix bugs. Lather, rinse, repeat.
  30. *
  31. * @extends PHPUnit_Framework_TestCase
  32. */
  33. class MustacheTest extends PHPUnit_Framework_TestCase {
  34. /**
  35. * Test everything in the `examples` directory.
  36. *
  37. * @dataProvider getExamples
  38. * @access public
  39. * @param mixed $class
  40. * @param mixed $template
  41. * @param mixed $output
  42. * @return void
  43. */
  44. public function testExamples($class, $template, $output) {
  45. $m = new $class;
  46. $this->assertEquals($m->render($template), $output);
  47. }
  48. /**
  49. * Data provider for testExamples method.
  50. *
  51. * Assumes that an `examples` directory exists inside parent directory.
  52. * This examples directory should contain any number of subdirectories, each of which contains
  53. * three files: one Mustache class (.php), one Mustache template (.mustache), and one output file
  54. * (.txt).
  55. *
  56. * This whole mess will be refined later to be more intuitive and less prescriptive, but it'll
  57. * do for now. Especially since it means we can have unit tests :)
  58. *
  59. * @access public
  60. * @return array
  61. */
  62. public function getExamples() {
  63. $basedir = dirname(__FILE__) . '/../examples/';
  64. $ret = array();
  65. $files = new RecursiveDirectoryIterator($basedir);
  66. while ($files->valid()) {
  67. if ($files->hasChildren() && $children = $files->getChildren()) {
  68. $example = $files->getSubPathname();
  69. $class = null;
  70. $template = null;
  71. $output = null;
  72. foreach ($children as $file) {
  73. if (!$file->isFile()) continue;
  74. $filename = $file->getPathInfo();
  75. $info = pathinfo($filename);
  76. switch($info['extension']) {
  77. case 'php':
  78. $class = $info['filename'];
  79. include_once($filename);
  80. break;
  81. case 'mustache':
  82. $template = file_get_contents($filename);
  83. break;
  84. case 'txt':
  85. $output = file_get_contents($filename);
  86. break;
  87. }
  88. }
  89. $ret[$example] = array($class, $template, $output);
  90. }
  91. $files->next();
  92. }
  93. return $ret;
  94. }
  95. }