MustacheTest.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. * Mustache should not use templates passed to the render() method for subsequent invocations.
  147. *
  148. * @access public
  149. * @return void
  150. */
  151. public function testResetTemplateForMultipleInvocations() {
  152. $m = new Mustache('Sirve.');
  153. $this->assertEquals('No sirve.', $m->render('No sirve.'));
  154. $this->assertEquals('Sirve.', $m->render());
  155. $m2 = new Mustache();
  156. $this->assertEquals('No sirve.', $m2->render('No sirve.'));
  157. $this->assertEquals('', $m2->render());
  158. }
  159. /**
  160. * testClone function.
  161. *
  162. * @dataProvider getExamples
  163. * @access public
  164. * @return void
  165. */
  166. public function test__clone($class, $template, $output) {
  167. if ($class == 'Delimiters') {
  168. $this->markTestSkipped("Known issue: sections don't respect delimiter changes");
  169. return;
  170. }
  171. $m = new $class;
  172. $n = clone $m;
  173. $n_output = $n->render($template);
  174. $o = clone $n;
  175. $this->assertEquals($m->render($template), $n_output);
  176. $this->assertEquals($n_output, $o->render($template));
  177. $this->assertNotSame($m, $n);
  178. $this->assertNotSame($n, $o);
  179. $this->assertNotSame($m, $o);
  180. }
  181. /**
  182. * Test everything in the `examples` directory.
  183. *
  184. * @dataProvider getExamples
  185. * @access public
  186. * @param mixed $class
  187. * @param mixed $template
  188. * @param mixed $output
  189. * @return void
  190. */
  191. public function testExamples($class, $template, $output) {
  192. if ($class == 'Delimiters') {
  193. $this->markTestSkipped("Known issue: sections don't respect delimiter changes");
  194. return;
  195. }
  196. $m = new $class;
  197. $this->assertEquals($output, $m->render($template));
  198. }
  199. /**
  200. * Data provider for testExamples method.
  201. *
  202. * Assumes that an `examples` directory exists inside parent directory.
  203. * This examples directory should contain any number of subdirectories, each of which contains
  204. * three files: one Mustache class (.php), one Mustache template (.mustache), and one output file
  205. * (.txt).
  206. *
  207. * This whole mess will be refined later to be more intuitive and less prescriptive, but it'll
  208. * do for now. Especially since it means we can have unit tests :)
  209. *
  210. * @access public
  211. * @return array
  212. */
  213. public function getExamples() {
  214. $basedir = dirname(__FILE__) . '/../examples/';
  215. $ret = array();
  216. $files = new RecursiveDirectoryIterator($basedir);
  217. while ($files->valid()) {
  218. if ($files->hasChildren() && $children = $files->getChildren()) {
  219. $example = $files->getSubPathname();
  220. $class = null;
  221. $template = null;
  222. $output = null;
  223. foreach ($children as $file) {
  224. if (!$file->isFile()) continue;
  225. $filename = $file->getPathInfo();
  226. $info = pathinfo($filename);
  227. switch($info['extension']) {
  228. case 'php':
  229. $class = $info['filename'];
  230. include_once($filename);
  231. break;
  232. case 'mustache':
  233. $template = file_get_contents($filename);
  234. break;
  235. case 'txt':
  236. $output = file_get_contents($filename);
  237. break;
  238. }
  239. }
  240. $ret[$example] = array($class, $template, $output);
  241. }
  242. $files->next();
  243. }
  244. return $ret;
  245. }
  246. public function testCrazyDelimiters() {
  247. $m = new Mustache(null, array('result' => 'success'));
  248. $this->assertEquals('success', $m->render('{{=[[ ]]=}}[[ result ]]'));
  249. $m = new Mustache(null, array('result' => 'success'));
  250. $this->assertEquals('success', $m->render('{{=(( ))=}}(( result ))'));
  251. $m = new Mustache(null, array('result' => 'success'));
  252. $this->assertEquals('success', $m->render('{{={$ $}=}}{$ result $}'));
  253. $m = new Mustache(null, array('result' => 'success'));
  254. $this->assertEquals('success', $m->render('{{=<.. ..>=}}<.. result ..>'));
  255. }
  256. public function testResetDelimiters() {
  257. $m = new Mustache(null, array('result' => 'success'));
  258. $this->assertEquals('success', $m->render('{{=[[ ]]=}}[[ result ]]'));
  259. $this->assertEquals('success', $m->render('{{=<< >>=}}<< result >>'));
  260. $this->assertEquals('success', $m->render('{{=<% %>=}}<% result %>'));
  261. }
  262. }