EngineTest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. require_once __DIR__.'/../../../bootstrap.php';
  11. require_once __DIR__.'/../../../../lib/SymfonyTests/Components/Templating/SimpleHelper.php';
  12. use Symfony\Components\Templating\Engine;
  13. use Symfony\Components\Templating\Loader\Loader;
  14. use Symfony\Components\Templating\Loader\CompilableLoaderInterface;
  15. use Symfony\Components\Templating\Helper\HelperSet;
  16. use Symfony\Components\Templating\Renderer\Renderer;
  17. use Symfony\Components\Templating\Renderer\PhpRenderer;
  18. use Symfony\Components\Templating\Storage\Storage;
  19. use Symfony\Components\Templating\Storage\StringStorage;
  20. $t = new LimeTest(33);
  21. class ProjectTemplateEngine extends Engine
  22. {
  23. public function getLoader()
  24. {
  25. return $this->loader;
  26. }
  27. public function getRenderers()
  28. {
  29. return $this->renderers;
  30. }
  31. }
  32. class ProjectTemplateRenderer extends PhpRenderer
  33. {
  34. public function getEngine()
  35. {
  36. return $this->engine;
  37. }
  38. }
  39. class ProjectTemplateLoader extends Loader
  40. {
  41. public $templates = array();
  42. public function setTemplate($name, $template)
  43. {
  44. $this->templates[$name] = $template;
  45. }
  46. public function load($template, array $options = array())
  47. {
  48. if (isset($this->templates[$template.'.'.$options['renderer']]))
  49. {
  50. return new StringStorage($this->templates[$template.'.'.$options['renderer']]);
  51. }
  52. return false;
  53. }
  54. }
  55. $loader = new ProjectTemplateLoader();
  56. $renderer = new ProjectTemplateRenderer();
  57. // __construct()
  58. $t->diag('__construct()');
  59. $engine = new ProjectTemplateEngine($loader);
  60. $t->is($engine->getLoader(), $loader, '__construct() takes a loader instance as its second first argument');
  61. $t->is(array_keys($engine->getRenderers()), array('php'), '__construct() automatically registers a PHP renderer if none is given');
  62. $t->ok($engine->getHelperSet() instanceof HelperSet, '__construct() automatically creates a helper set if none is given');
  63. $engine = new ProjectTemplateEngine($loader, array('foo' => $renderer));
  64. $t->is(array_keys($engine->getRenderers()), array('foo', 'php'), '__construct() takes an array of renderers as its third argument');
  65. $t->ok($renderer->getEngine() === $engine, '__construct() registers itself on all renderers');
  66. $engine = new ProjectTemplateEngine($loader, array('php' => $renderer));
  67. $t->ok($engine->getRenderers() === array('php' => $renderer), '__construct() can overridde the default PHP renderer');
  68. $engine = new ProjectTemplateEngine($loader, array(), $helperSet = new HelperSet());
  69. $t->ok($engine->getHelperSet() === $helperSet, '__construct() takes a helper set as its third argument');
  70. // ->getHelperSet() ->setHelperSet()
  71. $t->diag('->getHelperSet() ->setHelperSet()');
  72. $engine = new ProjectTemplateEngine($loader);
  73. $engine->setHelperSet(new HelperSet(array('foo' => $helper = new SimpleHelper('bar'))));
  74. $t->is((string) $engine->getHelperSet()->get('foo'), 'bar', '->setHelperSet() sets a helper set');
  75. // __get()
  76. $t->diag('__get()');
  77. $t->is($engine->foo, $helper, '->__get() returns the value of a helper');
  78. try
  79. {
  80. $engine->bar;
  81. $t->fail('->__get() throws an InvalidArgumentException if the helper is not defined');
  82. }
  83. catch (InvalidArgumentException $e)
  84. {
  85. $t->pass('->__get() throws an InvalidArgumentException if the helper is not defined');
  86. }
  87. // ->get() ->set() ->has()
  88. $t->diag('->get() ->set() ->has()');
  89. $engine = new ProjectTemplateEngine($loader);
  90. $engine->set('foo', 'bar');
  91. $t->is($engine->get('foo'), 'bar', '->set() sets a slot value');
  92. $t->is($engine->get('bar', 'bar'), 'bar', '->get() takes a default value to return if the slot does not exist');
  93. $t->ok($engine->has('foo'), '->has() returns true if the slot exists');
  94. $t->ok(!$engine->has('bar'), '->has() returns false if the slot does not exist');
  95. // ->output()
  96. $t->diag('->output()');
  97. ob_start();
  98. $ret = $engine->output('foo');
  99. $output = ob_get_clean();
  100. $t->is($output, 'bar', '->output() outputs the content of a slot');
  101. $t->is($ret, true, '->output() returns true if the slot exists');
  102. ob_start();
  103. $ret = $engine->output('bar', 'bar');
  104. $output = ob_get_clean();
  105. $t->is($output, 'bar', '->output() takes a default value to return if the slot does not exist');
  106. $t->is($ret, true, '->output() returns true if the slot does not exist but a default value is provided');
  107. ob_start();
  108. $ret = $engine->output('bar');
  109. $output = ob_get_clean();
  110. $t->is($output, '', '->output() outputs nothing if the slot does not exist');
  111. $t->is($ret, false, '->output() returns false if the slot does not exist');
  112. // ->start() ->stop()
  113. $t->diag('->start() ->stop()');
  114. $engine->start('bar');
  115. echo 'foo';
  116. $engine->stop();
  117. $t->is($engine->get('bar'), 'foo', '->start() starts a slot');
  118. $t->ok($engine->has('bar'), '->starts() starts a slot');
  119. $engine->start('bar');
  120. try
  121. {
  122. $engine->start('bar');
  123. $engine->stop();
  124. $t->fail('->start() throws an InvalidArgumentException if a slot with the same name is already started');
  125. }
  126. catch (InvalidArgumentException $e)
  127. {
  128. $engine->stop();
  129. $t->pass('->start() throws an InvalidArgumentException if a slot with the same name is already started');
  130. }
  131. try
  132. {
  133. $engine->stop();
  134. $t->fail('->stop() throws an LogicException if no slot is started');
  135. }
  136. catch (LogicException $e)
  137. {
  138. $t->pass('->stop() throws an LogicException if no slot is started');
  139. }
  140. // ->extend() ->render()
  141. $t->diag('->extend() ->render()');
  142. $engine = new ProjectTemplateEngine($loader);
  143. try
  144. {
  145. $engine->render('name');
  146. $t->fail('->render() throws an InvalidArgumentException if the template does not exist');
  147. }
  148. catch (InvalidArgumentException $e)
  149. {
  150. $t->pass('->render() throws an InvalidArgumentException if the template does not exist');
  151. }
  152. try
  153. {
  154. $loader->setTemplate('name.foo', 'foo');
  155. $engine->render('foo:name');
  156. $t->fail('->render() throws an InvalidArgumentException if no renderer is registered for the given renderer');
  157. }
  158. catch (InvalidArgumentException $e)
  159. {
  160. $t->pass('->render() throws an InvalidArgumentException if no renderer is registered for the given renderer');
  161. }
  162. $engine->getHelperSet()->set(new SimpleHelper('bar'));
  163. $loader->setTemplate('foo.php', '<?php $this->extend("layout"); echo $this->foo.$foo ?>');
  164. $loader->setTemplate('layout.php', '-<?php echo $this->get("content") ?>-');
  165. $t->is($engine->render('foo', array('foo' => 'foo')), '-barfoo-', '->render() uses the decorator to decorate the template');
  166. $loader->setTemplate('bar.php', 'bar');
  167. $loader->setTemplate('foo.php', '<?php $this->extend("layout"); echo $foo ?>');
  168. $loader->setTemplate('layout.php', '<?php echo $this->render("bar") ?>-<?php echo $this->get("content") ?>-');
  169. $t->is($engine->render('foo', array('foo' => 'foo', 'bar' => 'bar')), 'bar-foo-', '->render() supports render() calls in templates');
  170. class CompilableTemplateLoader extends Loader implements CompilableLoaderInterface
  171. {
  172. public function load($template, array $options = array())
  173. {
  174. return new StringStorage($template, 'foo');
  175. }
  176. public function compile($template)
  177. {
  178. return 'COMPILED';
  179. }
  180. }
  181. class FooTemplateRenderer extends Renderer
  182. {
  183. public function evaluate(Storage $template, array $parameters = array())
  184. {
  185. return 'foo';
  186. }
  187. }
  188. $t->diag('compilable templates');
  189. $engine = new ProjectTemplateEngine(new CompilableTemplateLoader(), array('foo' => new FooTemplateRenderer()));
  190. $t->is($engine->render('index'), 'foo', '->load() takes into account the renderer embedded in the Storage instance if not null');
  191. // ->escape()
  192. $t->diag('->escape()');
  193. $engine = new ProjectTemplateEngine($loader);
  194. $t->is($engine->escape('<br />'), '&lt;br /&gt;', '->escape() escapes strings');
  195. $t->is($engine->escape($foo = new stdClass()), $foo, '->escape() does nothing on non strings');
  196. // ->getCharset() ->setCharset()
  197. $t->diag('->getCharset() ->setCharset()');
  198. $engine = new ProjectTemplateEngine($loader);
  199. $t->is($engine->getCharset(), 'UTF-8', '->getCharset() returns UTF-8 by default');
  200. $engine->setCharset('ISO-8859-1');
  201. $t->is($engine->getCharset(), 'ISO-8859-1', '->setCharset() changes the default charset to use');