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;
|
2016-09-29 23:29:01 +01:00
|
|
|
use Symfony\Bridge\Twig\Extension\HttpKernelRuntime;
|
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;
|
2012-12-16 16:36:50 +00:00
|
|
|
|
2013-09-17 15:25:42 +01:00
|
|
|
class HttpKernelExtensionTest extends \PHPUnit_Framework_TestCase
|
2012-12-16 16:36:50 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @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()
|
|
|
|
{
|
2013-09-07 21:38:41 +01:00
|
|
|
$context = $this->getMockBuilder('Symfony\\Component\\HttpFoundation\\RequestStack')
|
2013-04-18 09:33:03 +01:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock()
|
|
|
|
;
|
2015-09-08 09:31:52 +01:00
|
|
|
$renderer = new FragmentHandler($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
|
|
|
{
|
2016-12-19 09:02:29 +00:00
|
|
|
$strategy = $this->getMockBuilder('Symfony\\Component\\HttpKernel\\Fragment\\FragmentRendererInterface')->getMock();
|
2013-01-31 09:37:27 +00:00
|
|
|
$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-09-07 21:38:41 +01:00
|
|
|
$context = $this->getMockBuilder('Symfony\\Component\\HttpFoundation\\RequestStack')
|
2013-04-18 09:33:03 +01:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock()
|
|
|
|
;
|
|
|
|
|
|
|
|
$context->expects($this->any())->method('getCurrentRequest')->will($this->returnValue(Request::create('/')));
|
|
|
|
|
2015-09-08 09:31:52 +01:00
|
|
|
$renderer = new FragmentHandler($context, array($strategy), false);
|
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));
|
2016-09-29 23:29:01 +01:00
|
|
|
$twig->addExtension(new HttpKernelExtension());
|
|
|
|
|
|
|
|
$loader = $this->getMock('Twig_RuntimeLoaderInterface');
|
|
|
|
$loader->expects($this->any())->method('load')->will($this->returnValueMap(array(
|
|
|
|
array('Symfony\Bridge\Twig\Extension\HttpKernelRuntime', new HttpKernelRuntime($renderer)),
|
|
|
|
)));
|
|
|
|
$twig->addRuntimeLoader($loader);
|
2012-12-16 16:36:50 +00:00
|
|
|
|
|
|
|
return $twig->render('index');
|
|
|
|
}
|
|
|
|
}
|