ErrorControllerTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpKernel\Tests\Controller;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\ErrorHandler\ErrorRenderer\HtmlErrorRenderer;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\HttpKernel\Controller\ErrorController;
  16. use Symfony\Component\HttpKernel\Exception\HttpException;
  17. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  18. use Symfony\Component\HttpKernel\HttpKernelInterface;
  19. class ErrorControllerTest extends TestCase
  20. {
  21. /**
  22. * @dataProvider getInvokeControllerDataProvider
  23. */
  24. public function testInvokeController(Request $request, \Exception $exception, int $statusCode, string $content)
  25. {
  26. $kernel = $this->createMock(HttpKernelInterface::class);
  27. $errorRenderer = new HtmlErrorRenderer();
  28. $controller = new ErrorController($kernel, null, $errorRenderer);
  29. $response = $controller($exception);
  30. $this->assertSame($statusCode, $response->getStatusCode());
  31. self::assertStringContainsString($content, strtr($response->getContent(), ["\n" => '', ' ' => '']));
  32. }
  33. public static function getInvokeControllerDataProvider()
  34. {
  35. yield 'default status code and HTML format' => [
  36. new Request(),
  37. new \Exception(),
  38. 500,
  39. 'The server returned a "500 Internal Server Error".',
  40. ];
  41. yield 'custom status code' => [
  42. new Request(),
  43. new NotFoundHttpException('Page not found.'),
  44. 404,
  45. 'The server returned a "404 Not Found".',
  46. ];
  47. $request = new Request();
  48. $request->attributes->set('_format', 'unknown');
  49. yield 'default HTML format for unknown formats' => [
  50. $request,
  51. new HttpException(405, 'Invalid request.'),
  52. 405,
  53. 'The server returned a "405 Method Not Allowed".',
  54. ];
  55. }
  56. public function testPreviewController()
  57. {
  58. $_controller = 'error_controller';
  59. $code = 404;
  60. $kernel = $this->createMock(HttpKernelInterface::class);
  61. $kernel
  62. ->expects($this->once())
  63. ->method('handle')
  64. ->with(
  65. $this->callback(function (Request $request) use ($_controller, $code) {
  66. $exception = $request->attributes->get('exception');
  67. $this->assertSame($_controller, $request->attributes->get('_controller'));
  68. $this->assertInstanceOf(\Throwable::class, $exception);
  69. $this->assertSame($code, $exception->getStatusCode());
  70. $this->assertFalse($request->attributes->get('showException'));
  71. return true;
  72. }),
  73. $this->equalTo(HttpKernelInterface::SUB_REQUEST)
  74. )
  75. ->willReturn($response = new Response());
  76. $controller = new ErrorController($kernel, $_controller, new HtmlErrorRenderer());
  77. $this->assertSame($response, $controller->preview(new Request(), $code));
  78. }
  79. }