From 8c566eca1dae6184beaf0eca0856018e3e54934b Mon Sep 17 00:00:00 2001 From: bidi47 Date: Wed, 14 Oct 2026 19:56:32 +0300 Subject: [PATCH 1/2] updated exceptions Signed-off-by: bidi47 --- src/App/src/Exception/RuntimeException.php | 2 +- src/App/src/Exception/SunsetException.php | 2 +- src/App/src/Service/ErrorReportService.php | 3 ++- .../App/Attribute/ResourceDeprecationTest.php | 10 ++++--- .../App/Exception/RuntimeExceptionTest.php | 19 ++++++++++++++ .../App/Service/ErrorReportServiceTest.php | 26 +++++++++++++++++++ 6 files changed, 56 insertions(+), 6 deletions(-) create mode 100644 test/Unit/App/Exception/RuntimeExceptionTest.php create mode 100644 test/Unit/App/Service/ErrorReportServiceTest.php 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..66f0ea56 100644 --- a/test/Unit/App/Attribute/ResourceDeprecationTest.php +++ b/test/Unit/App/Attribute/ResourceDeprecationTest.php @@ -7,6 +7,7 @@ use Api\App\Attribute\ResourceDeprecation; use Api\App\Exception\SunsetException; use Api\App\Middleware\DeprecationMiddleware; +use Fig\Http\Message\StatusCodeInterface; use PHPUnit\Framework\TestCase; use ReflectionClass; @@ -24,9 +25,12 @@ public function testInvalidDateThrowsException(): void $reflectionClass = new ReflectionClass($class); $attributes = $reflectionClass->getAttributes(ResourceDeprecation::class); - $this->expectException(SunsetException::class); - - $attributes[0]->newInstance(); + try { + $attributes[0]->newInstance(); + $this->fail('SunsetException was not thrown'); + } catch (SunsetException $exception) { + $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()); + } +} From e070ca383202e24a46db930c0fb4b376a56fa097 Mon Sep 17 00:00:00 2001 From: bidi47 Date: Thu, 15 Oct 2026 12:49:32 +0300 Subject: [PATCH 2/2] updated test Signed-off-by: bidi47 --- .../App/Attribute/ResourceDeprecationTest.php | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/test/Unit/App/Attribute/ResourceDeprecationTest.php b/test/Unit/App/Attribute/ResourceDeprecationTest.php index 66f0ea56..f26e4e88 100644 --- a/test/Unit/App/Attribute/ResourceDeprecationTest.php +++ b/test/Unit/App/Attribute/ResourceDeprecationTest.php @@ -7,6 +7,7 @@ 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; @@ -15,20 +16,15 @@ 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); - try { - $attributes[0]->newInstance(); + 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()); } }