2013-01-10 15:08:44 +00:00
< ? php
/*
* This file is part of the Symfony package .
*
* ( c ) Fabien Potencier < fabien @ symfony . com >
*
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
2013-01-31 09:37:27 +00:00
namespace Symfony\Component\HttpKernel\Tests\Fragment\FragmentRenderer ;
2013-01-10 15:08:44 +00:00
use Symfony\Component\HttpKernel\Controller\ControllerReference ;
2013-01-31 09:37:27 +00:00
use Symfony\Component\HttpKernel\Fragment\EsiFragmentRenderer ;
2013-01-10 15:08:44 +00:00
use Symfony\Component\HttpKernel\HttpCache\Esi ;
use Symfony\Component\HttpFoundation\Request ;
2013-01-31 09:37:27 +00:00
class EsiFragmentRendererTest extends \PHPUnit_Framework_TestCase
2013-01-10 15:08:44 +00:00
{
protected function setUp ()
{
if ( ! class_exists ( 'Symfony\Component\HttpFoundation\Request' )) {
$this -> markTestSkipped ( 'The "HttpFoundation" component is not available' );
}
}
2013-01-31 09:37:27 +00:00
public function testRenderFallbackToInlineStrategyIfNoRequest ()
2013-01-10 15:08:44 +00:00
{
2013-01-31 09:37:27 +00:00
$strategy = new EsiFragmentRenderer ( new Esi (), $this -> getInlineStrategy ( true ));
2013-01-22 08:06:13 +00:00
$strategy -> render ( '/' , Request :: create ( '/' ));
2013-01-10 15:08:44 +00:00
}
2013-01-31 09:37:27 +00:00
public function testRenderFallbackToInlineStrategyIfEsiNotSupported ()
2013-01-10 15:08:44 +00:00
{
2013-01-31 09:37:27 +00:00
$strategy = new EsiFragmentRenderer ( new Esi (), $this -> getInlineStrategy ( true ));
2013-01-10 15:08:44 +00:00
$strategy -> render ( '/' , Request :: create ( '/' ));
}
public function testRender ()
{
2013-01-31 09:37:27 +00:00
$strategy = new EsiFragmentRenderer ( new Esi (), $this -> getInlineStrategy ());
2013-01-10 15:08:44 +00:00
$request = Request :: create ( '/' );
2013-07-13 11:18:20 +01:00
$request -> setLocale ( 'fr' );
2013-01-10 15:08:44 +00:00
$request -> headers -> set ( 'Surrogate-Capability' , 'ESI/1.0' );
2013-01-20 09:04:47 +00:00
$this -> assertEquals ( '<esi:include src="/" />' , $strategy -> render ( '/' , $request ) -> getContent ());
$this -> assertEquals ( " <esi:comment text= \" This is a comment \" /> \n <esi:include src= \" / \" /> " , $strategy -> render ( '/' , $request , array ( 'comment' => 'This is a comment' )) -> getContent ());
$this -> assertEquals ( '<esi:include src="/" alt="foo" />' , $strategy -> render ( '/' , $request , array ( 'alt' => 'foo' )) -> getContent ());
2013-08-29 06:19:56 +01:00
$this -> assertEquals ( '<esi:include src="/_fragment?_path=_format%3Dhtml%26_locale%3Dfr%26_controller%3Dmain_controller" alt="/_fragment?_path=_format%3Dhtml%26_locale%3Dfr%26_controller%3Dalt_controller" />' , $strategy -> render ( new ControllerReference ( 'main_controller' , array (), array ()), $request , array ( 'alt' => new ControllerReference ( 'alt_controller' , array (), array ()))) -> getContent ());
2013-01-10 15:08:44 +00:00
}
2013-01-31 09:37:27 +00:00
private function getInlineStrategy ( $called = false )
2013-01-10 15:08:44 +00:00
{
2013-01-31 09:37:27 +00:00
$inline = $this -> getMockBuilder ( 'Symfony\Component\HttpKernel\Fragment\InlineFragmentRenderer' ) -> disableOriginalConstructor () -> getMock ();
2013-01-10 15:08:44 +00:00
if ( $called ) {
2013-01-31 09:37:27 +00:00
$inline -> expects ( $this -> once ()) -> method ( 'render' );
2013-01-10 15:08:44 +00:00
}
2013-01-31 09:37:27 +00:00
return $inline ;
2013-01-10 15:08:44 +00:00
}
}