MustacheTest.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. protected $knownIssues = array(
  35. 'Delimiters' => "Known issue: sections don't respect delimiter changes",
  36. 'SectionsSpaces' => "Known issue: Mustache fails miserably at whitespace",
  37. );
  38. /**
  39. * Test Mustache constructor.
  40. *
  41. * @access public
  42. * @return void
  43. */
  44. public function test__construct() {
  45. $template = '{{#mustaches}}{{#last}}and {{/last}}{{type}}{{^last}}, {{/last}}{{/mustaches}}';
  46. $data = array(
  47. 'mustaches' => array(
  48. array('type' => 'Natural'),
  49. array('type' => 'Hungarian'),
  50. array('type' => 'Dali'),
  51. array('type' => 'English'),
  52. array('type' => 'Imperial'),
  53. array('type' => 'Freestyle', 'last' => 'true'),
  54. )
  55. );
  56. $output = 'Natural, Hungarian, Dali, English, Imperial, and Freestyle';
  57. $m1 = new Mustache();
  58. $this->assertEquals($output, $m1->render($template, $data));
  59. $m2 = new Mustache($template);
  60. $this->assertEquals($output, $m2->render(null, $data));
  61. $m3 = new Mustache($template, $data);
  62. $this->assertEquals($output, $m3->render());
  63. $m4 = new Mustache(null, $data);
  64. $this->assertEquals($output, $m4->render($template));
  65. }
  66. /**
  67. * Test __toString() function.
  68. *
  69. * @access public
  70. * @return void
  71. */
  72. public function test__toString() {
  73. $m = new Mustache('{{first_name}} {{last_name}}', array('first_name' => 'Karl', 'last_name' => 'Marx'));
  74. $this->assertEquals('Karl Marx', $m->__toString());
  75. $this->assertEquals('Karl Marx', (string) $m);
  76. $m2 = $this->getMock(self::TEST_CLASS, array('render'), array());
  77. $m2->expects($this->once())
  78. ->method('render')
  79. ->will($this->returnValue('foo'));
  80. $this->assertEquals('foo', $m2->render());
  81. }
  82. public function test__toStringException() {
  83. $m = $this->getMock(self::TEST_CLASS, array('render'), array());
  84. $m->expects($this->once())
  85. ->method('render')
  86. ->will($this->throwException(new Exception));
  87. try {
  88. $out = (string) $m;
  89. } catch (Exception $e) {
  90. $this->fail('__toString should catch all exceptions');
  91. }
  92. }
  93. /**
  94. * Test render().
  95. *
  96. * @access public
  97. * @return void
  98. */
  99. public function testRender() {
  100. $m = new Mustache();
  101. $this->assertEquals('', $m->render(''));
  102. $this->assertEquals('foo', $m->render('foo'));
  103. $this->assertEquals('', $m->render(null));
  104. $m2 = new Mustache('foo');
  105. $this->assertEquals('foo', $m2->render());
  106. $m3 = new Mustache('');
  107. $this->assertEquals('', $m3->render());
  108. $m3 = new Mustache();
  109. $this->assertEquals('', $m3->render(null));
  110. }
  111. /**
  112. * Test render() with data.
  113. *
  114. * @group interpolation
  115. */
  116. public function testRenderWithData() {
  117. $m = new Mustache('{{first_name}} {{last_name}}');
  118. $this->assertEquals('Charlie Chaplin', $m->render(null, array('first_name' => 'Charlie', 'last_name' => 'Chaplin')));
  119. $this->assertEquals('Zappa, Frank', $m->render('{{last_name}}, {{first_name}}', array('first_name' => 'Frank', 'last_name' => 'Zappa')));
  120. }
  121. /**
  122. * @group partials
  123. */
  124. public function testRenderWithPartials() {
  125. $m = new Mustache('{{>stache}}', null, array('stache' => '{{first_name}} {{last_name}}'));
  126. $this->assertEquals('Charlie Chaplin', $m->render(null, array('first_name' => 'Charlie', 'last_name' => 'Chaplin')));
  127. $this->assertEquals('Zappa, Frank', $m->render('{{last_name}}, {{first_name}}', array('first_name' => 'Frank', 'last_name' => 'Zappa')));
  128. }
  129. /**
  130. * Mustache should allow newlines (and other whitespace) in comments and all other tags.
  131. *
  132. * @group comments
  133. */
  134. public function testNewlinesInComments() {
  135. $m = new Mustache("{{! comment \n \t still a comment... }}");
  136. $this->assertEquals('', $m->render());
  137. }
  138. /**
  139. * Mustache should return the same thing when invoked multiple times.
  140. */
  141. public function testMultipleInvocations() {
  142. $m = new Mustache('x');
  143. $first = $m->render();
  144. $second = $m->render();
  145. $this->assertEquals('x', $first);
  146. $this->assertEquals($first, $second);
  147. }
  148. /**
  149. * Mustache should return the same thing when invoked multiple times.
  150. *
  151. * @group interpolation
  152. */
  153. public function testMultipleInvocationsWithTags() {
  154. $m = new Mustache('{{one}} {{two}}', array('one' => 'foo', 'two' => 'bar'));
  155. $first = $m->render();
  156. $second = $m->render();
  157. $this->assertEquals('foo bar', $first);
  158. $this->assertEquals($first, $second);
  159. }
  160. /**
  161. * Mustache should not use templates passed to the render() method for subsequent invocations.
  162. */
  163. public function testResetTemplateForMultipleInvocations() {
  164. $m = new Mustache('Sirve.');
  165. $this->assertEquals('No sirve.', $m->render('No sirve.'));
  166. $this->assertEquals('Sirve.', $m->render());
  167. $m2 = new Mustache();
  168. $this->assertEquals('No sirve.', $m2->render('No sirve.'));
  169. $this->assertEquals('', $m2->render());
  170. }
  171. /**
  172. * Test the __clone() magic function.
  173. *
  174. * @group examples
  175. * @dataProvider getExamples
  176. *
  177. * @param string $class
  178. * @param string $template
  179. * @param string $output
  180. */
  181. public function test__clone($class, $template, $output) {
  182. if (isset($this->knownIssues[$class])) {
  183. return $this->markTestSkipped($this->knownIssues[$class]);
  184. }
  185. $m = new $class;
  186. $n = clone $m;
  187. $n_output = $n->render($template);
  188. $o = clone $n;
  189. $this->assertEquals($m->render($template), $n_output);
  190. $this->assertEquals($n_output, $o->render($template));
  191. $this->assertNotSame($m, $n);
  192. $this->assertNotSame($n, $o);
  193. $this->assertNotSame($m, $o);
  194. }
  195. /**
  196. * Test everything in the `examples` directory.
  197. *
  198. * @group examples
  199. * @dataProvider getExamples
  200. *
  201. * @param string $class
  202. * @param string $template
  203. * @param string $output
  204. */
  205. public function testExamples($class, $template, $output) {
  206. if (isset($this->knownIssues[$class])) {
  207. return $this->markTestSkipped($this->knownIssues[$class]);
  208. }
  209. $m = new $class;
  210. $this->assertEquals($output, $m->render($template));
  211. }
  212. /**
  213. * Data provider for testExamples method.
  214. *
  215. * Assumes that an `examples` directory exists inside parent directory.
  216. * This examples directory should contain any number of subdirectories, each of which contains
  217. * three files: one Mustache class (.php), one Mustache template (.mustache), and one output file
  218. * (.txt).
  219. *
  220. * This whole mess will be refined later to be more intuitive and less prescriptive, but it'll
  221. * do for now. Especially since it means we can have unit tests :)
  222. *
  223. * @return array
  224. */
  225. public function getExamples() {
  226. $basedir = dirname(__FILE__) . '/../examples/';
  227. $ret = array();
  228. $files = new RecursiveDirectoryIterator($basedir);
  229. while ($files->valid()) {
  230. if ($files->hasChildren() && $children = $files->getChildren()) {
  231. $example = $files->getSubPathname();
  232. $class = null;
  233. $template = null;
  234. $output = null;
  235. foreach ($children as $file) {
  236. if (!$file->isFile()) continue;
  237. $filename = $file->getPathname();
  238. $info = pathinfo($filename);
  239. if (isset($info['extension'])) {
  240. switch($info['extension']) {
  241. case 'php':
  242. $class = $info['filename'];
  243. include_once($filename);
  244. break;
  245. case 'mustache':
  246. $template = file_get_contents($filename);
  247. break;
  248. case 'txt':
  249. $output = file_get_contents($filename);
  250. break;
  251. }
  252. }
  253. }
  254. if (!empty($class)) {
  255. $ret[$example] = array($class, $template, $output);
  256. }
  257. }
  258. $files->next();
  259. }
  260. return $ret;
  261. }
  262. /**
  263. * @group delimiters
  264. */
  265. public function testCrazyDelimiters() {
  266. $m = new Mustache(null, array('result' => 'success'));
  267. $this->assertEquals('success', $m->render('{{=[[ ]]=}}[[ result ]]'));
  268. $this->assertEquals('success', $m->render('{{=(( ))=}}(( result ))'));
  269. $this->assertEquals('success', $m->render('{{={$ $}=}}{$ result $}'));
  270. $this->assertEquals('success', $m->render('{{=<.. ..>=}}<.. result ..>'));
  271. $this->assertEquals('success', $m->render('{{=^^ ^^}}^^ result ^^'));
  272. $this->assertEquals('success', $m->render('{{=// \\\\}}// result \\\\'));
  273. }
  274. /**
  275. * @group delimiters
  276. */
  277. public function testResetDelimiters() {
  278. $m = new Mustache(null, array('result' => 'success'));
  279. $this->assertEquals('success', $m->render('{{=[[ ]]=}}[[ result ]]'));
  280. $this->assertEquals('success', $m->render('{{=<< >>=}}<< result >>'));
  281. $this->assertEquals('success', $m->render('{{=<% %>=}}<% result %>'));
  282. }
  283. /**
  284. * @group sections
  285. * @dataProvider poorlyNestedSections
  286. * @expectedException MustacheException
  287. */
  288. public function testPoorlyNestedSections($template) {
  289. $m = new Mustache($template);
  290. $m->render();
  291. }
  292. public function poorlyNestedSections() {
  293. return array(
  294. array('{{#foo}}'),
  295. array('{{#foo}}{{/bar}}'),
  296. array('{{#foo}}{{#bar}}{{/foo}}'),
  297. array('{{#foo}}{{#bar}}{{/foo}}{{/bar}}'),
  298. array('{{#foo}}{{/bar}}{{/foo}}'),
  299. );
  300. }
  301. }