Skip to content

Commit 5a64329

Browse files
authored
Merge pull request #567 from dotkernel/issue-543
updated exceptions
2 parents b7c92dc + e070ca3 commit 5a64329

6 files changed

Lines changed: 62 additions & 16 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: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,26 @@
77
use Api\App\Attribute\ResourceDeprecation;
88
use Api\App\Exception\SunsetException;
99
use Api\App\Middleware\DeprecationMiddleware;
10+
use Core\App\Message;
11+
use Fig\Http\Message\StatusCodeInterface;
1012
use PHPUnit\Framework\TestCase;
1113
use ReflectionClass;
1214

1315
class ResourceDeprecationTest extends TestCase
1416
{
1517
public function testInvalidDateThrowsException(): void
1618
{
17-
$class = new #[ResourceDeprecation(
18-
sunset: 'invalid-01-01',
19-
link: 'test-link',
20-
deprecationReason: 'test-deprecation-reason',
21-
)] class {
22-
};
23-
24-
$reflectionClass = new ReflectionClass($class);
25-
$attributes = $reflectionClass->getAttributes(ResourceDeprecation::class);
26-
27-
$this->expectException(SunsetException::class);
28-
29-
$attributes[0]->newInstance();
19+
try {
20+
new ResourceDeprecation(
21+
sunset: 'invalid-01-01',
22+
link: 'test-link',
23+
deprecationReason: 'test-deprecation-reason',
24+
);
25+
$this->fail('SunsetException was not thrown');
26+
} catch (SunsetException $exception) {
27+
$this->assertSame(Message::invalidValue('sunset'), $exception->getDetail());
28+
$this->assertSame(StatusCodeInterface::STATUS_INTERNAL_SERVER_ERROR, $exception->getStatus());
29+
}
3030
}
3131

3232
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)