PhpEngineTest.php 8.0 KB

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