MustacheTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. * Test everything in the `examples` directory.
  147. *
  148. * @dataProvider getExamples
  149. * @access public
  150. * @param mixed $class
  151. * @param mixed $template
  152. * @param mixed $output
  153. * @return void
  154. */
  155. public function testExamples($class, $template, $output) {
  156. $m = new $class;
  157. $this->assertEquals($output, $m->render($template));
  158. }
  159. /**
  160. * Data provider for testExamples method.
  161. *
  162. * Assumes that an `examples` directory exists inside parent directory.
  163. * This examples directory should contain any number of subdirectories, each of which contains
  164. * three files: one Mustache class (.php), one Mustache template (.mustache), and one output file
  165. * (.txt).
  166. *
  167. * This whole mess will be refined later to be more intuitive and less prescriptive, but it'll
  168. * do for now. Especially since it means we can have unit tests :)
  169. *
  170. * @access public
  171. * @return array
  172. */
  173. public function getExamples() {
  174. $basedir = dirname(__FILE__) . '/../examples/';
  175. $ret = array();
  176. $files = new RecursiveDirectoryIterator($basedir);
  177. while ($files->valid()) {
  178. if ($files->hasChildren() && $children = $files->getChildren()) {
  179. $example = $files->getSubPathname();
  180. $class = null;
  181. $template = null;
  182. $output = null;
  183. foreach ($children as $file) {
  184. if (!$file->isFile()) continue;
  185. $filename = $file->getPathInfo();
  186. $info = pathinfo($filename);
  187. switch($info['extension']) {
  188. case 'php':
  189. $class = $info['filename'];
  190. include_once($filename);
  191. break;
  192. case 'mustache':
  193. $template = file_get_contents($filename);
  194. break;
  195. case 'txt':
  196. $output = file_get_contents($filename);
  197. break;
  198. }
  199. }
  200. $ret[$example] = array($class, $template, $output);
  201. }
  202. $files->next();
  203. }
  204. return $ret;
  205. }
  206. }