2010-03-18 14:26:21 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the symfony package.
|
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Symfony\Tests\Components\Templating;
|
|
|
|
|
|
|
|
require_once __DIR__.'/../../bootstrap.php';
|
|
|
|
|
|
|
|
require_once __DIR__.'/../../../../lib/SymfonyTests/Components/Templating/SimpleHelper.php';
|
|
|
|
|
|
|
|
use Symfony\Components\Templating\Engine;
|
|
|
|
use Symfony\Components\Templating\Loader\Loader;
|
|
|
|
use Symfony\Components\Templating\Loader\CompilableLoaderInterface;
|
|
|
|
use Symfony\Components\Templating\Renderer\Renderer;
|
|
|
|
use Symfony\Components\Templating\Renderer\PhpRenderer;
|
|
|
|
use Symfony\Components\Templating\Storage\Storage;
|
|
|
|
use Symfony\Components\Templating\Storage\StringStorage;
|
|
|
|
use Symfony\Components\Templating\Helper\SlotsHelper;
|
|
|
|
|
|
|
|
class EngineTest extends \PHPUnit_Framework_TestCase
|
|
|
|
{
|
|
|
|
static protected $loader, $renderer;
|
|
|
|
|
|
|
|
static public function setUpBeforeClass()
|
|
|
|
{
|
|
|
|
self::$loader = new ProjectTemplateLoader();
|
|
|
|
self::$renderer = new ProjectTemplateRenderer();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testConstructor()
|
|
|
|
{
|
|
|
|
$engine = new ProjectTemplateEngine(self::$loader);
|
2010-03-19 14:04:37 +00:00
|
|
|
$this->assertEquals(self::$loader, $engine->getLoader(), '__construct() takes a loader instance as its second first argument');
|
|
|
|
$this->assertEquals(array('php'), array_keys($engine->getRenderers()), '__construct() automatically registers a PHP renderer if none is given');
|
2010-03-18 14:26:21 +00:00
|
|
|
|
|
|
|
$engine = new ProjectTemplateEngine(self::$loader, array('foo' => self::$renderer));
|
2010-03-19 14:04:37 +00:00
|
|
|
$this->assertEquals(array('foo', 'php'), array_keys($engine->getRenderers()), '__construct() takes an array of renderers as its third argument');
|
2010-03-18 14:26:21 +00:00
|
|
|
$this->assertTrue(self::$renderer->getEngine() === $engine, '__construct() registers itself on all renderers');
|
|
|
|
|
|
|
|
$engine = new ProjectTemplateEngine(self::$loader, array('php' => self::$renderer));
|
|
|
|
$this->assertTrue($engine->getRenderers() === array('php' => self::$renderer), '__construct() can overridde the default PHP renderer');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testMagicGet()
|
|
|
|
{
|
|
|
|
$engine = new ProjectTemplateEngine(self::$loader);
|
|
|
|
$engine->set($helper = new \SimpleHelper('bar'), 'foo');
|
2010-03-19 14:04:37 +00:00
|
|
|
$this->assertEquals($helper, $engine->foo, '->__get() returns the value of a helper');
|
2010-03-18 14:26:21 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
$engine->bar;
|
|
|
|
$this->fail('->__get() throws an InvalidArgumentException if the helper is not defined');
|
|
|
|
}
|
|
|
|
catch (\InvalidArgumentException $e)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetSetHas()
|
|
|
|
{
|
|
|
|
$engine = new ProjectTemplateEngine(self::$loader);
|
|
|
|
$foo = new \SimpleHelper('foo');
|
|
|
|
$engine->set($foo);
|
2010-03-19 14:04:37 +00:00
|
|
|
$this->assertEquals($foo, $engine->get('foo'), '->set() sets a helper');
|
2010-03-18 14:26:21 +00:00
|
|
|
|
|
|
|
$engine->set($foo, 'bar');
|
2010-03-19 14:04:37 +00:00
|
|
|
$this->assertEquals($foo, $engine->get('bar'), '->set() takes an alias as a second argument');
|
2010-03-18 14:26:21 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
$engine->get('foobar');
|
|
|
|
$this->fail('->get() throws an InvalidArgumentException if the helper is not defined');
|
|
|
|
}
|
|
|
|
catch (\InvalidArgumentException $e)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertTrue($engine->has('foo'), '->has() returns true if the helper exists');
|
|
|
|
$this->assertTrue(!$engine->has('foobar'), '->has() returns false if the helper does not exist');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testExtendRender()
|
|
|
|
{
|
|
|
|
$engine = new ProjectTemplateEngine(self::$loader, array(), array(new SlotsHelper()));
|
|
|
|
try
|
|
|
|
{
|
|
|
|
$engine->render('name');
|
|
|
|
$this->fail('->render() throws an InvalidArgumentException if the template does not exist');
|
|
|
|
}
|
|
|
|
catch (\InvalidArgumentException $e)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
self::$loader->setTemplate('name.foo', 'foo');
|
|
|
|
$engine->render('foo:name');
|
|
|
|
$this->fail('->render() throws an InvalidArgumentException if no renderer is registered for the given renderer');
|
|
|
|
}
|
|
|
|
catch (\InvalidArgumentException $e)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
$engine = new ProjectTemplateEngine(self::$loader, array(), array(new SlotsHelper()));
|
|
|
|
$engine->set(new \SimpleHelper('bar'));
|
|
|
|
self::$loader->setTemplate('foo.php', '<?php $view->extend("layout"); echo $view->foo.$foo ?>');
|
|
|
|
self::$loader->setTemplate('layout.php', '-<?php echo $view->slots->get("_content") ?>-');
|
2010-03-19 14:04:37 +00:00
|
|
|
$this->assertEquals('-barfoo-', $engine->render('foo', array('foo' => 'foo')), '->render() uses the decorator to decorate the template');
|
2010-03-18 14:26:21 +00:00
|
|
|
|
|
|
|
$engine = new ProjectTemplateEngine(self::$loader, array(), array(new SlotsHelper()));
|
|
|
|
$engine->set(new \SimpleHelper('bar'));
|
|
|
|
self::$loader->setTemplate('bar.php', 'bar');
|
|
|
|
self::$loader->setTemplate('foo.php', '<?php $view->extend("layout"); echo $foo ?>');
|
|
|
|
self::$loader->setTemplate('layout.php', '<?php echo $view->render("bar") ?>-<?php echo $view->slots->get("_content") ?>-');
|
2010-03-19 14:04:37 +00:00
|
|
|
$this->assertEquals('bar-foo-', $engine->render('foo', array('foo' => 'foo', 'bar' => 'bar')), '->render() supports render() calls in templates');
|
2010-03-18 14:26:21 +00:00
|
|
|
|
|
|
|
// compilable templates
|
|
|
|
$engine = new ProjectTemplateEngine(new CompilableTemplateLoader(), array('foo' => new FooTemplateRenderer()));
|
2010-03-19 14:04:37 +00:00
|
|
|
$this->assertEquals('foo', $engine->render('index'), '->load() takes into account the renderer embedded in the Storage instance if not null');
|
2010-03-18 14:26:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testEscape()
|
|
|
|
{
|
|
|
|
$engine = new ProjectTemplateEngine(self::$loader);
|
2010-03-19 14:04:37 +00:00
|
|
|
$this->assertEquals('<br />', $engine->escape('<br />'), '->escape() escapes strings');
|
|
|
|
$foo = new \stdClass();
|
|
|
|
$this->assertEquals($foo, $engine->escape($foo), '->escape() does nothing on non strings');
|
2010-03-18 14:26:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetSetCharset()
|
|
|
|
{
|
|
|
|
$engine = new ProjectTemplateEngine(self::$loader);
|
2010-03-19 14:04:37 +00:00
|
|
|
$this->assertEquals('UTF-8', $engine->getCharset(), '->getCharset() returns UTF-8 by default');
|
2010-03-18 14:26:21 +00:00
|
|
|
$engine->setCharset('ISO-8859-1');
|
2010-03-19 14:04:37 +00:00
|
|
|
$this->assertEquals('ISO-8859-1', $engine->getCharset(), '->setCharset() changes the default charset to use');
|
2010-03-18 14:26:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ProjectTemplateEngine extends Engine
|
|
|
|
{
|
|
|
|
public function getLoader()
|
|
|
|
{
|
|
|
|
return $this->loader;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getRenderers()
|
|
|
|
{
|
|
|
|
return $this->renderers;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ProjectTemplateRenderer extends PhpRenderer
|
|
|
|
{
|
|
|
|
public function getEngine()
|
|
|
|
{
|
|
|
|
return $this->engine;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ProjectTemplateLoader extends Loader
|
|
|
|
{
|
|
|
|
public $templates = array();
|
|
|
|
|
|
|
|
public function setTemplate($name, $template)
|
|
|
|
{
|
|
|
|
$this->templates[$name] = $template;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function load($template, array $options = array())
|
|
|
|
{
|
|
|
|
if (isset($this->templates[$template.'.'.$options['renderer']]))
|
|
|
|
{
|
|
|
|
return new StringStorage($this->templates[$template.'.'.$options['renderer']]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class CompilableTemplateLoader extends Loader implements CompilableLoaderInterface
|
|
|
|
{
|
|
|
|
public function load($template, array $options = array())
|
|
|
|
{
|
|
|
|
return new StringStorage($template, 'foo');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function compile($template)
|
|
|
|
{
|
|
|
|
return 'COMPILED';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class FooTemplateRenderer extends Renderer
|
|
|
|
{
|
|
|
|
public function evaluate(Storage $template, array $parameters = array())
|
|
|
|
{
|
|
|
|
return 'foo';
|
|
|
|
}
|
|
|
|
}
|