2011-01-17 22:28:59 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Symfony\Bundle\FrameworkBundle\Tests;
|
|
|
|
|
|
|
|
use Symfony\Component\HttpKernel\HttpKernelInterface;
|
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
|
|
|
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
|
|
|
|
|
|
|
$controller = function() use($expected)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
|
|
|
$controller = function() use ($expected)
|
|
|
|
{
|
|
|
|
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),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|