Skip to content

Commit 9700ecb

Browse files
authored
Release/4.7.0 (#50)
1 parent 3b1387b commit 9700ecb

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

src/Response.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99

1010
final readonly class Response implements Responses
1111
{
12+
public static function from(Code $code, mixed $body, Headers ...$headers): ResponseInterface
13+
{
14+
return InternalResponse::createWithBody($body, $code, ...$headers);
15+
}
16+
1217
public static function ok(mixed $body, Headers ...$headers): ResponseInterface
1318
{
1419
return InternalResponse::createWithBody($body, Code::OK, ...$headers);

src/Responses.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@
1313
*/
1414
interface Responses
1515
{
16+
/**
17+
* Creates a response with the specified status code, body, and headers.
18+
*
19+
* @param Code $code The HTTP status code for the response.
20+
* @param mixed $body The body of the response.
21+
* @param Headers ...$headers Optional additional headers for the response.
22+
* @return ResponseInterface The generated response with the specified status code, body, and headers.
23+
*/
24+
public static function from(Code $code, mixed $body, Headers ...$headers): ResponseInterface;
25+
1626
/**
1727
* Creates a response with a 200 OK status.
1828
*

tests/ResponseTest.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,31 @@
2222

2323
final class ResponseTest extends TestCase
2424
{
25+
#[DataProvider('responseFromProvider')]
26+
public function testResponseFrom(Code $code, mixed $body, string $expectedBody): void
27+
{
28+
/** @Given a specific status code and body */
29+
/** @When we create the HTTP response using the generic from method */
30+
$actual = Response::from(code: $code, body: $body);
31+
32+
/** @Then the protocol version should be "1.1" */
33+
self::assertSame('1.1', $actual->getProtocolVersion());
34+
35+
/** @And the body of the response should match the expected output */
36+
self::assertSame($expectedBody, $actual->getBody()->__toString());
37+
self::assertSame($expectedBody, $actual->getBody()->getContents());
38+
39+
/** @And the status code should match the provided code */
40+
self::assertSame($code->value, $actual->getStatusCode());
41+
self::assertTrue(Code::isValidCode(code: $actual->getStatusCode()));
42+
43+
/** @And the reason phrase should match the provided code message */
44+
self::assertSame($code->message(), $actual->getReasonPhrase());
45+
46+
/** @And the headers should contain Content-Type as application/json with charset=utf-8 */
47+
self::assertSame(['Content-Type' => ['application/json; charset=utf-8']], $actual->getHeaders());
48+
}
49+
2550
public function testResponseOk(): void
2651
{
2752
/** @Given a body with data */
@@ -351,6 +376,37 @@ public function testResponseInternalServerError(): void
351376
self::assertSame(['Content-Type' => ['application/json; charset=utf-8']], $actual->getHeaders());
352377
}
353378

379+
public static function responseFromProvider(): array
380+
{
381+
return [
382+
'I am a teapot' => [
383+
'code' => Code::IM_A_TEAPOT,
384+
'body' => 'Short and stout',
385+
'expectedBody' => 'Short and stout'
386+
],
387+
'OK with array body' => [
388+
'code' => Code::OK,
389+
'body' => ['status' => 'success'],
390+
'expectedBody' => '{"status":"success"}'
391+
],
392+
'Accepted with null body' => [
393+
'code' => Code::ACCEPTED,
394+
'body' => null,
395+
'expectedBody' => ''
396+
],
397+
'Not Found with string body' => [
398+
'code' => Code::NOT_FOUND,
399+
'body' => 'Resource not found',
400+
'expectedBody' => 'Resource not found'
401+
],
402+
'Internal Server Error with complex body' => [
403+
'code' => Code::INTERNAL_SERVER_ERROR,
404+
'body' => ['error' => ['code' => 500, 'message' => 'Crash']],
405+
'expectedBody' => '{"error":{"code":500,"message":"Crash"}}'
406+
]
407+
];
408+
}
409+
354410
#[DataProvider('bodyProviderData')]
355411
public function testResponseBodySerialization(mixed $body, string $expected): void
356412
{

0 commit comments

Comments
 (0)