MustacheTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 Mustache constructor.
  36. *
  37. * @access public
  38. * @return void
  39. */
  40. public function test__construct() {
  41. $template = '{{#mustaches}}{{#last}}and {{/last}}{{type}}{{^last}}, {{/last}}{{/mustaches}}';
  42. $data = array(
  43. 'mustaches' => array(
  44. array('type' => 'Natural'),
  45. array('type' => 'Hungarian'),
  46. array('type' => 'Dali'),
  47. array('type' => 'English'),
  48. array('type' => 'Imperial'),
  49. array('type' => 'Freestyle', 'last' => 'true'),
  50. )
  51. );
  52. $output = 'Natural, Hungarian, Dali, English, Imperial, and Freestyle';
  53. $m1 = new Mustache();
  54. $this->assertEquals($output, $m1->render($template, $data));
  55. $m2 = new Mustache($template);
  56. $this->assertEquals($output, $m2->render(null, $data));
  57. $m3 = new Mustache($template, $data);
  58. $this->assertEquals($output, $m3->render());
  59. $m4 = new Mustache(null, $data);
  60. $this->assertEquals($output, $m4->render($template));
  61. }
  62. /**
  63. * Test everything in the `examples` directory.
  64. *
  65. * @dataProvider getExamples
  66. * @access public
  67. * @param mixed $class
  68. * @param mixed $template
  69. * @param mixed $output
  70. * @return void
  71. */
  72. public function testExamples($class, $template, $output) {
  73. $m = new $class;
  74. $this->assertEquals($output, $m->render($template));
  75. }
  76. /**
  77. * Test render().
  78. *
  79. * @access public
  80. * @return void
  81. */
  82. public function testRender() {
  83. $m = new Mustache();
  84. $this->assertEquals('', $m->render(''));
  85. $this->assertEquals('foo', $m->render('foo'));
  86. $this->assertEquals('', $m->render(null));
  87. $m2 = new Mustache('foo');
  88. $this->assertEquals('foo', $m2->render());
  89. $m3 = new Mustache('');
  90. $this->assertEquals('', $m3->render());
  91. $m3 = new Mustache();
  92. $this->assertEquals('', $m3->render(null));
  93. }
  94. /**
  95. * Test render() with data.
  96. *
  97. * @access public
  98. * @return void
  99. */
  100. public function testRenderWithData() {
  101. $m = new Mustache('{{first_name}} {{last_name}}');
  102. $this->assertEquals('Charlie Chaplin', $m->render(null, array('first_name' => 'Charlie', 'last_name' => 'Chaplin')));
  103. $this->assertEquals('Zappa, Frank', $m->render('{{last_name}}, {{first_name}}', array('first_name' => 'Frank', 'last_name' => 'Zappa')));
  104. }
  105. /**
  106. * Data provider for testExamples method.
  107. *
  108. * Assumes that an `examples` directory exists inside parent directory.
  109. * This examples directory should contain any number of subdirectories, each of which contains
  110. * three files: one Mustache class (.php), one Mustache template (.mustache), and one output file
  111. * (.txt).
  112. *
  113. * This whole mess will be refined later to be more intuitive and less prescriptive, but it'll
  114. * do for now. Especially since it means we can have unit tests :)
  115. *
  116. * @access public
  117. * @return array
  118. */
  119. public function getExamples() {
  120. $basedir = dirname(__FILE__) . '/../examples/';
  121. $ret = array();
  122. $files = new RecursiveDirectoryIterator($basedir);
  123. while ($files->valid()) {
  124. if ($files->hasChildren() && $children = $files->getChildren()) {
  125. $example = $files->getSubPathname();
  126. $class = null;
  127. $template = null;
  128. $output = null;
  129. foreach ($children as $file) {
  130. if (!$file->isFile()) continue;
  131. $filename = $file->getPathInfo();
  132. $info = pathinfo($filename);
  133. switch($info['extension']) {
  134. case 'php':
  135. $class = $info['filename'];
  136. include_once($filename);
  137. break;
  138. case 'mustache':
  139. $template = file_get_contents($filename);
  140. break;
  141. case 'txt':
  142. $output = file_get_contents($filename);
  143. break;
  144. }
  145. }
  146. $ret[$example] = array($class, $template, $output);
  147. }
  148. $files->next();
  149. }
  150. return $ret;
  151. }
  152. }