2012-12-16 16:36:50 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Symfony\Bridge\Twig\Tests\Extension;
|
|
|
|
|
|
|
|
use Symfony\Bridge\Twig\Extension\HttpKernelExtension;
|
|
|
|
use Symfony\Bridge\Twig\Tests\TestCase;
|
2013-01-22 08:06:13 +00:00
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
2013-04-18 09:33:03 +01:00
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
2013-01-31 09:37:27 +00:00
|
|
|
use Symfony\Component\HttpKernel\Fragment\FragmentHandler;
|
2013-04-18 09:33:03 +01:00
|
|
|
use Symfony\Component\HttpKernel\RequestContext;
|
2012-12-16 16:36:50 +00:00
|
|
|
|
|
|
|
class HttpKernelExtensionTest extends TestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @expectedException \Twig_Error_Runtime
|
|
|
|
*/
|
2013-01-31 09:37:27 +00:00
|
|
|
public function testFragmentWithError()
|
2012-12-16 16:36:50 +00:00
|
|
|
{
|
2013-04-18 09:33:03 +01:00
|
|
|
$renderer = $this->getFragmentHandler($this->throwException(new \Exception('foo')));
|
2012-12-16 16:36:50 +00:00
|
|
|
|
2013-04-18 09:33:03 +01:00
|
|
|
$this->renderTemplate($renderer);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRenderFragment()
|
|
|
|
{
|
|
|
|
$renderer = $this->getFragmentHandler($this->returnValue(new Response('html')));
|
|
|
|
|
|
|
|
$response = $this->renderTemplate($renderer);
|
2012-12-16 16:36:50 +00:00
|
|
|
|
2013-04-18 09:33:03 +01:00
|
|
|
$this->assertEquals('html', $response);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testUnknownFragmentRenderer()
|
|
|
|
{
|
|
|
|
$context = $this->getMockBuilder('Symfony\\Component\\HttpKernel\\RequestContext')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock()
|
|
|
|
;
|
2013-09-07 19:20:07 +01:00
|
|
|
$renderer = new FragmentHandler(array(), false, $context);
|
2013-04-18 09:33:03 +01:00
|
|
|
|
|
|
|
$this->setExpectedException('InvalidArgumentException', 'The "inline" renderer does not exist.');
|
|
|
|
$renderer->render('/foo');
|
2012-12-16 16:36:50 +00:00
|
|
|
}
|
|
|
|
|
2013-01-31 09:37:27 +00:00
|
|
|
protected function getFragmentHandler($return)
|
2012-12-16 16:36:50 +00:00
|
|
|
{
|
2013-01-31 09:37:27 +00:00
|
|
|
$strategy = $this->getMock('Symfony\\Component\\HttpKernel\\Fragment\\FragmentRendererInterface');
|
|
|
|
$strategy->expects($this->once())->method('getName')->will($this->returnValue('inline'));
|
2012-12-19 07:54:53 +00:00
|
|
|
$strategy->expects($this->once())->method('render')->will($return);
|
2012-12-16 16:36:50 +00:00
|
|
|
|
2013-04-18 09:33:03 +01:00
|
|
|
$context = $this->getMockBuilder('Symfony\\Component\\HttpKernel\\RequestContext')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock()
|
|
|
|
;
|
|
|
|
|
|
|
|
$context->expects($this->any())->method('getCurrentRequest')->will($this->returnValue(Request::create('/')));
|
|
|
|
|
2013-09-07 19:20:07 +01:00
|
|
|
$renderer = new FragmentHandler(array($strategy), false, $context);
|
2013-01-22 08:06:13 +00:00
|
|
|
|
|
|
|
return $renderer;
|
2012-12-16 16:36:50 +00:00
|
|
|
}
|
|
|
|
|
2013-01-31 09:37:27 +00:00
|
|
|
protected function renderTemplate(FragmentHandler $renderer, $template = '{{ render("foo") }}')
|
2012-12-16 16:36:50 +00:00
|
|
|
{
|
|
|
|
$loader = new \Twig_Loader_Array(array('index' => $template));
|
|
|
|
$twig = new \Twig_Environment($loader, array('debug' => true, 'cache' => false));
|
2012-12-19 07:54:53 +00:00
|
|
|
$twig->addExtension(new HttpKernelExtension($renderer));
|
2012-12-16 16:36:50 +00:00
|
|
|
|
|
|
|
return $twig->render('index');
|
|
|
|
}
|
|
|
|
}
|