MustacheTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. require_once '../Mustache.php';
  3. require_once 'PHPUnit/Framework.php';
  4. /**
  5. * A PHPUnit test case for Mustache.php.
  6. *
  7. * This is a very basic, very rudimentary unit test case. It's probably more important to have tests
  8. * than to have elegant tests, so let's bear with it for a bit.
  9. *
  10. * This class assumes an example directory exists at `../examples` with the following structure:
  11. *
  12. * @code
  13. * examples
  14. * foo
  15. * Foo.php
  16. * foo.mustache
  17. * foo.txt
  18. * bar
  19. * Bar.php
  20. * bar.mustache
  21. * bar.txt
  22. * @endcode
  23. *
  24. * To use this test:
  25. *
  26. * 1. {@link http://www.phpunit.de/manual/current/en/installation.html Install PHPUnit}
  27. * 2. run phpunit from the `test` directory:
  28. * `phpunit MustacheTest`
  29. * 3. Fix bugs. Lather, rinse, repeat.
  30. *
  31. * @extends PHPUnit_Framework_TestCase
  32. */
  33. class MustacheTest extends PHPUnit_Framework_TestCase {
  34. const TEST_CLASS = 'Mustache';
  35. /**
  36. * Test Mustache constructor.
  37. *
  38. * @access public
  39. * @return void
  40. */
  41. public function test__construct() {
  42. $template = '{{#mustaches}}{{#last}}and {{/last}}{{type}}{{^last}}, {{/last}}{{/mustaches}}';
  43. $data = array(
  44. 'mustaches' => array(
  45. array('type' => 'Natural'),
  46. array('type' => 'Hungarian'),
  47. array('type' => 'Dali'),
  48. array('type' => 'English'),
  49. array('type' => 'Imperial'),
  50. array('type' => 'Freestyle', 'last' => 'true'),
  51. )
  52. );
  53. $output = 'Natural, Hungarian, Dali, English, Imperial, and Freestyle';
  54. $m1 = new Mustache();
  55. $this->assertEquals($output, $m1->render($template, $data));
  56. $m2 = new Mustache($template);
  57. $this->assertEquals($output, $m2->render(null, $data));
  58. $m3 = new Mustache($template, $data);
  59. $this->assertEquals($output, $m3->render());
  60. $m4 = new Mustache(null, $data);
  61. $this->assertEquals($output, $m4->render($template));
  62. }
  63. /**
  64. * Test __toString() function.
  65. *
  66. * @access public
  67. * @return void
  68. */
  69. public function test__toString() {
  70. $m = new Mustache('{{first_name}} {{last_name}}', array('first_name' => 'Karl', 'last_name' => 'Marx'));
  71. $this->assertEquals('Karl Marx', $m->__toString());
  72. $this->assertEquals('Karl Marx', (string) $m);
  73. $m2 = $this->getMock(self::TEST_CLASS, array('render'), array());
  74. $m2->expects($this->once())
  75. ->method('render')
  76. ->will($this->returnValue('foo'));
  77. $this->assertEquals('foo', $m2->render());
  78. }
  79. public function test__toStringException() {
  80. $m = $this->getMock(self::TEST_CLASS, array('render'), array());
  81. $m->expects($this->once())
  82. ->method('render')
  83. ->will($this->throwException(new Exception));
  84. try {
  85. $out = (string) $m;
  86. } catch (Exception $e) {
  87. $this->fail('__toString should catch all exceptions');
  88. }
  89. }
  90. /**
  91. * Test render().
  92. *
  93. * @access public
  94. * @return void
  95. */
  96. public function testRender() {
  97. $m = new Mustache();
  98. $this->assertEquals('', $m->render(''));
  99. $this->assertEquals('foo', $m->render('foo'));
  100. $this->assertEquals('', $m->render(null));
  101. $m2 = new Mustache('foo');
  102. $this->assertEquals('foo', $m2->render());
  103. $m3 = new Mustache('');
  104. $this->assertEquals('', $m3->render());
  105. $m3 = new Mustache();
  106. $this->assertEquals('', $m3->render(null));
  107. }
  108. /**
  109. * Test render() with data.
  110. *
  111. * @access public
  112. * @return void
  113. */
  114. public function testRenderWithData() {
  115. $m = new Mustache('{{first_name}} {{last_name}}');
  116. $this->assertEquals('Charlie Chaplin', $m->render(null, array('first_name' => 'Charlie', 'last_name' => 'Chaplin')));
  117. $this->assertEquals('Zappa, Frank', $m->render('{{last_name}}, {{first_name}}', array('first_name' => 'Frank', 'last_name' => 'Zappa')));
  118. }
  119. /**
  120. * Test everything in the `examples` directory.
  121. *
  122. * @dataProvider getExamples
  123. * @access public
  124. * @param mixed $class
  125. * @param mixed $template
  126. * @param mixed $output
  127. * @return void
  128. */
  129. public function testExamples($class, $template, $output) {
  130. $m = new $class;
  131. $this->assertEquals($output, $m->render($template));
  132. }
  133. /**
  134. * Data provider for testExamples method.
  135. *
  136. * Assumes that an `examples` directory exists inside parent directory.
  137. * This examples directory should contain any number of subdirectories, each of which contains
  138. * three files: one Mustache class (.php), one Mustache template (.mustache), and one output file
  139. * (.txt).
  140. *
  141. * This whole mess will be refined later to be more intuitive and less prescriptive, but it'll
  142. * do for now. Especially since it means we can have unit tests :)
  143. *
  144. * @access public
  145. * @return array
  146. */
  147. public function getExamples() {
  148. $basedir = dirname(__FILE__) . '/../examples/';
  149. $ret = array();
  150. $files = new RecursiveDirectoryIterator($basedir);
  151. while ($files->valid()) {
  152. if ($files->hasChildren() && $children = $files->getChildren()) {
  153. $example = $files->getSubPathname();
  154. $class = null;
  155. $template = null;
  156. $output = null;
  157. foreach ($children as $file) {
  158. if (!$file->isFile()) continue;
  159. $filename = $file->getPathInfo();
  160. $info = pathinfo($filename);
  161. switch($info['extension']) {
  162. case 'php':
  163. $class = $info['filename'];
  164. include_once($filename);
  165. break;
  166. case 'mustache':
  167. $template = file_get_contents($filename);
  168. break;
  169. case 'txt':
  170. $output = file_get_contents($filename);
  171. break;
  172. }
  173. }
  174. $ret[$example] = array($class, $template, $output);
  175. }
  176. $files->next();
  177. }
  178. return $ret;
  179. }
  180. }