MustacheTest.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. * Mustache should allow newlines (and other whitespace) in comments and all other tags.
  130. *
  131. * @group comments
  132. */
  133. public function testNewlinesInComments() {
  134. $m = new Mustache("{{! comment \n \t still a comment... }}");
  135. $this->assertEquals('', $m->render());
  136. }
  137. /**
  138. * Mustache should return the same thing when invoked multiple times.
  139. */
  140. public function testMultipleInvocations() {
  141. $m = new Mustache('x');
  142. $first = $m->render();
  143. $second = $m->render();
  144. $this->assertEquals('x', $first);
  145. $this->assertEquals($first, $second);
  146. }
  147. /**
  148. * Mustache should return the same thing when invoked multiple times.
  149. *
  150. * @group interpolation
  151. */
  152. public function testMultipleInvocationsWithTags() {
  153. $m = new Mustache('{{one}} {{two}}', array('one' => 'foo', 'two' => 'bar'));
  154. $first = $m->render();
  155. $second = $m->render();
  156. $this->assertEquals('foo bar', $first);
  157. $this->assertEquals($first, $second);
  158. }
  159. /**
  160. * Mustache should not use templates passed to the render() method for subsequent invocations.
  161. */
  162. public function testResetTemplateForMultipleInvocations() {
  163. $m = new Mustache('Sirve.');
  164. $this->assertEquals('No sirve.', $m->render('No sirve.'));
  165. $this->assertEquals('Sirve.', $m->render());
  166. $m2 = new Mustache();
  167. $this->assertEquals('No sirve.', $m2->render('No sirve.'));
  168. $this->assertEquals('', $m2->render());
  169. }
  170. /**
  171. * Test the __clone() magic function.
  172. *
  173. * @group examples
  174. * @dataProvider getExamples
  175. *
  176. * @param string $class
  177. * @param string $template
  178. * @param string $output
  179. */
  180. public function test__clone($class, $template, $output) {
  181. if (isset($this->knownIssues[$class])) {
  182. return $this->markTestSkipped($this->knownIssues[$class]);
  183. }
  184. $m = new $class;
  185. $n = clone $m;
  186. $n_output = $n->render($template);
  187. $o = clone $n;
  188. $this->assertEquals($m->render($template), $n_output);
  189. $this->assertEquals($n_output, $o->render($template));
  190. $this->assertNotSame($m, $n);
  191. $this->assertNotSame($n, $o);
  192. $this->assertNotSame($m, $o);
  193. }
  194. /**
  195. * Test everything in the `examples` directory.
  196. *
  197. * @group examples
  198. * @dataProvider getExamples
  199. *
  200. * @param string $class
  201. * @param string $template
  202. * @param string $output
  203. */
  204. public function testExamples($class, $template, $output) {
  205. if (isset($this->knownIssues[$class])) {
  206. return $this->markTestSkipped($this->knownIssues[$class]);
  207. }
  208. $m = new $class;
  209. $this->assertEquals($output, $m->render($template));
  210. }
  211. /**
  212. * Data provider for testExamples method.
  213. *
  214. * Assumes that an `examples` directory exists inside parent directory.
  215. * This examples directory should contain any number of subdirectories, each of which contains
  216. * three files: one Mustache class (.php), one Mustache template (.mustache), and one output file
  217. * (.txt).
  218. *
  219. * This whole mess will be refined later to be more intuitive and less prescriptive, but it'll
  220. * do for now. Especially since it means we can have unit tests :)
  221. *
  222. * @return array
  223. */
  224. public function getExamples() {
  225. $basedir = dirname(__FILE__) . '/../examples/';
  226. $ret = array();
  227. $files = new RecursiveDirectoryIterator($basedir);
  228. while ($files->valid()) {
  229. if ($files->hasChildren() && $children = $files->getChildren()) {
  230. $example = $files->getSubPathname();
  231. $class = null;
  232. $template = null;
  233. $output = null;
  234. foreach ($children as $file) {
  235. if (!$file->isFile()) continue;
  236. $filename = $file->getPathname();
  237. $info = pathinfo($filename);
  238. if (isset($info['extension'])) {
  239. switch($info['extension']) {
  240. case 'php':
  241. $class = $info['filename'];
  242. include_once($filename);
  243. break;
  244. case 'mustache':
  245. $template = file_get_contents($filename);
  246. break;
  247. case 'txt':
  248. $output = file_get_contents($filename);
  249. break;
  250. }
  251. }
  252. }
  253. if (!empty($class)) {
  254. $ret[$example] = array($class, $template, $output);
  255. }
  256. }
  257. $files->next();
  258. }
  259. return $ret;
  260. }
  261. /**
  262. * @group delimiters
  263. */
  264. public function testCrazyDelimiters() {
  265. $m = new Mustache(null, array('result' => 'success'));
  266. $this->assertEquals('success', $m->render('{{=[[ ]]=}}[[ result ]]'));
  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. }
  273. /**
  274. * @group delimiters
  275. */
  276. public function testResetDelimiters() {
  277. $m = new Mustache(null, array('result' => 'success'));
  278. $this->assertEquals('success', $m->render('{{=[[ ]]=}}[[ result ]]'));
  279. $this->assertEquals('success', $m->render('{{=<< >>=}}<< result >>'));
  280. $this->assertEquals('success', $m->render('{{=<% %>=}}<% result %>'));
  281. }
  282. /**
  283. * @group sections
  284. * @dataProvider poorlyNestedSections
  285. * @expectedException MustacheException
  286. */
  287. public function testPoorlyNestedSections($template) {
  288. $m = new Mustache($template);
  289. $m->render();
  290. }
  291. public function poorlyNestedSections() {
  292. return array(
  293. array('{{#foo}}'),
  294. array('{{#foo}}{{/bar}}'),
  295. array('{{#foo}}{{#bar}}{{/foo}}'),
  296. array('{{#foo}}{{#bar}}{{/foo}}{{/bar}}'),
  297. array('{{#foo}}{{/bar}}{{/foo}}'),
  298. );
  299. }
  300. }