PhpEngineTest.php 7.0 KB

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