2010-03-18 14:26:21 +00:00
< ? php
/*
2010-04-25 16:06:54 +01:00
* This file is part of the Symfony package .
2010-03-18 14:26:21 +00:00
*
2011-03-06 11:40:06 +00:00
* ( c ) Fabien Potencier < fabien @ symfony . com >
2010-04-07 02:07:59 +01:00
*
2010-03-18 14:26:21 +00:00
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
2010-08-20 22:09:55 +01:00
namespace Symfony\Tests\Component\Templating ;
2010-03-18 14:26:21 +00:00
2010-06-28 08:31:54 +01:00
require_once __DIR__ . '/Fixtures/SimpleHelper.php' ;
2010-03-18 14:26:21 +00:00
2011-01-13 13:58:17 +00:00
use Symfony\Component\Templating\PhpEngine ;
2010-08-20 22:09:55 +01:00
use Symfony\Component\Templating\Loader\Loader ;
use Symfony\Component\Templating\Storage\Storage ;
use Symfony\Component\Templating\Storage\StringStorage ;
use Symfony\Component\Templating\Helper\SlotsHelper ;
2011-01-26 13:53:12 +00:00
use Symfony\Component\Templating\TemplateNameParser ;
2011-02-10 17:20:44 +00:00
use Symfony\Component\Templating\TemplateReferenceInterface ;
use Symfony\Component\Templating\TemplateReference ;
2010-03-18 14:26:21 +00:00
2011-01-13 13:58:17 +00:00
class PhpEngineTest extends \PHPUnit_Framework_TestCase
2010-03-18 14:26:21 +00:00
{
2011-01-13 13:58:17 +00:00
protected $loader ;
2010-05-06 12:25:53 +01:00
2011-01-13 13:58:17 +00:00
public function setUp ()
2010-03-18 14:26:21 +00:00
{
2011-01-26 13:53:12 +00:00
$this -> loader = new ProjectTemplateLoader ();
2010-03-18 14:26:21 +00:00
}
2010-05-06 12:25:53 +01:00
public function testConstructor ()
{
2011-01-26 13:53:12 +00:00
$engine = new ProjectTemplateEngine ( new TemplateNameParser (), $this -> loader );
2011-01-13 13:58:17 +00:00
$this -> assertEquals ( $this -> loader , $engine -> getLoader (), '__construct() takes a loader instance as its second first argument' );
2010-03-18 14:26:21 +00:00
}
2010-05-06 12:25:53 +01:00
2010-08-19 14:52:47 +01:00
public function testOffsetGet ()
2010-03-18 14:26:21 +00:00
{
2011-01-26 13:53:12 +00:00
$engine = new ProjectTemplateEngine ( new TemplateNameParser (), $this -> loader );
2010-05-06 12:25:53 +01:00
$engine -> set ( $helper = new \SimpleHelper ( 'bar' ), 'foo' );
2010-08-19 14:52:47 +01:00
$this -> assertEquals ( $helper , $engine [ 'foo' ], '->offsetGet() returns the value of a helper' );
2010-05-06 12:25:53 +01:00
2010-05-07 15:09:11 +01:00
try {
2010-08-19 14:52:47 +01:00
$engine [ 'bar' ];
$this -> fail ( '->offsetGet() throws an InvalidArgumentException if the helper is not defined' );
2010-05-07 15:09:11 +01:00
} catch ( \Exception $e ) {
2010-08-19 14:52:47 +01:00
$this -> assertInstanceOf ( '\InvalidArgumentException' , $e , '->offsetGet() throws an InvalidArgumentException if the helper is not defined' );
$this -> assertEquals ( 'The helper "bar" is not defined.' , $e -> getMessage (), '->offsetGet() throws an InvalidArgumentException if the helper is not defined' );
2010-05-06 12:25:53 +01:00
}
2010-03-18 14:26:21 +00:00
}
2010-05-06 12:25:53 +01:00
public function testGetSetHas ()
2010-03-18 14:26:21 +00:00
{
2011-01-26 13:53:12 +00:00
$engine = new ProjectTemplateEngine ( new TemplateNameParser (), $this -> loader );
2010-05-06 12:25:53 +01:00
$foo = new \SimpleHelper ( 'foo' );
$engine -> set ( $foo );
$this -> assertEquals ( $foo , $engine -> get ( 'foo' ), '->set() sets a helper' );
$engine -> set ( $foo , 'bar' );
$this -> assertEquals ( $foo , $engine -> get ( 'bar' ), '->set() takes an alias as a second argument' );
2010-05-07 15:09:11 +01:00
try {
2010-05-06 12:25:53 +01:00
$engine -> get ( 'foobar' );
$this -> fail ( '->get() throws an InvalidArgumentException if the helper is not defined' );
2010-05-07 15:09:11 +01:00
} catch ( \Exception $e ) {
2010-05-06 12:25:53 +01:00
$this -> assertInstanceOf ( '\InvalidArgumentException' , $e , '->get() throws an InvalidArgumentException if the helper is not defined' );
$this -> assertEquals ( 'The helper "foobar" is not defined.' , $e -> getMessage (), '->get() throws an InvalidArgumentException if the helper is not defined' );
}
$this -> assertTrue ( $engine -> has ( 'foo' ), '->has() returns true if the helper exists' );
$this -> assertFalse ( $engine -> has ( 'foobar' ), '->has() returns false if the helper does not exist' );
2010-03-18 14:26:21 +00:00
}
2010-05-06 12:25:53 +01:00
public function testExtendRender ()
2010-03-18 14:26:21 +00:00
{
2011-01-26 13:53:12 +00:00
$engine = new ProjectTemplateEngine ( new TemplateNameParser (), $this -> loader , array (), array ( new SlotsHelper ()));
2010-05-07 15:09:11 +01:00
try {
2010-05-06 12:25:53 +01:00
$engine -> render ( 'name' );
$this -> fail ( '->render() throws an InvalidArgumentException if the template does not exist' );
2010-05-07 15:09:11 +01:00
} catch ( \Exception $e ) {
2010-05-06 12:25:53 +01:00
$this -> assertInstanceOf ( '\InvalidArgumentException' , $e , '->render() throws an InvalidArgumentException if the template does not exist' );
2011-01-13 13:58:17 +00:00
$this -> assertEquals ( 'The template "name" does not exist.' , $e -> getMessage (), '->render() throws an InvalidArgumentException if the template does not exist' );
2010-05-06 12:25:53 +01:00
}
2011-01-26 13:53:12 +00:00
$engine = new ProjectTemplateEngine ( new TemplateNameParser (), $this -> loader , array ( new SlotsHelper ()));
2010-05-06 12:25:53 +01:00
$engine -> set ( new \SimpleHelper ( 'bar' ));
2011-01-13 13:58:17 +00:00
$this -> loader -> setTemplate ( 'foo.php' , '<?php $view->extend("layout.php"); echo $view[\'foo\'].$foo ?>' );
$this -> loader -> setTemplate ( 'layout.php' , '-<?php echo $view[\'slots\']->get("_content") ?>-' );
$this -> assertEquals ( '-barfoo-' , $engine -> render ( 'foo.php' , array ( 'foo' => 'foo' )), '->render() uses the decorator to decorate the template' );
2010-05-06 12:25:53 +01:00
2011-01-26 13:53:12 +00:00
$engine = new ProjectTemplateEngine ( new TemplateNameParser (), $this -> loader , array ( new SlotsHelper ()));
2010-05-06 12:25:53 +01:00
$engine -> set ( new \SimpleHelper ( 'bar' ));
2011-01-13 13:58:17 +00:00
$this -> loader -> setTemplate ( 'bar.php' , 'bar' );
$this -> loader -> setTemplate ( 'foo.php' , '<?php $view->extend("layout.php"); echo $foo ?>' );
$this -> loader -> setTemplate ( 'layout.php' , '<?php echo $view->render("bar.php") ?>-<?php echo $view[\'slots\']->get("_content") ?>-' );
$this -> assertEquals ( 'bar-foo-' , $engine -> render ( 'foo.php' , array ( 'foo' => 'foo' , 'bar' => 'bar' )), '->render() supports render() calls in templates' );
2010-03-18 14:26:21 +00:00
}
2010-05-06 12:25:53 +01:00
public function testEscape ()
2010-03-18 14:26:21 +00:00
{
2011-01-26 13:53:12 +00:00
$engine = new ProjectTemplateEngine ( new TemplateNameParser (), $this -> loader );
2010-05-06 12:25:53 +01: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
}
2010-05-06 12:25:53 +01:00
public function testGetSetCharset ()
2010-03-18 14:26:21 +00:00
{
2011-01-26 13:53:12 +00:00
$engine = new ProjectTemplateEngine ( new TemplateNameParser (), $this -> loader );
2010-05-06 12:25:53 +01:00
$this -> assertEquals ( 'UTF-8' , $engine -> getCharset (), '->getCharset() returns UTF-8 by default' );
$engine -> setCharset ( 'ISO-8859-1' );
$this -> assertEquals ( 'ISO-8859-1' , $engine -> getCharset (), '->setCharset() changes the default charset to use' );
2010-03-18 14:26:21 +00:00
}
2010-12-20 12:05:28 +00:00
public function testGlobalVariables ()
{
2011-01-26 13:53:12 +00:00
$engine = new ProjectTemplateEngine ( new TemplateNameParser (), $this -> loader );
2010-12-20 12:05:28 +00:00
$engine -> addGlobal ( 'global_variable' , 'lorem ipsum' );
$this -> assertEquals ( array (
'global_variable' => 'lorem ipsum' ,
), $engine -> getGlobals ());
}
public function testGlobalsGetPassedToTemplate ()
{
2011-01-26 13:53:12 +00:00
$engine = new ProjectTemplateEngine ( new TemplateNameParser (), $this -> loader );
2010-12-20 12:05:28 +00:00
$engine -> addGlobal ( 'global' , 'global variable' );
2011-01-13 13:58:17 +00:00
$this -> loader -> setTemplate ( 'global.php' , '<?php echo $global; ?>' );
2010-12-20 12:05:28 +00:00
2011-01-13 13:58:17 +00:00
$this -> assertEquals ( $engine -> render ( 'global.php' ), 'global variable' );
2010-12-20 12:05:28 +00:00
2011-01-13 13:58:17 +00:00
$this -> assertEquals ( $engine -> render ( 'global.php' , array ( 'global' => 'overwritten' )), 'overwritten' );
2010-12-20 12:05:28 +00:00
}
2010-03-18 14:26:21 +00:00
}
2011-01-13 13:58:17 +00:00
class ProjectTemplateEngine extends PhpEngine
2010-03-18 14:26:21 +00:00
{
2010-05-06 12:25:53 +01:00
public function getLoader ()
{
return $this -> loader ;
}
2010-03-18 14:26:21 +00:00
}
class ProjectTemplateLoader extends Loader
{
2010-05-06 12:25:53 +01:00
public $templates = array ();
2010-03-18 14:26:21 +00:00
2011-02-10 17:20:44 +00:00
public function setTemplate ( $name , $content )
2010-03-18 14:26:21 +00:00
{
2011-02-10 17:20:44 +00:00
$template = new TemplateReference ( $name , 'php' );
$this -> templates [ $template -> getSignature ()] = $content ;
2010-03-18 14:26:21 +00:00
}
2011-02-10 17:20:44 +00:00
public function load ( TemplateReferenceInterface $template )
2010-05-06 12:25:53 +01:00
{
2011-02-10 17:20:44 +00:00
if ( isset ( $this -> templates [ $template -> getSignature ()])) {
return new StringStorage ( $this -> templates [ $template -> getSignature ()]);
2010-05-06 12:25:53 +01:00
}
return false ;
}
2011-02-10 17:20:44 +00:00
public function isFresh ( TemplateReferenceInterface $template , $time )
2010-05-06 12:25:53 +01:00
{
2010-05-20 19:57:25 +01:00
return false ;
2010-05-06 12:25:53 +01:00
}
2010-03-18 14:26:21 +00:00
}