PhpEngineTest.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.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\Component\Templating\Tests;
  11. use Symfony\Component\Templating\PhpEngine;
  12. use Symfony\Component\Templating\Loader\Loader;
  13. use Symfony\Component\Templating\Storage\StringStorage;
  14. use Symfony\Component\Templating\Helper\SlotsHelper;
  15. use Symfony\Component\Templating\TemplateNameParser;
  16. use Symfony\Component\Templating\TemplateReferenceInterface;
  17. use Symfony\Component\Templating\TemplateReference;
  18. class PhpEngineTest extends \PHPUnit_Framework_TestCase
  19. {
  20. protected $loader;
  21. protected function setUp()
  22. {
  23. $this->loader = new ProjectTemplateLoader();
  24. }
  25. protected function tearDown()
  26. {
  27. $this->loader = null;
  28. }
  29. public function testConstructor()
  30. {
  31. $engine = new ProjectTemplateEngine(new TemplateNameParser(), $this->loader);
  32. $this->assertEquals($this->loader, $engine->getLoader(), '__construct() takes a loader instance as its second first argument');
  33. }
  34. public function testOffsetGet()
  35. {
  36. $engine = new ProjectTemplateEngine(new TemplateNameParser(), $this->loader);
  37. $engine->set($helper = new \Symfony\Component\Templating\Tests\Fixtures\SimpleHelper('bar'), 'foo');
  38. $this->assertEquals($helper, $engine['foo'], '->offsetGet() returns the value of a helper');
  39. try {
  40. $engine['bar'];
  41. $this->fail('->offsetGet() throws an InvalidArgumentException if the helper is not defined');
  42. } catch (\Exception $e) {
  43. $this->assertInstanceOf('\InvalidArgumentException', $e, '->offsetGet() throws an InvalidArgumentException if the helper is not defined');
  44. $this->assertEquals('The helper "bar" is not defined.', $e->getMessage(), '->offsetGet() throws an InvalidArgumentException if the helper is not defined');
  45. }
  46. }
  47. public function testGetSetHas()
  48. {
  49. $engine = new ProjectTemplateEngine(new TemplateNameParser(), $this->loader);
  50. $foo = new \Symfony\Component\Templating\Tests\Fixtures\SimpleHelper('foo');
  51. $engine->set($foo);
  52. $this->assertEquals($foo, $engine->get('foo'), '->set() sets a helper');
  53. $engine[$foo] = 'bar';
  54. $this->assertEquals($foo, $engine->get('bar'), '->set() takes an alias as a second argument');
  55. $this->assertTrue(isset($engine['bar']));
  56. try {
  57. $engine->get('foobar');
  58. $this->fail('->get() throws an InvalidArgumentException if the helper is not defined');
  59. } catch (\Exception $e) {
  60. $this->assertInstanceOf('\InvalidArgumentException', $e, '->get() throws an InvalidArgumentException if the helper is not defined');
  61. $this->assertEquals('The helper "foobar" is not defined.', $e->getMessage(), '->get() throws an InvalidArgumentException if the helper is not defined');
  62. }
  63. $this->assertTrue(isset($engine['bar']));
  64. $this->assertTrue($engine->has('foo'), '->has() returns true if the helper exists');
  65. $this->assertFalse($engine->has('foobar'), '->has() returns false if the helper does not exist');
  66. }
  67. public function testUnsetHelper()
  68. {
  69. $engine = new ProjectTemplateEngine(new TemplateNameParser(), $this->loader);
  70. $foo = new \Symfony\Component\Templating\Tests\Fixtures\SimpleHelper('foo');
  71. $engine->set($foo);
  72. $this->setExpectedException('\LogicException');
  73. unset($engine['foo']);
  74. }
  75. public function testExtendRender()
  76. {
  77. $engine = new ProjectTemplateEngine(new TemplateNameParser(), $this->loader, array(), array(new SlotsHelper()));
  78. try {
  79. $engine->render('name');
  80. $this->fail('->render() throws an InvalidArgumentException if the template does not exist');
  81. } catch (\Exception $e) {
  82. $this->assertInstanceOf('\InvalidArgumentException', $e, '->render() throws an InvalidArgumentException if the template does not exist');
  83. $this->assertEquals('The template "name" does not exist.', $e->getMessage(), '->render() throws an InvalidArgumentException if the template does not exist');
  84. }
  85. $engine = new ProjectTemplateEngine(new TemplateNameParser(), $this->loader, array(new SlotsHelper()));
  86. $engine->set(new \Symfony\Component\Templating\Tests\Fixtures\SimpleHelper('bar'));
  87. $this->loader->setTemplate('foo.php', '<?php $this->extend("layout.php"); echo $this[\'foo\'].$foo ?>');
  88. $this->loader->setTemplate('layout.php', '-<?php echo $this[\'slots\']->get("_content") ?>-');
  89. $this->assertEquals('-barfoo-', $engine->render('foo.php', array('foo' => 'foo')), '->render() uses the decorator to decorate the template');
  90. $engine = new ProjectTemplateEngine(new TemplateNameParser(), $this->loader, array(new SlotsHelper()));
  91. $engine->set(new \Symfony\Component\Templating\Tests\Fixtures\SimpleHelper('bar'));
  92. $this->loader->setTemplate('bar.php', 'bar');
  93. $this->loader->setTemplate('foo.php', '<?php $this->extend("layout.php"); echo $foo ?>');
  94. $this->loader->setTemplate('layout.php', '<?php echo $this->render("bar.php") ?>-<?php echo $this[\'slots\']->get("_content") ?>-');
  95. $this->assertEquals('bar-foo-', $engine->render('foo.php', array('foo' => 'foo', 'bar' => 'bar')), '->render() supports render() calls in templates');
  96. }
  97. public function testRenderParameter()
  98. {
  99. $engine = new ProjectTemplateEngine(new TemplateNameParser(), $this->loader);
  100. $this->loader->setTemplate('foo.php', '<?php echo $template . $parameters ?>');
  101. $this->assertEquals('foobar', $engine->render('foo.php', array('template' => 'foo', 'parameters' => 'bar')), '->render() extract variables');
  102. }
  103. /**
  104. * @expectedException \InvalidArgumentException
  105. * @dataProvider forbiddenParameterNames
  106. */
  107. public function testRenderForbiddenParameter($name)
  108. {
  109. $engine = new ProjectTemplateEngine(new TemplateNameParser(), $this->loader);
  110. $this->loader->setTemplate('foo.php', 'bar');
  111. $engine->render('foo.php', array($name => 'foo'));
  112. }
  113. public function forbiddenParameterNames()
  114. {
  115. return array(
  116. array('this'),
  117. array('view'),
  118. );
  119. }
  120. public function testEscape()
  121. {
  122. $engine = new ProjectTemplateEngine(new TemplateNameParser(), $this->loader);
  123. $this->assertEquals('&lt;br /&gt;', $engine->escape('<br />'), '->escape() escapes strings');
  124. $foo = new \stdClass();
  125. $this->assertEquals($foo, $engine->escape($foo), '->escape() does nothing on non strings');
  126. }
  127. public function testGetSetCharset()
  128. {
  129. $helper = new SlotsHelper();
  130. $engine = new ProjectTemplateEngine(new TemplateNameParser(), $this->loader, array($helper));
  131. $this->assertEquals('UTF-8', $engine->getCharset(), 'EngineInterface::getCharset() returns UTF-8 by default');
  132. $this->assertEquals('UTF-8', $helper->getCharset(), 'HelperInterface::getCharset() returns UTF-8 by default');
  133. $engine->setCharset('ISO-8859-1');
  134. $this->assertEquals('ISO-8859-1', $engine->getCharset(), 'EngineInterface::setCharset() changes the default charset to use');
  135. $this->assertEquals('ISO-8859-1', $helper->getCharset(), 'EngineInterface::setCharset() changes the default charset of helper');
  136. }
  137. public function testGlobalVariables()
  138. {
  139. $engine = new ProjectTemplateEngine(new TemplateNameParser(), $this->loader);
  140. $engine->addGlobal('global_variable', 'lorem ipsum');
  141. $this->assertEquals(array(
  142. 'global_variable' => 'lorem ipsum',
  143. ), $engine->getGlobals());
  144. }
  145. public function testGlobalsGetPassedToTemplate()
  146. {
  147. $engine = new ProjectTemplateEngine(new TemplateNameParser(), $this->loader);
  148. $engine->addGlobal('global', 'global variable');
  149. $this->loader->setTemplate('global.php', '<?php echo $global; ?>');
  150. $this->assertEquals($engine->render('global.php'), 'global variable');
  151. $this->assertEquals($engine->render('global.php', array('global' => 'overwritten')), 'overwritten');
  152. }
  153. public function testGetLoader()
  154. {
  155. $engine = new ProjectTemplateEngine(new TemplateNameParser(), $this->loader);
  156. $this->assertSame($this->loader, $engine->getLoader());
  157. }
  158. }
  159. class ProjectTemplateEngine extends PhpEngine
  160. {
  161. public function getLoader()
  162. {
  163. return $this->loader;
  164. }
  165. }
  166. class ProjectTemplateLoader extends Loader
  167. {
  168. public $templates = array();
  169. public function setTemplate($name, $content)
  170. {
  171. $template = new TemplateReference($name, 'php');
  172. $this->templates[$template->getLogicalName()] = $content;
  173. }
  174. public function load(TemplateReferenceInterface $template)
  175. {
  176. if (isset($this->templates[$template->getLogicalName()])) {
  177. return new StringStorage($this->templates[$template->getLogicalName()]);
  178. }
  179. return false;
  180. }
  181. public function isFresh(TemplateReferenceInterface $template, $time)
  182. {
  183. return false;
  184. }
  185. }