MustacheTest.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. <?php
  2. require_once '../Mustache.php';
  3. /**
  4. * A PHPUnit test case for Mustache.php.
  5. *
  6. * This is a very basic, very rudimentary unit test case. It's probably more important to have tests
  7. * than to have elegant tests, so let's bear with it for a bit.
  8. *
  9. * This class assumes an example directory exists at `../examples` with the following structure:
  10. *
  11. * @code
  12. * examples
  13. * foo
  14. * Foo.php
  15. * foo.mustache
  16. * foo.txt
  17. * bar
  18. * Bar.php
  19. * bar.mustache
  20. * bar.txt
  21. * @endcode
  22. *
  23. * To use this test:
  24. *
  25. * 1. {@link http://www.phpunit.de/manual/current/en/installation.html Install PHPUnit}
  26. * 2. run phpunit from the `test` directory:
  27. * `phpunit MustacheTest`
  28. * 3. Fix bugs. Lather, rinse, repeat.
  29. *
  30. * @extends PHPUnit_Framework_TestCase
  31. */
  32. class MustacheTest extends PHPUnit_Framework_TestCase {
  33. const TEST_CLASS = 'Mustache';
  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. $m2 = $this->getMock(self::TEST_CLASS, array('render'), array());
  73. $m2->expects($this->once())
  74. ->method('render')
  75. ->will($this->returnValue('foo'));
  76. $this->assertEquals('foo', $m2->render());
  77. }
  78. public function test__toStringException() {
  79. $m = $this->getMock(self::TEST_CLASS, array('render'), array());
  80. $m->expects($this->once())
  81. ->method('render')
  82. ->will($this->throwException(new Exception));
  83. try {
  84. $out = (string) $m;
  85. } catch (Exception $e) {
  86. $this->fail('__toString should catch all exceptions');
  87. }
  88. }
  89. /**
  90. * Test render().
  91. *
  92. * @access public
  93. * @return void
  94. */
  95. public function testRender() {
  96. $m = new Mustache();
  97. $this->assertEquals('', $m->render(''));
  98. $this->assertEquals('foo', $m->render('foo'));
  99. $this->assertEquals('', $m->render(null));
  100. $m2 = new Mustache('foo');
  101. $this->assertEquals('foo', $m2->render());
  102. $m3 = new Mustache('');
  103. $this->assertEquals('', $m3->render());
  104. $m3 = new Mustache();
  105. $this->assertEquals('', $m3->render(null));
  106. }
  107. /**
  108. * Test render() with data.
  109. *
  110. * @access public
  111. * @return void
  112. */
  113. public function testRenderWithData() {
  114. $m = new Mustache('{{first_name}} {{last_name}}');
  115. $this->assertEquals('Charlie Chaplin', $m->render(null, array('first_name' => 'Charlie', 'last_name' => 'Chaplin')));
  116. $this->assertEquals('Zappa, Frank', $m->render('{{last_name}}, {{first_name}}', array('first_name' => 'Frank', 'last_name' => 'Zappa')));
  117. }
  118. /**
  119. * Mustache should return the same thing when invoked multiple times.
  120. *
  121. * @access public
  122. * @return void
  123. */
  124. public function testMultipleInvocations() {
  125. $m = new Mustache('x');
  126. $first = $m->render();
  127. $second = $m->render();
  128. $this->assertEquals('x', $first);
  129. $this->assertEquals($first, $second);
  130. }
  131. /**
  132. * Mustache should return the same thing when invoked multiple times.
  133. *
  134. * @access public
  135. * @return void
  136. */
  137. public function testMultipleInvocationsWithTags() {
  138. $m = new Mustache('{{one}} {{two}}', array('one' => 'foo', 'two' => 'bar'));
  139. $first = $m->render();
  140. $second = $m->render();
  141. $this->assertEquals('foo bar', $first);
  142. $this->assertEquals($first, $second);
  143. }
  144. /**
  145. * Mustache should not use templates passed to the render() method for subsequent invocations.
  146. *
  147. * @access public
  148. * @return void
  149. */
  150. public function testResetTemplateForMultipleInvocations() {
  151. $m = new Mustache('Sirve.');
  152. $this->assertEquals('No sirve.', $m->render('No sirve.'));
  153. $this->assertEquals('Sirve.', $m->render());
  154. $m2 = new Mustache();
  155. $this->assertEquals('No sirve.', $m2->render('No sirve.'));
  156. $this->assertEquals('', $m2->render());
  157. }
  158. /**
  159. * testClone function.
  160. *
  161. * @dataProvider getExamples
  162. * @access public
  163. * @return void
  164. */
  165. public function test__clone($class, $template, $output) {
  166. if ($class == 'Delimiters') {
  167. $this->markTestSkipped("Known issue: sections don't respect delimiter changes");
  168. return;
  169. }
  170. $m = new $class;
  171. $n = clone $m;
  172. $n_output = $n->render($template);
  173. $o = clone $n;
  174. $this->assertEquals($m->render($template), $n_output);
  175. $this->assertEquals($n_output, $o->render($template));
  176. $this->assertNotSame($m, $n);
  177. $this->assertNotSame($n, $o);
  178. $this->assertNotSame($m, $o);
  179. }
  180. /**
  181. * Test everything in the `examples` directory.
  182. *
  183. * @dataProvider getExamples
  184. * @access public
  185. * @param mixed $class
  186. * @param mixed $template
  187. * @param mixed $output
  188. * @return void
  189. */
  190. public function testExamples($class, $template, $output) {
  191. if ($class == 'Delimiters') {
  192. $this->markTestSkipped("Known issue: sections don't respect delimiter changes");
  193. return;
  194. }
  195. $m = new $class;
  196. $this->assertEquals($output, $m->render($template));
  197. }
  198. /**
  199. * Data provider for testExamples method.
  200. *
  201. * Assumes that an `examples` directory exists inside parent directory.
  202. * This examples directory should contain any number of subdirectories, each of which contains
  203. * three files: one Mustache class (.php), one Mustache template (.mustache), and one output file
  204. * (.txt).
  205. *
  206. * This whole mess will be refined later to be more intuitive and less prescriptive, but it'll
  207. * do for now. Especially since it means we can have unit tests :)
  208. *
  209. * @access public
  210. * @return array
  211. */
  212. public function getExamples() {
  213. $basedir = dirname(__FILE__) . '/../examples/';
  214. $ret = array();
  215. $files = new RecursiveDirectoryIterator($basedir);
  216. while ($files->valid()) {
  217. if ($files->hasChildren() && $children = $files->getChildren()) {
  218. $example = $files->getSubPathname();
  219. $class = null;
  220. $template = null;
  221. $output = null;
  222. foreach ($children as $file) {
  223. if (!$file->isFile()) continue;
  224. $filename = $file->getPathname();
  225. $info = pathinfo($filename);
  226. if (isset($info['extension'])) {
  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. }
  241. if (!empty($class)) {
  242. $ret[$example] = array($class, $template, $output);
  243. }
  244. }
  245. $files->next();
  246. }
  247. return $ret;
  248. }
  249. public function testCrazyDelimiters() {
  250. $m = new Mustache(null, array('result' => 'success'));
  251. $this->assertEquals('success', $m->render('{{=[[ ]]=}}[[ result ]]'));
  252. $this->assertEquals('success', $m->render('{{=(( ))=}}(( result ))'));
  253. $this->assertEquals('success', $m->render('{{={$ $}=}}{$ result $}'));
  254. $this->assertEquals('success', $m->render('{{=<.. ..>=}}<.. result ..>'));
  255. $this->assertEquals('success', $m->render('{{=^^ ^^}}^^ result ^^'));
  256. $this->assertEquals('success', $m->render('{{=// \\\\}}// result \\\\'));
  257. }
  258. public function testResetDelimiters() {
  259. $m = new Mustache(null, array('result' => 'success'));
  260. $this->assertEquals('success', $m->render('{{=[[ ]]=}}[[ result ]]'));
  261. $this->assertEquals('success', $m->render('{{=<< >>=}}<< result >>'));
  262. $this->assertEquals('success', $m->render('{{=<% %>=}}<% result %>'));
  263. }
  264. }