MustacheTest.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. * @access public
  115. * @return void
  116. */
  117. public function testRenderWithData() {
  118. $m = new Mustache('{{first_name}} {{last_name}}');
  119. $this->assertEquals('Charlie Chaplin', $m->render(null, array('first_name' => 'Charlie', 'last_name' => 'Chaplin')));
  120. $this->assertEquals('Zappa, Frank', $m->render('{{last_name}}, {{first_name}}', array('first_name' => 'Frank', 'last_name' => 'Zappa')));
  121. }
  122. public function testRenderWithPartials() {
  123. $m = new Mustache('{{>stache}}', null, array('stache' => '{{first_name}} {{last_name}}'));
  124. $this->assertEquals('Charlie Chaplin', $m->render(null, array('first_name' => 'Charlie', 'last_name' => 'Chaplin')));
  125. $this->assertEquals('Zappa, Frank', $m->render('{{last_name}}, {{first_name}}', array('first_name' => 'Frank', 'last_name' => 'Zappa')));
  126. }
  127. /**
  128. * Mustache should allow newlines (and other whitespace) in comments and all other tags.
  129. *
  130. * @access public
  131. * @return void
  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. * @access public
  141. * @return void
  142. */
  143. public function testMultipleInvocations() {
  144. $m = new Mustache('x');
  145. $first = $m->render();
  146. $second = $m->render();
  147. $this->assertEquals('x', $first);
  148. $this->assertEquals($first, $second);
  149. }
  150. /**
  151. * Mustache should return the same thing when invoked multiple times.
  152. *
  153. * @access public
  154. * @return void
  155. */
  156. public function testMultipleInvocationsWithTags() {
  157. $m = new Mustache('{{one}} {{two}}', array('one' => 'foo', 'two' => 'bar'));
  158. $first = $m->render();
  159. $second = $m->render();
  160. $this->assertEquals('foo bar', $first);
  161. $this->assertEquals($first, $second);
  162. }
  163. /**
  164. * Mustache should not use templates passed to the render() method for subsequent invocations.
  165. *
  166. * @access public
  167. * @return void
  168. */
  169. public function testResetTemplateForMultipleInvocations() {
  170. $m = new Mustache('Sirve.');
  171. $this->assertEquals('No sirve.', $m->render('No sirve.'));
  172. $this->assertEquals('Sirve.', $m->render());
  173. $m2 = new Mustache();
  174. $this->assertEquals('No sirve.', $m2->render('No sirve.'));
  175. $this->assertEquals('', $m2->render());
  176. }
  177. /**
  178. * testClone function.
  179. *
  180. * @group examples
  181. * @dataProvider getExamples
  182. * @access public
  183. * @param string $class
  184. * @param string $template
  185. * @param string $output
  186. * @return void
  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. * @access public
  208. * @param string $class
  209. * @param string $template
  210. * @param string $output
  211. * @return void
  212. */
  213. public function testExamples($class, $template, $output) {
  214. if (isset($this->knownIssues[$class])) {
  215. return $this->markTestSkipped($this->knownIssues[$class]);
  216. }
  217. $m = new $class;
  218. $this->assertEquals($output, $m->render($template));
  219. }
  220. /**
  221. * Data provider for testExamples method.
  222. *
  223. * Assumes that an `examples` directory exists inside parent directory.
  224. * This examples directory should contain any number of subdirectories, each of which contains
  225. * three files: one Mustache class (.php), one Mustache template (.mustache), and one output file
  226. * (.txt).
  227. *
  228. * This whole mess will be refined later to be more intuitive and less prescriptive, but it'll
  229. * do for now. Especially since it means we can have unit tests :)
  230. *
  231. * @access public
  232. * @return array
  233. */
  234. public function getExamples() {
  235. $basedir = dirname(__FILE__) . '/../examples/';
  236. $ret = array();
  237. $files = new RecursiveDirectoryIterator($basedir);
  238. while ($files->valid()) {
  239. if ($files->hasChildren() && $children = $files->getChildren()) {
  240. $example = $files->getSubPathname();
  241. $class = null;
  242. $template = null;
  243. $output = null;
  244. foreach ($children as $file) {
  245. if (!$file->isFile()) continue;
  246. $filename = $file->getPathname();
  247. $info = pathinfo($filename);
  248. if (isset($info['extension'])) {
  249. switch($info['extension']) {
  250. case 'php':
  251. $class = $info['filename'];
  252. include_once($filename);
  253. break;
  254. case 'mustache':
  255. $template = file_get_contents($filename);
  256. break;
  257. case 'txt':
  258. $output = file_get_contents($filename);
  259. break;
  260. }
  261. }
  262. }
  263. if (!empty($class)) {
  264. $ret[$example] = array($class, $template, $output);
  265. }
  266. }
  267. $files->next();
  268. }
  269. return $ret;
  270. }
  271. public function testCrazyDelimiters() {
  272. $m = new Mustache(null, array('result' => 'success'));
  273. $this->assertEquals('success', $m->render('{{=[[ ]]=}}[[ result ]]'));
  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. }
  280. public function testResetDelimiters() {
  281. $m = new Mustache(null, array('result' => 'success'));
  282. $this->assertEquals('success', $m->render('{{=[[ ]]=}}[[ result ]]'));
  283. $this->assertEquals('success', $m->render('{{=<< >>=}}<< result >>'));
  284. $this->assertEquals('success', $m->render('{{=<% %>=}}<% result %>'));
  285. }
  286. }