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 packages/http-client/src/Driver/Psr18Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ private function convertTempestRequestToPsrRequest(Request $tempestRequest): Req
private function convertPsrResponseToTempestResponse(ResponseInterface $response): Response
{
return new GenericResponse(
status: Status::code($response->getStatusCode()),
status: Status::fromCode($response->getStatusCode()),
body: $response->getBody()->getContents(),
headers: $response->getHeaders(),
);
Expand Down
16 changes: 14 additions & 2 deletions packages/http/src/Status.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Tempest\Http;

use ValueError;

enum Status: int
{
// Informational
Expand Down Expand Up @@ -79,9 +81,19 @@ enum Status: int
case NOT_EXTENDED = 510;
case NETWORK_AUTHENTICATION_REQUIRED = 511;

public static function code(int $code): self
public static function fromCode(int $code): self
{
return self::from($code);
try {
return self::from($code);
} catch (ValueError) {
return match (intdiv($code, 100) * 100) {
100 => self::CONTINUE,
200 => self::OK,
300 => self::MULTIPLE_CHOICES,
400 => self::BAD_REQUEST,
default => self::INTERNAL_SERVER_ERROR,
};
}
}

public function description(): string
Expand Down
16 changes: 15 additions & 1 deletion packages/http/tests/StatusTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace Tempest\Http\Tests;

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\Attributes\TestWith;
use PHPUnit\Framework\TestCase;
use Tempest\Http\Status;

Expand All @@ -25,7 +27,7 @@ private static function descriptionToStatus(string $description): Status
#[DataProvider('provide_status_code_cases')]
public function test_status_code(int $code, string $description): void
{
$status = Status::code($code);
$status = Status::fromCode($code);

$this->assertSame(
self::descriptionToStatus($description),
Expand Down Expand Up @@ -143,4 +145,16 @@ public static function provide_status_code_cases(): iterable
[511, 'Network Authentication Required'],
];
}

#[TestWith([150, Status::CONTINUE])]
#[TestWith([250, Status::OK])]
#[TestWith([399, Status::MULTIPLE_CHOICES])]
#[TestWith([450, Status::BAD_REQUEST])]
#[TestWith([550, Status::INTERNAL_SERVER_ERROR])]
#[TestWith([650, Status::INTERNAL_SERVER_ERROR])]
#[Test]
public function unknown_status_code_maps_to_class_base(int $code, Status $expected): void
{
$this->assertSame($expected, Status::fromCode($code));
}
}