diff --git a/src/App/src/Exception/RuntimeException.php b/src/App/src/Exception/RuntimeException.php index aaf822bb..e0d04882 100644 --- a/src/App/src/Exception/RuntimeException.php +++ b/src/App/src/Exception/RuntimeException.php @@ -22,7 +22,7 @@ public static function create(string $detail, string $type = '', string $title = $exception->type = $type; $exception->detail = $detail; - $exception->status = StatusCodeInterface::STATUS_BAD_REQUEST; + $exception->status = StatusCodeInterface::STATUS_INTERNAL_SERVER_ERROR; $exception->title = $title; $exception->additional = $additional; diff --git a/src/App/src/Exception/SunsetException.php b/src/App/src/Exception/SunsetException.php index 2adf0d1e..6308ffdd 100644 --- a/src/App/src/Exception/SunsetException.php +++ b/src/App/src/Exception/SunsetException.php @@ -23,7 +23,7 @@ public static function create(string $detail, string $type = '', string $title = $exception->type = $type; $exception->detail = $detail; - $exception->status = StatusCodeInterface::STATUS_UNAUTHORIZED; + $exception->status = StatusCodeInterface::STATUS_INTERNAL_SERVER_ERROR; $exception->title = $title; $exception->additional = $additional; diff --git a/src/App/src/Service/ErrorReportService.php b/src/App/src/Service/ErrorReportService.php index 6734607b..d69a7317 100644 --- a/src/App/src/Service/ErrorReportService.php +++ b/src/App/src/Service/ErrorReportService.php @@ -116,6 +116,7 @@ private function isMatchingIpAddress(ServerRequestInterface $request): bool } /** + * @throws ForbiddenException * @throws RuntimeException */ private function validateConfigs(): void @@ -127,7 +128,7 @@ private function validateConfigs(): void } if ($this->config['enabled'] !== true) { - throw RuntimeException::create(Message::ERROR_REPORT_NOT_ENABLED); + throw ForbiddenException::create(Message::ERROR_REPORT_NOT_ENABLED); } if (! array_key_exists('path', $this->config)) { diff --git a/test/Unit/App/Attribute/ResourceDeprecationTest.php b/test/Unit/App/Attribute/ResourceDeprecationTest.php index f89e7e04..f26e4e88 100644 --- a/test/Unit/App/Attribute/ResourceDeprecationTest.php +++ b/test/Unit/App/Attribute/ResourceDeprecationTest.php @@ -7,6 +7,8 @@ use Api\App\Attribute\ResourceDeprecation; use Api\App\Exception\SunsetException; use Api\App\Middleware\DeprecationMiddleware; +use Core\App\Message; +use Fig\Http\Message\StatusCodeInterface; use PHPUnit\Framework\TestCase; use ReflectionClass; @@ -14,19 +16,17 @@ class ResourceDeprecationTest extends TestCase { public function testInvalidDateThrowsException(): void { - $class = new #[ResourceDeprecation( - sunset: 'invalid-01-01', - link: 'test-link', - deprecationReason: 'test-deprecation-reason', - )] class { - }; - - $reflectionClass = new ReflectionClass($class); - $attributes = $reflectionClass->getAttributes(ResourceDeprecation::class); - - $this->expectException(SunsetException::class); - - $attributes[0]->newInstance(); + try { + new ResourceDeprecation( + sunset: 'invalid-01-01', + link: 'test-link', + deprecationReason: 'test-deprecation-reason', + ); + $this->fail('SunsetException was not thrown'); + } catch (SunsetException $exception) { + $this->assertSame(Message::invalidValue('sunset'), $exception->getDetail()); + $this->assertSame(StatusCodeInterface::STATUS_INTERNAL_SERVER_ERROR, $exception->getStatus()); + } } public function testValidDatePassesValidation(): void diff --git a/test/Unit/App/Exception/RuntimeExceptionTest.php b/test/Unit/App/Exception/RuntimeExceptionTest.php new file mode 100644 index 00000000..cb60f8e8 --- /dev/null +++ b/test/Unit/App/Exception/RuntimeExceptionTest.php @@ -0,0 +1,19 @@ +assertSame(StatusCodeInterface::STATUS_INTERNAL_SERVER_ERROR, $exception->getStatus()); + } +} diff --git a/test/Unit/App/Service/ErrorReportServiceTest.php b/test/Unit/App/Service/ErrorReportServiceTest.php new file mode 100644 index 00000000..c5360e54 --- /dev/null +++ b/test/Unit/App/Service/ErrorReportServiceTest.php @@ -0,0 +1,26 @@ + ['enabled' => false], + ]); + + $this->expectExceptionObject(ForbiddenException::create(Message::ERROR_REPORT_NOT_ENABLED)); + + $service->checkRequest(new ServerRequest()); + } +}