diff --git a/src/Business/Executor/HttpExceptionPageExecutorDecorator.php b/src/Business/Executor/HttpExceptionPageExecutorDecorator.php index 24d10f8..ff07764 100644 --- a/src/Business/Executor/HttpExceptionPageExecutorDecorator.php +++ b/src/Business/Executor/HttpExceptionPageExecutorDecorator.php @@ -45,10 +45,6 @@ public function execute(Request $request, bool $flush = true): Response return $response; } catch (\Throwable $throwable) { - if (!$flush) { - throw $throwable; - } - $content = $this->rendererFactory ->create($request) ->render($throwable); @@ -59,21 +55,20 @@ public function execute(Request $request, bool $flush = true): Response } $contentType = $request->get('_format', 'text/html'); - switch ($contentType) { - case 'json': - $contentType = 'application/json'; - break; - default: - $contentType = 'text/html'; - } + $contentType = match ($contentType) { + 'json' => 'application/json', + default => 'text/html', + }; $response = new Response($content, $statusCode, [ 'content-type' => $contentType, ]); - $response->send(); + if ($flush) { + $response->send(); + } - throw $throwable; + return $response; } } } diff --git a/src/HttpExceptionResponseDevPluginConfiguration.php b/src/HttpExceptionResponseDevPluginConfiguration.php index 40725a3..86f95cc 100644 --- a/src/HttpExceptionResponseDevPluginConfiguration.php +++ b/src/HttpExceptionResponseDevPluginConfiguration.php @@ -22,7 +22,7 @@ class HttpExceptionResponseDevPluginConfiguration extends PluginConfiguration implements HttpExceptionResponseDevPluginConfigurationInterface { public const CFG_DECORATED_WEIGHT = 'MICRO_HTTP_EXCEPTION_DEV_PAGE_DECORATION_WEIGHT'; - public const DECORATED_DEFAULT = 200; + public const DECORATED_DEFAULT = 1001; public function getProjectDir(): string { diff --git a/tests/Unit/HttpExceptionPagePluginTest.php b/tests/Unit/HttpExceptionPagePluginTest.php index c007209..be49b14 100644 --- a/tests/Unit/HttpExceptionPagePluginTest.php +++ b/tests/Unit/HttpExceptionPagePluginTest.php @@ -21,6 +21,7 @@ use Micro\Plugin\Http\Facade\HttpFacadeInterface; use PHPUnit\Framework\TestCase; use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Response; /** * @author Stanislau Komar @@ -72,44 +73,40 @@ public function testDecorated() /** * @dataProvider dataProviderException */ - public function testExceptionResponse(string $env, string $uri, bool $isFlush, mixed $result, string $format) + public function testExceptionResponse(string $env, string $uri, bool $isFlush, mixed $result, string $format): void { $kernel = $this->createKernel($env); $request = Request::create($uri); $request->request->set('_format', $format); $isDev = str_starts_with($env, 'dev'); + if (!$isDev) { + $this->expectException(HttpException::class); + } + preg_match('/\d+/', $uri, $match); $exceptionCode = (int) $match[0]; - $this->expectException(HttpException::class); - $this->expectExceptionCode($exceptionCode); - - ob_start(); - - try { + if (!$isDev) { $response = $kernel->container()->get(HttpFacadeInterface::class) ->execute($request, $isFlush); + } else { + ob_start(); + /** @var Response $response */ + $response = $kernel->container()->get(HttpFacadeInterface::class) + ->execute($request, $isFlush); + $flushedContent = ob_get_contents(); + ob_end_clean(); + } - $flushedContent = ob_get_clean(); - } catch (HttpException $httpException) { - $flushedContent = ob_get_clean(); - - if ($isFlush) { - if ('json' === $format) { - $this->assertJson($flushedContent); - } + $responseContent = $response->getContent(); - if ('html' === $format) { - $this->assertStringStartsWith('', $flushedContent); - } - } + $this->assertEquals($exceptionCode, $response->getStatusCode()); - throw $httpException; + if ($isFlush) { + $this->assertEquals($responseContent, $flushedContent); } - $responseContent = $response->getContent(); if ('html' === $format) { $this->assertStringStartsWith('', $responseContent); @@ -118,15 +115,12 @@ public function testExceptionResponse(string $env, string $uri, bool $isFlush, m if ('json' === $format) { $this->assertJson($responseContent); } - - $this->assertStringEndsWith($responseContent, $flushedContent); - $this->assertStringStartsWith($responseContent, $flushedContent); } /** * @dataProvider dataProviderSuccess */ - public function testSuccessResponse(string $env, string $uri, bool $isFlush, mixed $result, string $format) + public function testSuccessResponse(string $env, string $uri, bool $isFlush, mixed $result, string $format): void { $kernel = $this->createKernel($env); $request = Request::create($uri); @@ -139,11 +133,11 @@ public function testSuccessResponse(string $env, string $uri, bool $isFlush, mix $responseFlushedContent = ob_get_clean(); - $this->assertEquals($isFlush ? $result : '', $responseFlushedContent); + $this->assertEquals($isFlush ? $result : false, $responseFlushedContent); $this->assertEquals($result, $response->getContent()); } - public function dataProviderSuccess() + public function dataProviderSuccess(): array { return [ ['dev', '/', true, 'Hello, world', 'html'], @@ -159,7 +153,7 @@ public function dataProviderSuccess() ]; } - public function dataProviderException() + public function dataProviderException(): array { return [ ['dev', '/404', true, null, 'html'],