2011-01-17 22:28:59 +00:00
|
|
|
<?php
|
|
|
|
|
2011-05-31 09:57:06 +01:00
|
|
|
/*
|
2012-03-31 22:00:32 +01:00
|
|
|
* This file is part of the Symfony package.
|
2011-05-31 09:57:06 +01:00
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
|
*
|
2012-03-31 22:00:32 +01:00
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
2011-05-31 09:57:06 +01:00
|
|
|
*/
|
|
|
|
|
2011-01-17 22:28:59 +00:00
|
|
|
namespace Symfony\Bundle\FrameworkBundle\Tests;
|
|
|
|
|
|
|
|
use Symfony\Component\HttpKernel\HttpKernelInterface;
|
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
use Symfony\Bundle\FrameworkBundle\HttpKernel;
|
2011-03-13 18:16:56 +00:00
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcher;
|
2011-01-17 22:28:59 +00:00
|
|
|
|
|
|
|
class HttpKernelTest extends \PHPUnit_Framework_TestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @dataProvider getProviderTypes
|
|
|
|
*/
|
|
|
|
public function testHandle($type)
|
|
|
|
{
|
|
|
|
$request = new Request();
|
|
|
|
$expected = new Response();
|
|
|
|
|
|
|
|
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
|
|
|
|
$container
|
|
|
|
->expects($this->once())
|
|
|
|
->method('enterScope')
|
|
|
|
->with($this->equalTo('request'))
|
|
|
|
;
|
2011-01-23 07:47:00 +00:00
|
|
|
$container
|
|
|
|
->expects($this->once())
|
|
|
|
->method('leaveScope')
|
|
|
|
->with($this->equalTo('request'))
|
|
|
|
;
|
2011-01-17 22:28:59 +00:00
|
|
|
$container
|
|
|
|
->expects($this->once())
|
|
|
|
->method('set')
|
|
|
|
->with($this->equalTo('request'), $this->equalTo($request), $this->equalTo('request'))
|
|
|
|
;
|
|
|
|
|
2011-03-13 18:16:56 +00:00
|
|
|
$dispatcher = new EventDispatcher();
|
2011-01-17 22:28:59 +00:00
|
|
|
$resolver = $this->getMock('Symfony\\Component\\HttpKernel\\Controller\\ControllerResolverInterface');
|
2011-03-13 18:16:56 +00:00
|
|
|
$kernel = new HttpKernel($dispatcher, $container, $resolver);
|
2011-01-17 22:28:59 +00:00
|
|
|
|
2012-07-09 13:50:58 +01:00
|
|
|
$controller = function() use ($expected) {
|
2011-01-17 22:28:59 +00:00
|
|
|
return $expected;
|
|
|
|
};
|
|
|
|
|
|
|
|
$resolver->expects($this->once())
|
|
|
|
->method('getController')
|
|
|
|
->with($request)
|
|
|
|
->will($this->returnValue($controller));
|
|
|
|
$resolver->expects($this->once())
|
|
|
|
->method('getArguments')
|
|
|
|
->with($request, $controller)
|
|
|
|
->will($this->returnValue(array()));
|
|
|
|
|
|
|
|
$actual = $kernel->handle($request, $type);
|
|
|
|
|
|
|
|
$this->assertSame($expected, $actual, '->handle() returns the response');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider getProviderTypes
|
|
|
|
*/
|
|
|
|
public function testHandleRestoresThePreviousRequestOnException($type)
|
|
|
|
{
|
|
|
|
$request = new Request();
|
|
|
|
$expected = new \Exception();
|
|
|
|
|
|
|
|
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
|
|
|
|
$container
|
|
|
|
->expects($this->once())
|
|
|
|
->method('enterScope')
|
|
|
|
->with($this->equalTo('request'))
|
|
|
|
;
|
2011-01-23 07:47:00 +00:00
|
|
|
$container
|
|
|
|
->expects($this->once())
|
|
|
|
->method('leaveScope')
|
|
|
|
->with($this->equalTo('request'))
|
|
|
|
;
|
2011-01-17 22:28:59 +00:00
|
|
|
$container
|
|
|
|
->expects($this->once())
|
|
|
|
->method('set')
|
|
|
|
->with($this->equalTo('request'), $this->equalTo($request), $this->equalTo('request'))
|
|
|
|
;
|
|
|
|
|
2011-03-13 18:16:56 +00:00
|
|
|
$dispatcher = new EventDispatcher();
|
2011-01-17 22:28:59 +00:00
|
|
|
$resolver = $this->getMock('Symfony\\Component\\HttpKernel\\Controller\\ControllerResolverInterface');
|
2011-03-13 18:16:56 +00:00
|
|
|
$kernel = new HttpKernel($dispatcher, $container, $resolver);
|
2011-01-17 22:28:59 +00:00
|
|
|
|
2012-05-20 17:15:10 +01:00
|
|
|
$controller = function() use ($expected) {
|
2011-01-17 22:28:59 +00:00
|
|
|
throw $expected;
|
|
|
|
};
|
|
|
|
|
|
|
|
$resolver->expects($this->once())
|
|
|
|
->method('getController')
|
|
|
|
->with($request)
|
|
|
|
->will($this->returnValue($controller));
|
|
|
|
$resolver->expects($this->once())
|
|
|
|
->method('getArguments')
|
|
|
|
->with($request, $controller)
|
|
|
|
->will($this->returnValue(array()));
|
|
|
|
|
|
|
|
try {
|
|
|
|
$kernel->handle($request, $type);
|
|
|
|
$this->fail('->handle() suppresses the controller exception');
|
|
|
|
} catch (\Exception $actual) {
|
|
|
|
$this->assertSame($expected, $actual, '->handle() throws the controller exception');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getProviderTypes()
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
array(HttpKernelInterface::MASTER_REQUEST),
|
|
|
|
array(HttpKernelInterface::SUB_REQUEST),
|
|
|
|
);
|
|
|
|
}
|
2011-11-07 17:25:31 +00:00
|
|
|
|
|
|
|
public function testExceptionInSubRequestsDoesNotMangleOutputBuffers()
|
|
|
|
{
|
|
|
|
$request = new Request();
|
|
|
|
|
|
|
|
$container = $this->getMock('Symfony\\Component\\DependencyInjection\\ContainerInterface');
|
|
|
|
$container
|
|
|
|
->expects($this->at(0))
|
2012-12-17 14:17:23 +00:00
|
|
|
->method('get')
|
|
|
|
->with($this->equalTo('request'))
|
|
|
|
->will($this->returnValue($request))
|
|
|
|
;
|
|
|
|
$container
|
|
|
|
->expects($this->at(1))
|
2011-11-07 17:25:31 +00:00
|
|
|
->method('getParameter')
|
|
|
|
->with($this->equalTo('kernel.debug'))
|
|
|
|
->will($this->returnValue(false))
|
|
|
|
;
|
|
|
|
$container
|
2012-12-17 14:17:23 +00:00
|
|
|
->expects($this->at(2))
|
2011-11-07 17:25:31 +00:00
|
|
|
->method('has')
|
|
|
|
->with($this->equalTo('esi'))
|
|
|
|
->will($this->returnValue(false))
|
|
|
|
;
|
|
|
|
|
|
|
|
$dispatcher = new EventDispatcher();
|
|
|
|
$resolver = $this->getMock('Symfony\\Component\\HttpKernel\\Controller\\ControllerResolverInterface');
|
|
|
|
$resolver->expects($this->once())
|
|
|
|
->method('getController')
|
|
|
|
->will($this->returnValue(function () {
|
|
|
|
ob_start();
|
|
|
|
echo 'bar';
|
|
|
|
throw new \RuntimeException();
|
|
|
|
}));
|
|
|
|
$resolver->expects($this->once())
|
|
|
|
->method('getArguments')
|
|
|
|
->will($this->returnValue(array()));
|
|
|
|
|
|
|
|
$kernel = new HttpKernel($dispatcher, $container, $resolver);
|
|
|
|
|
|
|
|
// simulate a main request with output buffering
|
|
|
|
ob_start();
|
|
|
|
echo 'Foo';
|
|
|
|
|
|
|
|
// simulate a sub-request with output buffering and an exception
|
|
|
|
$kernel->render('/');
|
|
|
|
|
|
|
|
$this->assertEquals('Foo', ob_get_clean());
|
|
|
|
}
|
2011-06-08 11:16:48 +01:00
|
|
|
}
|