MustacheTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 __toString() function.
  64. *
  65. * @access public
  66. * @return void
  67. */
  68. public function test__toString() {
  69. $m = new Mustache('{{first_name}} {{last_name}}', array('first_name' => 'Karl', 'last_name' => 'Marx'));
  70. $this->assertEquals('Karl Marx', $m->__toString());
  71. $this->assertEquals('Karl Marx', (string) $m);
  72. }
  73. /**
  74. * Test render().
  75. *
  76. * @access public
  77. * @return void
  78. */
  79. public function testRender() {
  80. $m = new Mustache();
  81. $this->assertEquals('', $m->render(''));
  82. $this->assertEquals('foo', $m->render('foo'));
  83. $this->assertEquals('', $m->render(null));
  84. $m2 = new Mustache('foo');
  85. $this->assertEquals('foo', $m2->render());
  86. $m3 = new Mustache('');
  87. $this->assertEquals('', $m3->render());
  88. $m3 = new Mustache();
  89. $this->assertEquals('', $m3->render(null));
  90. }
  91. /**
  92. * Test render() with data.
  93. *
  94. * @access public
  95. * @return void
  96. */
  97. public function testRenderWithData() {
  98. $m = new Mustache('{{first_name}} {{last_name}}');
  99. $this->assertEquals('Charlie Chaplin', $m->render(null, array('first_name' => 'Charlie', 'last_name' => 'Chaplin')));
  100. $this->assertEquals('Zappa, Frank', $m->render('{{last_name}}, {{first_name}}', array('first_name' => 'Frank', 'last_name' => 'Zappa')));
  101. }
  102. /**
  103. * Test everything in the `examples` directory.
  104. *
  105. * @dataProvider getExamples
  106. * @access public
  107. * @param mixed $class
  108. * @param mixed $template
  109. * @param mixed $output
  110. * @return void
  111. */
  112. public function testExamples($class, $template, $output) {
  113. $m = new $class;
  114. $this->assertEquals($output, $m->render($template));
  115. }
  116. /**
  117. * Data provider for testExamples method.
  118. *
  119. * Assumes that an `examples` directory exists inside parent directory.
  120. * This examples directory should contain any number of subdirectories, each of which contains
  121. * three files: one Mustache class (.php), one Mustache template (.mustache), and one output file
  122. * (.txt).
  123. *
  124. * This whole mess will be refined later to be more intuitive and less prescriptive, but it'll
  125. * do for now. Especially since it means we can have unit tests :)
  126. *
  127. * @access public
  128. * @return array
  129. */
  130. public function getExamples() {
  131. $basedir = dirname(__FILE__) . '/../examples/';
  132. $ret = array();
  133. $files = new RecursiveDirectoryIterator($basedir);
  134. while ($files->valid()) {
  135. if ($files->hasChildren() && $children = $files->getChildren()) {
  136. $example = $files->getSubPathname();
  137. $class = null;
  138. $template = null;
  139. $output = null;
  140. foreach ($children as $file) {
  141. if (!$file->isFile()) continue;
  142. $filename = $file->getPathInfo();
  143. $info = pathinfo($filename);
  144. switch($info['extension']) {
  145. case 'php':
  146. $class = $info['filename'];
  147. include_once($filename);
  148. break;
  149. case 'mustache':
  150. $template = file_get_contents($filename);
  151. break;
  152. case 'txt':
  153. $output = file_get_contents($filename);
  154. break;
  155. }
  156. }
  157. $ret[$example] = array($class, $template, $output);
  158. }
  159. $files->next();
  160. }
  161. return $ret;
  162. }
  163. }