MustacheTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. const TEST_CLASS = 'Mustache';
  35. /**
  36. * Test Mustache constructor.
  37. *
  38. * @access public
  39. * @return void
  40. */
  41. public function test__construct() {
  42. $template = '{{#mustaches}}{{#last}}and {{/last}}{{type}}{{^last}}, {{/last}}{{/mustaches}}';
  43. $data = array(
  44. 'mustaches' => array(
  45. array('type' => 'Natural'),
  46. array('type' => 'Hungarian'),
  47. array('type' => 'Dali'),
  48. array('type' => 'English'),
  49. array('type' => 'Imperial'),
  50. array('type' => 'Freestyle', 'last' => 'true'),
  51. )
  52. );
  53. $output = 'Natural, Hungarian, Dali, English, Imperial, and Freestyle';
  54. $m1 = new Mustache();
  55. $this->assertEquals($output, $m1->render($template, $data));
  56. $m2 = new Mustache($template);
  57. $this->assertEquals($output, $m2->render(null, $data));
  58. $m3 = new Mustache($template, $data);
  59. $this->assertEquals($output, $m3->render());
  60. $m4 = new Mustache(null, $data);
  61. $this->assertEquals($output, $m4->render($template));
  62. }
  63. /**
  64. * Test __toString() function.
  65. *
  66. * @access public
  67. * @return void
  68. */
  69. public function test__toString() {
  70. $m = new Mustache('{{first_name}} {{last_name}}', array('first_name' => 'Karl', 'last_name' => 'Marx'));
  71. $this->assertEquals('Karl Marx', $m->__toString());
  72. $this->assertEquals('Karl Marx', (string) $m);
  73. $m2 = $this->getMock(self::TEST_CLASS, array('render'), array());
  74. $m2->expects($this->once())
  75. ->method('render')
  76. ->will($this->returnValue('foo'));
  77. $this->assertEquals('foo', $m2->render());
  78. }
  79. public function test__toStringException() {
  80. $m = $this->getMock(self::TEST_CLASS, array('render'), array());
  81. $m->expects($this->once())
  82. ->method('render')
  83. ->will($this->throwException(new Exception));
  84. try {
  85. $out = (string) $m;
  86. } catch (Exception $e) {
  87. $this->fail('__toString should catch all exceptions');
  88. }
  89. }
  90. /**
  91. * Test render().
  92. *
  93. * @access public
  94. * @return void
  95. */
  96. public function testRender() {
  97. $m = new Mustache();
  98. $this->assertEquals('', $m->render(''));
  99. $this->assertEquals('foo', $m->render('foo'));
  100. $this->assertEquals('', $m->render(null));
  101. $m2 = new Mustache('foo');
  102. $this->assertEquals('foo', $m2->render());
  103. $m3 = new Mustache('');
  104. $this->assertEquals('', $m3->render());
  105. $m3 = new Mustache();
  106. $this->assertEquals('', $m3->render(null));
  107. }
  108. /**
  109. * Test render() with data.
  110. *
  111. * @access public
  112. * @return void
  113. */
  114. public function testRenderWithData() {
  115. $m = new Mustache('{{first_name}} {{last_name}}');
  116. $this->assertEquals('Charlie Chaplin', $m->render(null, array('first_name' => 'Charlie', 'last_name' => 'Chaplin')));
  117. $this->assertEquals('Zappa, Frank', $m->render('{{last_name}}, {{first_name}}', array('first_name' => 'Frank', 'last_name' => 'Zappa')));
  118. }
  119. /**
  120. * Mustache should return the same thing when invoked multiple times.
  121. *
  122. * @access public
  123. * @return void
  124. */
  125. public function testMultipleInvocations() {
  126. $m = new Mustache('x');
  127. $first = $m->render();
  128. $second = $m->render();
  129. $this->assertEquals('x', $first);
  130. $this->assertEquals($first, $second);
  131. }
  132. /**
  133. * Mustache should return the same thing when invoked multiple times.
  134. *
  135. * @access public
  136. * @return void
  137. */
  138. public function testMultipleInvocationsWithTags() {
  139. $m = new Mustache('{{one}} {{two}}', array('one' => 'foo', 'two' => 'bar'));
  140. $first = $m->render();
  141. $second = $m->render();
  142. $this->assertEquals('foo bar', $first);
  143. $this->assertEquals($first, $second);
  144. }
  145. /**
  146. * testClone function.
  147. *
  148. * @dataProvider getExamples
  149. * @access public
  150. * @return void
  151. */
  152. public function testClone($class, $template, $output) {
  153. $m = new $class;
  154. $n = clone $m;
  155. $n_output = $n->render($template);
  156. $o = clone $n;
  157. $this->assertEquals($m->render($template), $n_output);
  158. $this->assertEquals($n_output, $o->render($template));
  159. }
  160. /**
  161. * Test everything in the `examples` directory.
  162. *
  163. * @dataProvider getExamples
  164. * @access public
  165. * @param mixed $class
  166. * @param mixed $template
  167. * @param mixed $output
  168. * @return void
  169. */
  170. public function testExamples($class, $template, $output) {
  171. $m = new $class;
  172. $this->assertEquals($output, $m->render($template));
  173. }
  174. /**
  175. * Data provider for testExamples method.
  176. *
  177. * Assumes that an `examples` directory exists inside parent directory.
  178. * This examples directory should contain any number of subdirectories, each of which contains
  179. * three files: one Mustache class (.php), one Mustache template (.mustache), and one output file
  180. * (.txt).
  181. *
  182. * This whole mess will be refined later to be more intuitive and less prescriptive, but it'll
  183. * do for now. Especially since it means we can have unit tests :)
  184. *
  185. * @access public
  186. * @return array
  187. */
  188. public function getExamples() {
  189. $basedir = dirname(__FILE__) . '/../examples/';
  190. $ret = array();
  191. $files = new RecursiveDirectoryIterator($basedir);
  192. while ($files->valid()) {
  193. if ($files->hasChildren() && $children = $files->getChildren()) {
  194. $example = $files->getSubPathname();
  195. $class = null;
  196. $template = null;
  197. $output = null;
  198. foreach ($children as $file) {
  199. if (!$file->isFile()) continue;
  200. $filename = $file->getPathInfo();
  201. $info = pathinfo($filename);
  202. switch($info['extension']) {
  203. case 'php':
  204. $class = $info['filename'];
  205. include_once($filename);
  206. break;
  207. case 'mustache':
  208. $template = file_get_contents($filename);
  209. break;
  210. case 'txt':
  211. $output = file_get_contents($filename);
  212. break;
  213. }
  214. }
  215. $ret[$example] = array($class, $template, $output);
  216. }
  217. $files->next();
  218. }
  219. return $ret;
  220. }
  221. }