|
14 | 14 | use Symfony\Component\HttpKernel\HttpKernel; |
15 | 15 | use Symfony\Component\HttpKernel\HttpKernelInterface; |
16 | 16 | use Symfony\Component\HttpKernel\KernelEvents; |
| 17 | +use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException; |
| 18 | +use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; |
17 | 19 | use Symfony\Component\HttpFoundation\Request; |
18 | 20 | use Symfony\Component\HttpFoundation\Response; |
| 21 | +use Symfony\Component\HttpFoundation\RedirectResponse; |
19 | 22 | use Symfony\Component\EventDispatcher\EventDispatcher; |
20 | 23 |
|
21 | 24 | class HttpKernelTest extends \PHPUnit_Framework_TestCase |
@@ -59,8 +62,38 @@ public function testHandleWhenControllerThrowsAnExceptionAndRawIsFalse() |
59 | 62 | }); |
60 | 63 |
|
61 | 64 | $kernel = new HttpKernel($dispatcher, $this->getResolver(function () { throw new \RuntimeException('foo'); })); |
| 65 | + $response = $kernel->handle(new Request()); |
62 | 66 |
|
63 | | - $this->assertEquals('foo', $kernel->handle(new Request())->getContent()); |
| 67 | + $this->assertEquals('500', $response->getStatusCode()); |
| 68 | + $this->assertEquals('foo', $response->getContent()); |
| 69 | + } |
| 70 | + |
| 71 | + public function testHandleExceptionWithARedirectionResponse() |
| 72 | + { |
| 73 | + $dispatcher = new EventDispatcher(); |
| 74 | + $dispatcher->addListener(KernelEvents::EXCEPTION, function ($event) { |
| 75 | + $event->setResponse(new RedirectResponse('/login', 301)); |
| 76 | + }); |
| 77 | + |
| 78 | + $kernel = new HttpKernel($dispatcher, $this->getResolver(function () { throw new AccessDeniedHttpException(); })); |
| 79 | + $response = $kernel->handle(new Request()); |
| 80 | + |
| 81 | + $this->assertEquals('301', $response->getStatusCode()); |
| 82 | + $this->assertEquals('/login', $response->headers->get('Location')); |
| 83 | + } |
| 84 | + |
| 85 | + public function testHandleHttpException() |
| 86 | + { |
| 87 | + $dispatcher = new EventDispatcher(); |
| 88 | + $dispatcher->addListener(KernelEvents::EXCEPTION, function ($event) { |
| 89 | + $event->setResponse(new Response($event->getException()->getMessage())); |
| 90 | + }); |
| 91 | + |
| 92 | + $kernel = new HttpKernel($dispatcher, $this->getResolver(function () { throw new MethodNotAllowedHttpException(array('POST')); })); |
| 93 | + $response = $kernel->handle(new Request()); |
| 94 | + |
| 95 | + $this->assertEquals('405', $response->getStatusCode()); |
| 96 | + $this->assertEquals('POST', $response->headers->get('Allow')); |
64 | 97 | } |
65 | 98 |
|
66 | 99 | public function testHandleWhenAListenerReturnsAResponse() |
|
0 commit comments