MustacheLoaderTest.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. require_once '../Mustache.php';
  3. require_once '../MustacheLoader.php';
  4. /**
  5. * @group loader
  6. */
  7. class MustacheLoaderTest extends PHPUnit_Framework_TestCase {
  8. public function testTheActualFilesystemLoader() {
  9. $loader = new MustacheLoader(dirname(__FILE__).'/fixtures');
  10. $this->assertEquals(file_get_contents(dirname(__FILE__).'/fixtures/foo.mustache'), $loader['foo']);
  11. $this->assertEquals(file_get_contents(dirname(__FILE__).'/fixtures/bar.mustache'), $loader['bar']);
  12. }
  13. public function testMustacheUsesFilesystemLoader() {
  14. $template = '{{> foo }} {{> bar }}';
  15. $data = array(
  16. 'truthy' => true,
  17. 'foo' => 'FOO',
  18. 'bar' => 'BAR',
  19. );
  20. $output = 'FOO BAR';
  21. $m = new Mustache();
  22. $partials = new MustacheLoader(dirname(__FILE__).'/fixtures');
  23. $this->assertEquals($output, $m->render($template, $data, $partials));
  24. }
  25. public function testMustacheUsesDifferentLoadersToo() {
  26. $template = '{{> foo }} {{> bar }}';
  27. $data = array(
  28. 'truthy' => true,
  29. 'foo' => 'FOO',
  30. 'bar' => 'BAR',
  31. );
  32. $output = 'FOO BAR';
  33. $m = new Mustache();
  34. $partials = new DifferentMustacheLoader();
  35. $this->assertEquals($output, $m->render($template, $data, $partials));
  36. }
  37. }
  38. class DifferentMustacheLoader implements ArrayAccess {
  39. protected $partials = array(
  40. 'foo' => '{{ foo }}',
  41. 'bar' => '{{# truthy }}{{ bar }}{{/ truthy }}',
  42. );
  43. public function offsetExists($offset) {
  44. return isset($this->partials[$offset]);
  45. }
  46. public function offsetGet($offset) {
  47. return $this->partials[$offset];
  48. }
  49. public function offsetSet($offset, $value) {}
  50. public function offsetUnset($offset) {}
  51. }