Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/App/src/Exception/RuntimeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/App/src/Exception/SunsetException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
3 changes: 2 additions & 1 deletion src/App/src/Service/ErrorReportService.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ private function isMatchingIpAddress(ServerRequestInterface $request): bool
}

/**
* @throws ForbiddenException
* @throws RuntimeException
*/
private function validateConfigs(): void
Expand All @@ -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)) {
Expand Down
26 changes: 13 additions & 13 deletions test/Unit/App/Attribute/ResourceDeprecationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,26 @@
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;

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());
}
Comment thread
alexmerlin marked this conversation as resolved.
}

public function testValidDatePassesValidation(): void
Expand Down
19 changes: 19 additions & 0 deletions test/Unit/App/Exception/RuntimeExceptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace ApiTest\Unit\App\Exception;

use Api\App\Exception\RuntimeException;
use Fig\Http\Message\StatusCodeInterface;
use PHPUnit\Framework\TestCase;

class RuntimeExceptionTest extends TestCase
{
public function testCreateReturnsInternalServerError(): void
{
$exception = RuntimeException::create('detail');

$this->assertSame(StatusCodeInterface::STATUS_INTERNAL_SERVER_ERROR, $exception->getStatus());
}
}
26 changes: 26 additions & 0 deletions test/Unit/App/Service/ErrorReportServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace ApiTest\Unit\App\Service;

use Api\App\Exception\ForbiddenException;
use Api\App\Service\ErrorReportService;
use Api\App\Service\ErrorReportServiceInterface;
use Core\App\Message;
use Laminas\Diactoros\ServerRequest;
use PHPUnit\Framework\TestCase;

class ErrorReportServiceTest extends TestCase
{
public function testDisabledErrorReportingThrowsForbidden(): void
{
$service = new ErrorReportService([
ErrorReportServiceInterface::class => ['enabled' => false],
]);

$this->expectExceptionObject(ForbiddenException::create(Message::ERROR_REPORT_NOT_ENABLED));

$service->checkRequest(new ServerRequest());
}
}
Loading