PhpEngineTest.php 7.1 KB

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