|
22 | 22 |
|
23 | 23 | final class ResponseTest extends TestCase |
24 | 24 | { |
| 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 | + |
25 | 50 | public function testResponseOk(): void |
26 | 51 | { |
27 | 52 | /** @Given a body with data */ |
@@ -351,6 +376,37 @@ public function testResponseInternalServerError(): void |
351 | 376 | self::assertSame(['Content-Type' => ['application/json; charset=utf-8']], $actual->getHeaders()); |
352 | 377 | } |
353 | 378 |
|
| 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 | + |
354 | 410 | #[DataProvider('bodyProviderData')] |
355 | 411 | public function testResponseBodySerialization(mixed $body, string $expected): void |
356 | 412 | { |
|
0 commit comments