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