|
11 | 11 |
|
12 | 12 | namespace Symfony\Component\HttpKernel\Tests\EventListener;
|
13 | 13 |
|
| 14 | +use Symfony\Component\EventDispatcher\EventDispatcher; |
| 15 | +use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; |
| 16 | +use Symfony\Component\HttpKernel\Exception\HttpException; |
| 17 | +use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
| 18 | +use Symfony\Component\HttpKernel\HttpKernel; |
14 | 19 | use Symfony\Component\HttpKernel\HttpKernelInterface;
|
15 | 20 | use Symfony\Component\HttpKernel\EventListener\ExceptionListener;
|
| 21 | +use Symfony\Component\HttpKernel\KernelEvents; |
16 | 22 | use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
|
17 | 23 | use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
|
18 | 24 | use Symfony\Component\HttpFoundation\Request;
|
@@ -40,6 +46,32 @@ public function testConstruct()
|
40 | 46 | $this->assertSame('foo', $_controller->getValue($l));
|
41 | 47 | }
|
42 | 48 |
|
| 49 | + public function testHandleHttpExceptionThrownInListener() |
| 50 | + { |
| 51 | + // store the current error_log, and disable it temporarily |
| 52 | + $errorLog = ini_set('error_log', file_exists('/dev/null') ? '/dev/null' : 'nul'); |
| 53 | + |
| 54 | + $listener = new ExceptionListener('foo'); |
| 55 | + |
| 56 | + $dispatcher = new EventDispatcher(); |
| 57 | + $dispatcher->addSubscriber($listener); |
| 58 | + $dispatcher->addListener(KernelEvents::REQUEST, function ($event) { |
| 59 | + throw new HttpException(1337); |
| 60 | + }); |
| 61 | + |
| 62 | + $kernel = new HttpKernel($dispatcher, $this->getTestResolver()); |
| 63 | + |
| 64 | + try { |
| 65 | + $kernel->handle(new Request(), HttpKernelInterface::MASTER_REQUEST); |
| 66 | + $this->fail('HttpKernel::handle is expected to throw HttpException'); |
| 67 | + } catch (HttpException $exception) { |
| 68 | + $this->assertEquals(1337, $exception->getStatusCode()); |
| 69 | + } |
| 70 | + |
| 71 | + // restore the old error_log |
| 72 | + ini_set('error_log', $errorLog); |
| 73 | + } |
| 74 | + |
43 | 75 | /**
|
44 | 76 | * @dataProvider provider
|
45 | 77 | */
|
@@ -119,6 +151,21 @@ public function testSubRequestFormat()
|
119 | 151 | $response = $event->getResponse();
|
120 | 152 | $this->assertEquals('xml', $response->getContent());
|
121 | 153 | }
|
| 154 | + |
| 155 | + protected function getTestResolver() |
| 156 | + { |
| 157 | + $controller = function () { return new Response('foo'); }; |
| 158 | + |
| 159 | + $resolver = $this->getMock('Symfony\\Component\\HttpKernel\\Controller\\ControllerResolverInterface'); |
| 160 | + $resolver->expects($this->any()) |
| 161 | + ->method('getController') |
| 162 | + ->will($this->returnValue($controller)); |
| 163 | + $resolver->expects($this->any()) |
| 164 | + ->method('getArguments') |
| 165 | + ->will($this->returnValue(array())); |
| 166 | + |
| 167 | + return $resolver; |
| 168 | + } |
122 | 169 | }
|
123 | 170 |
|
124 | 171 | class TestLogger extends Logger implements DebugLoggerInterface
|
|
0 commit comments