MustacheTest.php 8.3 KB

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