|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Tempest\Integration\Http; |
| 4 | + |
| 5 | +use Closure; |
| 6 | +use Exception; |
| 7 | +use PHPUnit\Framework\Attributes\TestWith; |
| 8 | +use Tempest\Container\Container; |
| 9 | +use Tempest\Core\AppConfig; |
| 10 | +use Tempest\Core\FrameworkKernel; |
| 11 | +use Tempest\Core\Kernel; |
| 12 | +use Tempest\Http\HttpException; |
| 13 | +use Tempest\Http\Response; |
| 14 | +use Tempest\Http\Responses\Redirect; |
| 15 | +use Tempest\Http\Status; |
| 16 | +use Tempest\Router\Exceptions\HttpExceptionHandler; |
| 17 | +use Tempest\Router\Exceptions\NotFoundException; |
| 18 | +use Tempest\Router\ResponseSender; |
| 19 | +use Tests\Tempest\Integration\FrameworkIntegrationTestCase; |
| 20 | +use Tests\Tempest\Integration\Http\Fixtures\ExceptionThatSendsRedirectResponse; |
| 21 | +use Tests\Tempest\Integration\Http\Fixtures\ExceptionWithContext; |
| 22 | +use Tests\Tempest\Integration\Http\Fixtures\NullExceptionProcessor; |
| 23 | + |
| 24 | +final class HttpExceptionHandlerTest extends FrameworkIntegrationTestCase |
| 25 | +{ |
| 26 | + public ?Response $response = null; |
| 27 | + |
| 28 | + protected function setUp(): void |
| 29 | + { |
| 30 | + parent::setUp(); |
| 31 | + |
| 32 | + $this->container->singleton( |
| 33 | + Kernel::class, |
| 34 | + fn () => new class($this->container->get(FrameworkKernel::class)) implements Kernel { |
| 35 | + public const string VERSION = '1.0.0-alpha.6'; |
| 36 | + |
| 37 | + public string $root; |
| 38 | + |
| 39 | + public string $internalStorage; |
| 40 | + |
| 41 | + public array $discoveryLocations; |
| 42 | + |
| 43 | + public array $discoveryClasses; |
| 44 | + |
| 45 | + public Container $container; |
| 46 | + |
| 47 | + public function __construct(FrameworkKernel $kernel) |
| 48 | + { |
| 49 | + $this->root = $kernel->root; |
| 50 | + $this->internalStorage = $kernel->internalStorage; |
| 51 | + $this->discoveryLocations = $kernel->discoveryLocations; |
| 52 | + $this->discoveryClasses = $kernel->discoveryClasses; |
| 53 | + $this->container = $kernel->container; |
| 54 | + } |
| 55 | + |
| 56 | + public static function boot(string $root, array $discoveryLocations = [], ?Container $container = null): self |
| 57 | + { |
| 58 | + // This is just to make static analysis pass, this is never called. |
| 59 | + // @mago-expect analysis/undefined-function-or-method |
| 60 | + return Kernel::boot($root, $discoveryLocations, $container); |
| 61 | + } |
| 62 | + |
| 63 | + public function shutdown(int|string $status = ''): never |
| 64 | + { |
| 65 | + throw new Exception('Shutdown.'); |
| 66 | + } |
| 67 | + }, |
| 68 | + ); |
| 69 | + |
| 70 | + $this->container->singleton( |
| 71 | + ResponseSender::class, |
| 72 | + fn () => new class($this) implements ResponseSender { |
| 73 | + public function __construct( |
| 74 | + private HttpExceptionHandlerTest $case, |
| 75 | + ) {} |
| 76 | + |
| 77 | + public function send(Response $response): Response |
| 78 | + { |
| 79 | + $this->case->response = $response; |
| 80 | + |
| 81 | + return $response; |
| 82 | + } |
| 83 | + }, |
| 84 | + ); |
| 85 | + } |
| 86 | + |
| 87 | + public function test_exception_handler_shuts_down_kernel(): void |
| 88 | + { |
| 89 | + $this->expectExceptionMessage('Shutdown.'); |
| 90 | + |
| 91 | + $handler = $this->container->get(HttpExceptionHandler::class); |
| 92 | + $handler->handle(new Exception()); |
| 93 | + } |
| 94 | + |
| 95 | + public function test_exception_handler_sends_response_specified_by_sends_response(): void |
| 96 | + { |
| 97 | + $this->callExceptionHandler(function (): void { |
| 98 | + $handler = $this->container->get(HttpExceptionHandler::class); |
| 99 | + $handler->handle(new ExceptionThatSendsRedirectResponse()); |
| 100 | + }); |
| 101 | + |
| 102 | + $this->assertInstanceOf(Redirect::class, $this->response); |
| 103 | + $this->assertContains('https://tempestphp.com', $this->response->getHeader('Location')->values); |
| 104 | + } |
| 105 | + |
| 106 | + public function test_exception_handler_returns_500_by_default(): void |
| 107 | + { |
| 108 | + $this->callExceptionHandler(function (): void { |
| 109 | + $handler = $this->container->get(HttpExceptionHandler::class); |
| 110 | + $handler->handle(new Exception()); |
| 111 | + }); |
| 112 | + |
| 113 | + $this->assertSame(Status::INTERNAL_SERVER_ERROR, $this->response->status); |
| 114 | + $this->assertStringContainsString('An unexpected server error occurred', $this->render($this->response->body)); |
| 115 | + } |
| 116 | + |
| 117 | + public function test_exception_handler_returns_404_for_router_not_found_execption(): void |
| 118 | + { |
| 119 | + $this->callExceptionHandler(function (): void { |
| 120 | + $handler = $this->container->get(HttpExceptionHandler::class); |
| 121 | + $handler->handle(new NotFoundException()); |
| 122 | + }); |
| 123 | + |
| 124 | + $this->assertSame(Status::NOT_FOUND, $this->response->status); |
| 125 | + $this->assertStringContainsString('This page could not be found on the server', $this->render($this->response->body)); |
| 126 | + } |
| 127 | + |
| 128 | + #[TestWith([Status::BAD_REQUEST])] |
| 129 | + #[TestWith([Status::INTERNAL_SERVER_ERROR])] |
| 130 | + #[TestWith([Status::NOT_FOUND])] |
| 131 | + #[TestWith([Status::FORBIDDEN])] |
| 132 | + #[TestWith([Status::METHOD_NOT_ALLOWED])] |
| 133 | + public function test_exception_handler_returns_sane_code_as_http_exception(Status $status): void |
| 134 | + { |
| 135 | + $this->callExceptionHandler(function () use ($status): void { |
| 136 | + $handler = $this->container->get(HttpExceptionHandler::class); |
| 137 | + $handler->handle(new HttpException($status)); |
| 138 | + }); |
| 139 | + |
| 140 | + $this->assertSame($status, $this->response->status); |
| 141 | + } |
| 142 | + |
| 143 | + public function test_exception_handler_runs_exception_processors(): void |
| 144 | + { |
| 145 | + $this->container->get(AppConfig::class)->exceptionProcessors[] = NullExceptionProcessor::class; |
| 146 | + |
| 147 | + $thrown = new ExceptionWithContext(); |
| 148 | + |
| 149 | + $this->callExceptionHandler(function () use ($thrown): void { |
| 150 | + $handler = $this->container->get(HttpExceptionHandler::class); |
| 151 | + $handler->handle($thrown); |
| 152 | + }); |
| 153 | + |
| 154 | + $this->assertContains($thrown, NullExceptionProcessor::$exceptions); |
| 155 | + $this->assertArrayHasKey('foo', NullExceptionProcessor::$exceptions[0]->context()); |
| 156 | + } |
| 157 | + |
| 158 | + private function callExceptionHandler(Closure $callback): void |
| 159 | + { |
| 160 | + try { |
| 161 | + $callback(); |
| 162 | + } catch (\Throwable $throwable) { |
| 163 | + $this->assertSame('Shutdown.', $throwable->getMessage()); |
| 164 | + } |
| 165 | + } |
| 166 | +} |
0 commit comments