EngineTest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. namespace Symfony\Tests\Components\Templating;
  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\Renderer\Renderer;
  16. use Symfony\Components\Templating\Renderer\PhpRenderer;
  17. use Symfony\Components\Templating\Storage\Storage;
  18. use Symfony\Components\Templating\Storage\StringStorage;
  19. use Symfony\Components\Templating\Helper\SlotsHelper;
  20. class EngineTest extends \PHPUnit_Framework_TestCase
  21. {
  22. static protected $loader, $renderer;
  23. static public function setUpBeforeClass()
  24. {
  25. self::$loader = new ProjectTemplateLoader();
  26. self::$renderer = new ProjectTemplateRenderer();
  27. }
  28. public function testConstructor()
  29. {
  30. $engine = new ProjectTemplateEngine(self::$loader);
  31. $this->assertEquals(self::$loader, $engine->getLoader(), '__construct() takes a loader instance as its second first argument');
  32. $this->assertEquals(array('php'), array_keys($engine->getRenderers()), '__construct() automatically registers a PHP renderer if none is given');
  33. $engine = new ProjectTemplateEngine(self::$loader, array('foo' => self::$renderer));
  34. $this->assertEquals(array('foo', 'php'), array_keys($engine->getRenderers()), '__construct() takes an array of renderers as its third argument');
  35. $this->assertTrue(self::$renderer->getEngine() === $engine, '__construct() registers itself on all renderers');
  36. $engine = new ProjectTemplateEngine(self::$loader, array('php' => self::$renderer));
  37. $this->assertTrue($engine->getRenderers() === array('php' => self::$renderer), '__construct() can overridde the default PHP renderer');
  38. }
  39. public function testMagicGet()
  40. {
  41. $engine = new ProjectTemplateEngine(self::$loader);
  42. $engine->set($helper = new \SimpleHelper('bar'), 'foo');
  43. $this->assertEquals($helper, $engine->foo, '->__get() returns the value of a helper');
  44. try
  45. {
  46. $engine->bar;
  47. $this->fail('->__get() throws an InvalidArgumentException if the helper is not defined');
  48. }
  49. catch (\Exception $e)
  50. {
  51. $this->assertType('\InvalidArgumentException', $e, '->__get() throws an InvalidArgumentException if the helper is not defined');
  52. $this->assertEquals('The helper "bar" is not defined.', $e->getMessage(), '->__get() throws an InvalidArgumentException if the helper is not defined');
  53. }
  54. }
  55. public function testGetSetHas()
  56. {
  57. $engine = new ProjectTemplateEngine(self::$loader);
  58. $foo = new \SimpleHelper('foo');
  59. $engine->set($foo);
  60. $this->assertEquals($foo, $engine->get('foo'), '->set() sets a helper');
  61. $engine->set($foo, 'bar');
  62. $this->assertEquals($foo, $engine->get('bar'), '->set() takes an alias as a second argument');
  63. try
  64. {
  65. $engine->get('foobar');
  66. $this->fail('->get() throws an InvalidArgumentException if the helper is not defined');
  67. }
  68. catch (\Exception $e)
  69. {
  70. $this->assertType('\InvalidArgumentException', $e, '->get() throws an InvalidArgumentException if the helper is not defined');
  71. $this->assertEquals('The helper "foobar" is not defined.', $e->getMessage(), '->get() throws an InvalidArgumentException if the helper is not defined');
  72. }
  73. $this->assertTrue($engine->has('foo'), '->has() returns true if the helper exists');
  74. $this->assertFalse($engine->has('foobar'), '->has() returns false if the helper does not exist');
  75. }
  76. public function testExtendRender()
  77. {
  78. $engine = new ProjectTemplateEngine(self::$loader, array(), array(new SlotsHelper()));
  79. try
  80. {
  81. $engine->render('name');
  82. $this->fail('->render() throws an InvalidArgumentException if the template does not exist');
  83. }
  84. catch (\Exception $e)
  85. {
  86. $this->assertType('\InvalidArgumentException', $e, '->render() throws an InvalidArgumentException if the template does not exist');
  87. $this->assertEquals('The template "name" does not exist (renderer: php).', $e->getMessage(), '->render() throws an InvalidArgumentException if the template does not exist');
  88. }
  89. try
  90. {
  91. self::$loader->setTemplate('name.foo', 'foo');
  92. $engine->render('foo:name');
  93. $this->fail('->render() throws an InvalidArgumentException if no renderer is registered for the given renderer');
  94. }
  95. catch (\Exception $e)
  96. {
  97. $this->assertType('\InvalidArgumentException', $e, '->render() throws an InvalidArgumentException if no renderer is registered for the given renderer');
  98. $this->assertEquals('The template "foo" does not exist (renderer: name).', $e->getMessage(), '->render() throws an InvalidArgumentException if no renderer is registered for the given renderer');
  99. }
  100. $engine = new ProjectTemplateEngine(self::$loader, array(), array(new SlotsHelper()));
  101. $engine->set(new \SimpleHelper('bar'));
  102. self::$loader->setTemplate('foo.php', '<?php $view->extend("layout"); echo $view->foo.$foo ?>');
  103. self::$loader->setTemplate('layout.php', '-<?php echo $view->slots->get("_content") ?>-');
  104. $this->assertEquals('-barfoo-', $engine->render('foo', array('foo' => 'foo')), '->render() uses the decorator to decorate the template');
  105. $engine = new ProjectTemplateEngine(self::$loader, array(), array(new SlotsHelper()));
  106. $engine->set(new \SimpleHelper('bar'));
  107. self::$loader->setTemplate('bar.php', 'bar');
  108. self::$loader->setTemplate('foo.php', '<?php $view->extend("layout"); echo $foo ?>');
  109. self::$loader->setTemplate('layout.php', '<?php echo $view->render("bar") ?>-<?php echo $view->slots->get("_content") ?>-');
  110. $this->assertEquals('bar-foo-', $engine->render('foo', array('foo' => 'foo', 'bar' => 'bar')), '->render() supports render() calls in templates');
  111. // compilable templates
  112. $engine = new ProjectTemplateEngine(new CompilableTemplateLoader(), array('foo' => new FooTemplateRenderer()));
  113. $this->assertEquals('foo', $engine->render('index'), '->load() takes into account the renderer embedded in the Storage instance if not null');
  114. }
  115. public function testEscape()
  116. {
  117. $engine = new ProjectTemplateEngine(self::$loader);
  118. $this->assertEquals('&lt;br /&gt;', $engine->escape('<br />'), '->escape() escapes strings');
  119. $foo = new \stdClass();
  120. $this->assertEquals($foo, $engine->escape($foo), '->escape() does nothing on non strings');
  121. }
  122. public function testGetSetCharset()
  123. {
  124. $engine = new ProjectTemplateEngine(self::$loader);
  125. $this->assertEquals('UTF-8', $engine->getCharset(), '->getCharset() returns UTF-8 by default');
  126. $engine->setCharset('ISO-8859-1');
  127. $this->assertEquals('ISO-8859-1', $engine->getCharset(), '->setCharset() changes the default charset to use');
  128. }
  129. }
  130. class ProjectTemplateEngine extends Engine
  131. {
  132. public function getLoader()
  133. {
  134. return $this->loader;
  135. }
  136. public function getRenderers()
  137. {
  138. return $this->renderers;
  139. }
  140. }
  141. class ProjectTemplateRenderer extends PhpRenderer
  142. {
  143. public function getEngine()
  144. {
  145. return $this->engine;
  146. }
  147. }
  148. class ProjectTemplateLoader extends Loader
  149. {
  150. public $templates = array();
  151. public function setTemplate($name, $template)
  152. {
  153. $this->templates[$name] = $template;
  154. }
  155. public function load($template, array $options = array())
  156. {
  157. if (isset($this->templates[$template.'.'.$options['renderer']]))
  158. {
  159. return new StringStorage($this->templates[$template.'.'.$options['renderer']]);
  160. }
  161. return false;
  162. }
  163. }
  164. class CompilableTemplateLoader extends Loader implements CompilableLoaderInterface
  165. {
  166. public function load($template, array $options = array())
  167. {
  168. return new StringStorage($template, 'foo');
  169. }
  170. public function compile($template)
  171. {
  172. return 'COMPILED';
  173. }
  174. }
  175. class FooTemplateRenderer extends Renderer
  176. {
  177. public function evaluate(Storage $template, array $parameters = array())
  178. {
  179. return 'foo';
  180. }
  181. }