From b47a4e8c1557b9a1fe4c18181d5133c9b6d75623 Mon Sep 17 00:00:00 2001 From: Gustavo Freze Date: Thu, 19 Dec 2024 11:50:24 -0300 Subject: [PATCH] fix: Fixes http status code message. --- README.md | 7 ++++--- src/Code.php | 9 ++------- tests/Drivers/Laminas/LaminasTest.php | 9 +++++++++ tests/Drivers/Slim/SlimTest.php | 9 +++++++++ tests/Response/CodeTest.php | 20 ++++++++++---------- 5 files changed, 34 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index ca3f60b..a93eba6 100644 --- a/README.md +++ b/README.md @@ -47,9 +47,10 @@ corresponding messages, and check for various status code ranges using the metho ```php use TinyBlocks\Http\Code; - Code::OK->message(); # 200 OK - Code::IM_A_TEAPOT->message(); # 418 I'm a teapot - Code::INTERNAL_SERVER_ERROR->message(); # 500 Internal Server Error + Code::OK->value; # 200 + Code::OK->message(); # OK + Code::IM_A_TEAPOT->message(); # I'm a teapot + Code::INTERNAL_SERVER_ERROR->message(); # Internal Server Error ``` - **Check if the code is valid**: Determines if the given code is a valid HTTP status code represented by the enum. diff --git a/src/Code.php b/src/Code.php index 856c74b..424fb82 100644 --- a/src/Code.php +++ b/src/Code.php @@ -4,8 +4,6 @@ namespace TinyBlocks\Http; -use BackedEnum; - /** * HTTP response status codes indicate whether a specific HTTP request has been successfully completed. * Responses are grouped in five classes: @@ -106,10 +104,7 @@ public function message(): string default => mb_convert_case($this->name, MB_CASE_TITLE) }; - $message = str_replace('_', ' ', $subject); - $template = '%s %s'; - - return sprintf($template, $this->value, $message); + return str_replace('_', ' ', $subject); } /** @@ -120,7 +115,7 @@ public function message(): string */ public static function isValidCode(int $code): bool { - $mapper = fn(BackedEnum $enum): int => $enum->value; + $mapper = fn(Code $code): int => $code->value; return in_array($code, array_map($mapper, self::cases())); } diff --git a/tests/Drivers/Laminas/LaminasTest.php b/tests/Drivers/Laminas/LaminasTest.php index 53360bf..4d8308b 100644 --- a/tests/Drivers/Laminas/LaminasTest.php +++ b/tests/Drivers/Laminas/LaminasTest.php @@ -82,5 +82,14 @@ public function testResponseEmissionWithLaminas(): void /** @Then the emitted response content should match the response body */ self::assertSame($response->getBody()->__toString(), $actual); + + /** @And the response status code should be 200 */ + self::assertSame(200, $response->getStatusCode()); + + /** @And the reason phrase should be 'OK' */ + self::assertSame('OK', $response->getReasonPhrase()); + + /** @And the response should contain the X-Request-ID header */ + self::assertSame('123456', $response->getHeaderLine(name: 'X-Request-ID')); } } diff --git a/tests/Drivers/Slim/SlimTest.php b/tests/Drivers/Slim/SlimTest.php index 0263604..706e2e0 100644 --- a/tests/Drivers/Slim/SlimTest.php +++ b/tests/Drivers/Slim/SlimTest.php @@ -82,5 +82,14 @@ public function testResponseEmissionWithSlim(): void /** @Then the emitted response content should match the response body */ self::assertSame($response->getBody()->__toString(), $actual); + + /** @And the response status code should be 200 */ + self::assertSame(200, $response->getStatusCode()); + + /** @And the reason phrase should be 'OK' */ + self::assertSame('OK', $response->getReasonPhrase()); + + /** @And the response should contain the X-Request-ID header */ + self::assertSame('123456', $response->getHeaderLine(name: 'X-Request-ID')); } } diff --git a/tests/Response/CodeTest.php b/tests/Response/CodeTest.php index 3ab83ed..05143d4 100644 --- a/tests/Response/CodeTest.php +++ b/tests/Response/CodeTest.php @@ -59,43 +59,43 @@ public static function messagesDataProvider(): array return [ 'OK message' => [ 'code' => Code::OK, - 'expected' => '200 OK' + 'expected' => 'OK' ], 'Created message' => [ 'code' => Code::CREATED, - 'expected' => '201 Created' + 'expected' => 'Created' ], 'IM Used message' => [ 'code' => Code::IM_USED, - 'expected' => '226 IM Used' + 'expected' => 'IM Used' ], 'Continue message' => [ 'code' => Code::CONTINUE, - 'expected' => '100 Continue' + 'expected' => 'Continue' ], "I'm a teapot message" => [ 'code' => Code::IM_A_TEAPOT, - 'expected' => "418 I'm a teapot" + 'expected' => "I'm a teapot" ], 'Permanent Redirect message' => [ 'code' => Code::PERMANENT_REDIRECT, - 'expected' => '308 Permanent Redirect' + 'expected' => 'Permanent Redirect' ], 'Internal Server Error message' => [ 'code' => Code::INTERNAL_SERVER_ERROR, - 'expected' => '500 Internal Server Error' + 'expected' => 'Internal Server Error' ], 'Non Authoritative Information message' => [ 'code' => Code::NON_AUTHORITATIVE_INFORMATION, - 'expected' => '203 Non Authoritative Information' + 'expected' => 'Non Authoritative Information' ], 'Proxy Authentication Required message' => [ 'code' => Code::PROXY_AUTHENTICATION_REQUIRED, - 'expected' => '407 Proxy Authentication Required' + 'expected' => 'Proxy Authentication Required' ], 'Network Authentication Required message' => [ 'code' => Code::NETWORK_AUTHENTICATION_REQUIRED, - 'expected' => '511 Network Authentication Required' + 'expected' => 'Network Authentication Required' ] ]; }