Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes http status code message. #37

Merged
merged 1 commit into from
Dec 19, 2024
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
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 2 additions & 7 deletions src/Code.php
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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()));
}
Expand Down
9 changes: 9 additions & 0 deletions tests/Drivers/Laminas/LaminasTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
}
9 changes: 9 additions & 0 deletions tests/Drivers/Slim/SlimTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
}
20 changes: 10 additions & 10 deletions tests/Response/CodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
]
];
}
Expand Down
Loading