|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\HttpKernel\Tests\Controller; |
| 13 | + |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use Symfony\Component\ErrorRenderer\ErrorRenderer; |
| 16 | +use Symfony\Component\ErrorRenderer\ErrorRenderer\HtmlErrorRenderer; |
| 17 | +use Symfony\Component\ErrorRenderer\ErrorRenderer\JsonErrorRenderer; |
| 18 | +use Symfony\Component\ErrorRenderer\Exception\FlattenException; |
| 19 | +use Symfony\Component\HttpFoundation\Request; |
| 20 | +use Symfony\Component\HttpFoundation\Response; |
| 21 | +use Symfony\Component\HttpKernel\Controller\ErrorController; |
| 22 | +use Symfony\Component\HttpKernel\Exception\HttpException; |
| 23 | +use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
| 24 | +use Symfony\Component\HttpKernel\HttpKernelInterface; |
| 25 | + |
| 26 | +class ErrorControllerTest extends TestCase |
| 27 | +{ |
| 28 | + /** |
| 29 | + * @dataProvider getInvokeControllerDataProvider |
| 30 | + */ |
| 31 | + public function testInvokeController(Request $request, FlattenException $exception, int $statusCode, string $content) |
| 32 | + { |
| 33 | + $kernel = $this->getMockBuilder(HttpKernelInterface::class)->getMock(); |
| 34 | + $errorRenderer = new ErrorRenderer([new HtmlErrorRenderer(), new JsonErrorRenderer()]); |
| 35 | + $controller = new ErrorController($kernel, null, $errorRenderer); |
| 36 | + $response = $controller($request, $exception); |
| 37 | + |
| 38 | + $this->assertSame($statusCode, $response->getStatusCode()); |
| 39 | + self::assertStringContainsString($content, strtr($response->getContent(), ["\n" => '', ' ' => ''])); |
| 40 | + } |
| 41 | + |
| 42 | + public function getInvokeControllerDataProvider() |
| 43 | + { |
| 44 | + yield 'default status code and HTML format' => [ |
| 45 | + new Request(), |
| 46 | + FlattenException::createFromThrowable(new \Exception()), |
| 47 | + 500, |
| 48 | + 'The server returned a "500 Internal Server Error".', |
| 49 | + ]; |
| 50 | + |
| 51 | + yield 'custom status code' => [ |
| 52 | + new Request(), |
| 53 | + FlattenException::createFromThrowable(new NotFoundHttpException('Page not found.')), |
| 54 | + 404, |
| 55 | + 'The server returned a "404 Not Found".', |
| 56 | + ]; |
| 57 | + |
| 58 | + $request = new Request(); |
| 59 | + $request->attributes->set('_format', 'json'); |
| 60 | + yield 'custom format via _format attribute' => [ |
| 61 | + $request, |
| 62 | + FlattenException::createFromThrowable(new \Exception('foo')), |
| 63 | + 500, |
| 64 | + '{"title": "Internal Server Error","status": 500,"detail": "foo"}', |
| 65 | + ]; |
| 66 | + |
| 67 | + $request = new Request(); |
| 68 | + $request->headers->set('Accept', 'application/json'); |
| 69 | + yield 'custom format via Accept header' => [ |
| 70 | + $request, |
| 71 | + FlattenException::createFromThrowable(new HttpException(405, 'Invalid request.')), |
| 72 | + 405, |
| 73 | + '{"title": "Method Not Allowed","status": 405,"detail": "Invalid request."}', |
| 74 | + ]; |
| 75 | + |
| 76 | + $request = new Request(); |
| 77 | + $request->headers->set('Content-Type', 'application/json'); |
| 78 | + yield 'custom format via Content-Type header' => [ |
| 79 | + $request, |
| 80 | + FlattenException::createFromThrowable(new HttpException(405, 'Invalid request.')), |
| 81 | + 405, |
| 82 | + '{"title": "Method Not Allowed","status": 405,"detail": "Invalid request."}', |
| 83 | + ]; |
| 84 | + |
| 85 | + $request = new Request(); |
| 86 | + $request->attributes->set('_format', 'unknown'); |
| 87 | + yield 'default HTML format for unknown formats' => [ |
| 88 | + $request, |
| 89 | + FlattenException::createFromThrowable(new HttpException(405, 'Invalid request.')), |
| 90 | + 405, |
| 91 | + 'The server returned a "405 Method Not Allowed".', |
| 92 | + ]; |
| 93 | + } |
| 94 | + |
| 95 | + public function testPreviewController() |
| 96 | + { |
| 97 | + $_controller = 'error_controller'; |
| 98 | + $code = 404; |
| 99 | + |
| 100 | + $kernel = $this->getMockBuilder(HttpKernelInterface::class)->getMock(); |
| 101 | + $kernel |
| 102 | + ->expects($this->once()) |
| 103 | + ->method('handle') |
| 104 | + ->with( |
| 105 | + $this->callback(function (Request $request) use ($_controller, $code) { |
| 106 | + $exception = $request->attributes->get('exception'); |
| 107 | + |
| 108 | + $this->assertSame($_controller, $request->attributes->get('_controller')); |
| 109 | + $this->assertInstanceOf(FlattenException::class, $exception); |
| 110 | + $this->assertSame($code, $exception->getStatusCode()); |
| 111 | + $this->assertFalse($request->attributes->get('showException')); |
| 112 | + |
| 113 | + return true; |
| 114 | + }), |
| 115 | + $this->equalTo(HttpKernelInterface::SUB_REQUEST) |
| 116 | + ) |
| 117 | + ->willReturn($response = new Response()); |
| 118 | + |
| 119 | + $controller = new ErrorController($kernel, $_controller, new ErrorRenderer([])); |
| 120 | + |
| 121 | + $this->assertSame($response, $controller->preview(new Request(), $code)); |
| 122 | + } |
| 123 | +} |
0 commit comments