Skip to content

Commit 8c566ec

Browse files
committed
updated exceptions
Signed-off-by: bidi47 <bidi@apidemia.com>
1 parent b7c92dc commit 8c566ec

6 files changed

Lines changed: 56 additions & 6 deletions

File tree

‎src/App/src/Exception/RuntimeException.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public static function create(string $detail, string $type = '', string $title =
2222

2323
$exception->type = $type;
2424
$exception->detail = $detail;
25-
$exception->status = StatusCodeInterface::STATUS_BAD_REQUEST;
25+
$exception->status = StatusCodeInterface::STATUS_INTERNAL_SERVER_ERROR;
2626
$exception->title = $title;
2727
$exception->additional = $additional;
2828

‎src/App/src/Exception/SunsetException.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public static function create(string $detail, string $type = '', string $title =
2323

2424
$exception->type = $type;
2525
$exception->detail = $detail;
26-
$exception->status = StatusCodeInterface::STATUS_UNAUTHORIZED;
26+
$exception->status = StatusCodeInterface::STATUS_INTERNAL_SERVER_ERROR;
2727
$exception->title = $title;
2828
$exception->additional = $additional;
2929

‎src/App/src/Service/ErrorReportService.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ private function isMatchingIpAddress(ServerRequestInterface $request): bool
116116
}
117117

118118
/**
119+
* @throws ForbiddenException
119120
* @throws RuntimeException
120121
*/
121122
private function validateConfigs(): void
@@ -127,7 +128,7 @@ private function validateConfigs(): void
127128
}
128129

129130
if ($this->config['enabled'] !== true) {
130-
throw RuntimeException::create(Message::ERROR_REPORT_NOT_ENABLED);
131+
throw ForbiddenException::create(Message::ERROR_REPORT_NOT_ENABLED);
131132
}
132133

133134
if (! array_key_exists('path', $this->config)) {

‎test/Unit/App/Attribute/ResourceDeprecationTest.php‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Api\App\Attribute\ResourceDeprecation;
88
use Api\App\Exception\SunsetException;
99
use Api\App\Middleware\DeprecationMiddleware;
10+
use Fig\Http\Message\StatusCodeInterface;
1011
use PHPUnit\Framework\TestCase;
1112
use ReflectionClass;
1213

@@ -24,9 +25,12 @@ public function testInvalidDateThrowsException(): void
2425
$reflectionClass = new ReflectionClass($class);
2526
$attributes = $reflectionClass->getAttributes(ResourceDeprecation::class);
2627

27-
$this->expectException(SunsetException::class);
28-
29-
$attributes[0]->newInstance();
28+
try {
29+
$attributes[0]->newInstance();
30+
$this->fail('SunsetException was not thrown');
31+
} catch (SunsetException $exception) {
32+
$this->assertSame(StatusCodeInterface::STATUS_INTERNAL_SERVER_ERROR, $exception->getStatus());
33+
}
3034
}
3135

3236
public function testValidDatePassesValidation(): void
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace ApiTest\Unit\App\Exception;
6+
7+
use Api\App\Exception\RuntimeException;
8+
use Fig\Http\Message\StatusCodeInterface;
9+
use PHPUnit\Framework\TestCase;
10+
11+
class RuntimeExceptionTest extends TestCase
12+
{
13+
public function testCreateReturnsInternalServerError(): void
14+
{
15+
$exception = RuntimeException::create('detail');
16+
17+
$this->assertSame(StatusCodeInterface::STATUS_INTERNAL_SERVER_ERROR, $exception->getStatus());
18+
}
19+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace ApiTest\Unit\App\Service;
6+
7+
use Api\App\Exception\ForbiddenException;
8+
use Api\App\Service\ErrorReportService;
9+
use Api\App\Service\ErrorReportServiceInterface;
10+
use Core\App\Message;
11+
use Laminas\Diactoros\ServerRequest;
12+
use PHPUnit\Framework\TestCase;
13+
14+
class ErrorReportServiceTest extends TestCase
15+
{
16+
public function testDisabledErrorReportingThrowsForbidden(): void
17+
{
18+
$service = new ErrorReportService([
19+
ErrorReportServiceInterface::class => ['enabled' => false],
20+
]);
21+
22+
$this->expectExceptionObject(ForbiddenException::create(Message::ERROR_REPORT_NOT_ENABLED));
23+
24+
$service->checkRequest(new ServerRequest());
25+
}
26+
}

0 commit comments

Comments
 (0)