PhpEngineTest.php 7.8KB

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