MustacheTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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. * @dataProvider constructorOptions
  67. */
  68. public function testConstructorOptions($options, $charset, $delimiters, $pragmas) {
  69. $mustache = new MustacheExposedOptionsStub(null, null, null, $options);
  70. $this->assertEquals($charset, $mustache->getCharset());
  71. $this->assertEquals($delimiters, $mustache->getDelimiters());
  72. $this->assertEquals($pragmas, $mustache->getPragmas());
  73. }
  74. public function constructorOptions() {
  75. return array(
  76. array(
  77. array(),
  78. 'UTF-8',
  79. array('{{', '}}'),
  80. array(),
  81. ),
  82. array(
  83. array(
  84. 'charset' => 'UTF-8',
  85. 'delimiters' => '<< >>',
  86. 'pragmas' => array(Mustache::PRAGMA_UNESCAPED)
  87. ),
  88. 'UTF-8',
  89. array('<<', '>>'),
  90. array(Mustache::PRAGMA_UNESCAPED),
  91. ),
  92. array(
  93. array(
  94. 'charset' => 'cp866',
  95. 'delimiters' => array('[[[[', ']]]]'),
  96. 'pragmas' => array(Mustache::PRAGMA_UNESCAPED)
  97. ),
  98. 'cp866',
  99. array('[[[[', ']]]]'),
  100. array(Mustache::PRAGMA_UNESCAPED),
  101. ),
  102. );
  103. }
  104. /**
  105. * @expectedException MustacheException
  106. */
  107. public function testConstructorInvalidPragmaOptionsThrowExceptions() {
  108. $mustache = new Mustache(null, null, null, array('pragmas' => array('banana phone')));
  109. }
  110. /**
  111. * Test __toString() function.
  112. *
  113. * @access public
  114. * @return void
  115. */
  116. public function test__toString() {
  117. $m = new Mustache('{{first_name}} {{last_name}}', array('first_name' => 'Karl', 'last_name' => 'Marx'));
  118. $this->assertEquals('Karl Marx', $m->__toString());
  119. $this->assertEquals('Karl Marx', (string) $m);
  120. $m2 = $this->getMock(self::TEST_CLASS, array('render'), array());
  121. $m2->expects($this->once())
  122. ->method('render')
  123. ->will($this->returnValue('foo'));
  124. $this->assertEquals('foo', $m2->render());
  125. }
  126. public function test__toStringException() {
  127. $m = $this->getMock(self::TEST_CLASS, array('render'), array());
  128. $m->expects($this->once())
  129. ->method('render')
  130. ->will($this->throwException(new Exception));
  131. try {
  132. $out = (string) $m;
  133. } catch (Exception $e) {
  134. $this->fail('__toString should catch all exceptions');
  135. }
  136. }
  137. /**
  138. * Test render().
  139. *
  140. * @access public
  141. * @return void
  142. */
  143. public function testRender() {
  144. $m = new Mustache();
  145. $this->assertEquals('', $m->render(''));
  146. $this->assertEquals('foo', $m->render('foo'));
  147. $this->assertEquals('', $m->render(null));
  148. $m2 = new Mustache('foo');
  149. $this->assertEquals('foo', $m2->render());
  150. $m3 = new Mustache('');
  151. $this->assertEquals('', $m3->render());
  152. $m3 = new Mustache();
  153. $this->assertEquals('', $m3->render(null));
  154. }
  155. /**
  156. * Test render() with data.
  157. *
  158. * @group interpolation
  159. */
  160. public function testRenderWithData() {
  161. $m = new Mustache('{{first_name}} {{last_name}}');
  162. $this->assertEquals('Charlie Chaplin', $m->render(null, array('first_name' => 'Charlie', 'last_name' => 'Chaplin')));
  163. $this->assertEquals('Zappa, Frank', $m->render('{{last_name}}, {{first_name}}', array('first_name' => 'Frank', 'last_name' => 'Zappa')));
  164. }
  165. /**
  166. * @group partials
  167. */
  168. public function testRenderWithPartials() {
  169. $m = new Mustache('{{>stache}}', null, array('stache' => '{{first_name}} {{last_name}}'));
  170. $this->assertEquals('Charlie Chaplin', $m->render(null, array('first_name' => 'Charlie', 'last_name' => 'Chaplin')));
  171. $this->assertEquals('Zappa, Frank', $m->render('{{last_name}}, {{first_name}}', array('first_name' => 'Frank', 'last_name' => 'Zappa')));
  172. }
  173. /**
  174. * @group partials
  175. */
  176. public function testRenderDelimitersInSections() {
  177. $m = new Mustache('{{#a}}{{=<% %>=}}{{b}} c<%={{ }}=%>{{/a}}');
  178. $this->assertEquals('{{b}} c', $m->render(null, array(
  179. 'a' => array(
  180. array('b' => 'Do Not Render')
  181. )
  182. )));
  183. }
  184. /**
  185. * Mustache should allow newlines (and other whitespace) in comments and all other tags.
  186. *
  187. * @group comments
  188. */
  189. public function testNewlinesInComments() {
  190. $m = new Mustache("{{! comment \n \t still a comment... }}");
  191. $this->assertEquals('', $m->render());
  192. }
  193. /**
  194. * Mustache should return the same thing when invoked multiple times.
  195. */
  196. public function testMultipleInvocations() {
  197. $m = new Mustache('x');
  198. $first = $m->render();
  199. $second = $m->render();
  200. $this->assertEquals('x', $first);
  201. $this->assertEquals($first, $second);
  202. }
  203. /**
  204. * Mustache should return the same thing when invoked multiple times.
  205. *
  206. * @group interpolation
  207. */
  208. public function testMultipleInvocationsWithTags() {
  209. $m = new Mustache('{{one}} {{two}}', array('one' => 'foo', 'two' => 'bar'));
  210. $first = $m->render();
  211. $second = $m->render();
  212. $this->assertEquals('foo bar', $first);
  213. $this->assertEquals($first, $second);
  214. }
  215. /**
  216. * Mustache should not use templates passed to the render() method for subsequent invocations.
  217. */
  218. public function testResetTemplateForMultipleInvocations() {
  219. $m = new Mustache('Sirve.');
  220. $this->assertEquals('No sirve.', $m->render('No sirve.'));
  221. $this->assertEquals('Sirve.', $m->render());
  222. $m2 = new Mustache();
  223. $this->assertEquals('No sirve.', $m2->render('No sirve.'));
  224. $this->assertEquals('', $m2->render());
  225. }
  226. /**
  227. * Test the __clone() magic function.
  228. *
  229. * @group examples
  230. * @dataProvider getExamples
  231. *
  232. * @param string $class
  233. * @param string $template
  234. * @param string $output
  235. */
  236. public function test__clone($class, $template, $output) {
  237. if (isset($this->knownIssues[$class])) {
  238. return $this->markTestSkipped($this->knownIssues[$class]);
  239. }
  240. $m = new $class;
  241. $n = clone $m;
  242. $n_output = $n->render($template);
  243. $o = clone $n;
  244. $this->assertEquals($m->render($template), $n_output);
  245. $this->assertEquals($n_output, $o->render($template));
  246. $this->assertNotSame($m, $n);
  247. $this->assertNotSame($n, $o);
  248. $this->assertNotSame($m, $o);
  249. }
  250. /**
  251. * Test everything in the `examples` directory.
  252. *
  253. * @group examples
  254. * @dataProvider getExamples
  255. *
  256. * @param string $class
  257. * @param string $template
  258. * @param string $output
  259. */
  260. public function testExamples($class, $template, $output) {
  261. if (isset($this->knownIssues[$class])) {
  262. return $this->markTestSkipped($this->knownIssues[$class]);
  263. }
  264. $m = new $class;
  265. $this->assertEquals($output, $m->render($template));
  266. }
  267. /**
  268. * Data provider for testExamples method.
  269. *
  270. * Assumes that an `examples` directory exists inside parent directory.
  271. * This examples directory should contain any number of subdirectories, each of which contains
  272. * three files: one Mustache class (.php), one Mustache template (.mustache), and one output file
  273. * (.txt).
  274. *
  275. * This whole mess will be refined later to be more intuitive and less prescriptive, but it'll
  276. * do for now. Especially since it means we can have unit tests :)
  277. *
  278. * @return array
  279. */
  280. public function getExamples() {
  281. $basedir = dirname(__FILE__) . '/../examples/';
  282. $ret = array();
  283. $files = new RecursiveDirectoryIterator($basedir);
  284. while ($files->valid()) {
  285. if ($files->hasChildren() && $children = $files->getChildren()) {
  286. $example = $files->getSubPathname();
  287. $class = null;
  288. $template = null;
  289. $output = null;
  290. foreach ($children as $file) {
  291. if (!$file->isFile()) continue;
  292. $filename = $file->getPathname();
  293. $info = pathinfo($filename);
  294. if (isset($info['extension'])) {
  295. switch($info['extension']) {
  296. case 'php':
  297. $class = $info['filename'];
  298. include_once($filename);
  299. break;
  300. case 'mustache':
  301. $template = file_get_contents($filename);
  302. break;
  303. case 'txt':
  304. $output = file_get_contents($filename);
  305. break;
  306. }
  307. }
  308. }
  309. if (!empty($class)) {
  310. $ret[$example] = array($class, $template, $output);
  311. }
  312. }
  313. $files->next();
  314. }
  315. return $ret;
  316. }
  317. /**
  318. * @group delimiters
  319. */
  320. public function testCrazyDelimiters() {
  321. $m = new Mustache(null, array('result' => 'success'));
  322. $this->assertEquals('success', $m->render('{{=[[ ]]=}}[[ result ]]'));
  323. $this->assertEquals('success', $m->render('{{=(( ))=}}(( result ))'));
  324. $this->assertEquals('success', $m->render('{{={$ $}=}}{$ result $}'));
  325. $this->assertEquals('success', $m->render('{{=<.. ..>=}}<.. result ..>'));
  326. $this->assertEquals('success', $m->render('{{=^^ ^^}}^^ result ^^'));
  327. $this->assertEquals('success', $m->render('{{=// \\\\}}// result \\\\'));
  328. }
  329. /**
  330. * @group delimiters
  331. */
  332. public function testResetDelimiters() {
  333. $m = new Mustache(null, array('result' => 'success'));
  334. $this->assertEquals('success', $m->render('{{=[[ ]]=}}[[ result ]]'));
  335. $this->assertEquals('success', $m->render('{{=<< >>=}}<< result >>'));
  336. $this->assertEquals('success', $m->render('{{=<% %>=}}<% result %>'));
  337. }
  338. /**
  339. * @group sections
  340. * @dataProvider poorlyNestedSections
  341. * @expectedException MustacheException
  342. */
  343. public function testPoorlyNestedSections($template) {
  344. $m = new Mustache($template);
  345. $m->render();
  346. }
  347. public function poorlyNestedSections() {
  348. return array(
  349. array('{{#foo}}'),
  350. array('{{#foo}}{{/bar}}'),
  351. array('{{#foo}}{{#bar}}{{/foo}}'),
  352. array('{{#foo}}{{#bar}}{{/foo}}{{/bar}}'),
  353. array('{{#foo}}{{/bar}}{{/foo}}'),
  354. );
  355. }
  356. /**
  357. * Ensure that Mustache doesn't double-render sections (allowing mustache injection).
  358. *
  359. * @group sections
  360. */
  361. public function testMustacheInjection() {
  362. $template = '{{#foo}}{{bar}}{{/foo}}';
  363. $view = array(
  364. 'foo' => true,
  365. 'bar' => '{{win}}',
  366. 'win' => 'FAIL',
  367. );
  368. $m = new Mustache($template, $view);
  369. $this->assertEquals('{{win}}', $m->render());
  370. }
  371. }
  372. class MustacheExposedOptionsStub extends Mustache {
  373. public function getPragmas() {
  374. return $this->_pragmas;
  375. }
  376. public function getCharset() {
  377. return $this->_charset;
  378. }
  379. public function getDelimiters() {
  380. return array($this->_otag, $this->_ctag);
  381. }
  382. }