Skip to content

Commit

Permalink
Fixes http status code message.
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavofreze authored Dec 19, 2024
1 parent 5b04afd commit d5b6fee
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 20 deletions.
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

0 comments on commit d5b6fee

Please sign in to comment.