diff --git a/src/Symfony/Component/HttpKernel/Tests/EventListener/ExceptionListenerTest.php b/src/Symfony/Component/HttpKernel/Tests/EventListener/ExceptionListenerTest.php index b4e8819161348..11f458d6bfe29 100644 --- a/src/Symfony/Component/HttpKernel/Tests/EventListener/ExceptionListenerTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/EventListener/ExceptionListenerTest.php @@ -11,8 +11,12 @@ namespace Symfony\Component\HttpKernel\Tests\EventListener; +use Symfony\Component\EventDispatcher\EventDispatcher; +use Symfony\Component\HttpKernel\Exception\HttpException; +use Symfony\Component\HttpKernel\HttpKernel; use Symfony\Component\HttpKernel\HttpKernelInterface; use Symfony\Component\HttpKernel\EventListener\ExceptionListener; +use Symfony\Component\HttpKernel\KernelEvents; use Symfony\Component\HttpKernel\Log\DebugLoggerInterface; use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; use Symfony\Component\HttpFoundation\Request; @@ -40,6 +44,32 @@ public function testConstruct() $this->assertSame('foo', $_controller->getValue($l)); } + public function testHandleHttpExceptionThrownInListener() + { + // store the current error_log, and disable it temporarily + $errorLog = ini_set('error_log', file_exists('/dev/null') ? '/dev/null' : 'nul'); + + $listener = new ExceptionListener('foo'); + + $dispatcher = new EventDispatcher(); + $dispatcher->addSubscriber($listener); + $dispatcher->addListener(KernelEvents::REQUEST, function ($event) { + throw new HttpException(1337); + }); + + $kernel = new HttpKernel($dispatcher, $this->getTestResolver()); + + try { + $kernel->handle(new Request(), HttpKernelInterface::MASTER_REQUEST); + $this->fail('HttpKernel::handle is expected to throw HttpException'); + } catch (HttpException $exception) { + $this->assertEquals(1337, $exception->getStatusCode()); + } + + // restore the old error_log + ini_set('error_log', $errorLog); + } + /** * @dataProvider provider */ @@ -119,6 +149,21 @@ public function testSubRequestFormat() $response = $event->getResponse(); $this->assertEquals('xml', $response->getContent()); } + + protected function getTestResolver() + { + $controller = function () { return new Response('foo'); }; + + $resolver = $this->getMock('Symfony\\Component\\HttpKernel\\Controller\\ControllerResolverInterface'); + $resolver->expects($this->any()) + ->method('getController') + ->will($this->returnValue($controller)); + $resolver->expects($this->any()) + ->method('getArguments') + ->will($this->returnValue(array())); + + return $resolver; + } } class TestLogger extends Logger implements DebugLoggerInterface diff --git a/src/Symfony/Component/HttpKernel/Tests/HttpKernelTest.php b/src/Symfony/Component/HttpKernel/Tests/HttpKernelTest.php index 700b111228bee..c1c516e525c40 100644 --- a/src/Symfony/Component/HttpKernel/Tests/HttpKernelTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/HttpKernelTest.php @@ -11,6 +11,7 @@ namespace Symfony\Component\HttpKernel\Tests; +use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; use Symfony\Component\HttpKernel\HttpKernel; use Symfony\Component\HttpKernel\HttpKernelInterface; use Symfony\Component\HttpKernel\KernelEvents; @@ -71,6 +72,25 @@ public function testHandleExceptionWithARedirectionResponse() $this->assertEquals('/login', $response->headers->get('Location')); } + public function testHandleWhenAnAccessDeniedHttpExceptionIsThrownByAListener() + { + $dispatcher = new EventDispatcher(); + + $dispatcher->addListener(KernelEvents::REQUEST, function ($event) { + throw new AccessDeniedHttpException(); + }); + + $dispatcher->addListener(KernelEvents::EXCEPTION, function (GetResponseForExceptionEvent $event) { + $event->setResponse(new Response('foo', $event->getException()->getStatusCode())); + }); + + $kernel = new HttpKernel($dispatcher, $this->getResolver(function () { return new Response(); })); + $response = $kernel->handle(new Request()); + + $this->assertEquals('foo', $response->getContent()); + $this->assertEquals('403', $response->getStatusCode()); + } + public function testHandleHttpException() { $dispatcher = new EventDispatcher();