MustacheTest.php 10 KB

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