subject = new ResponseFormatter(); } public function testBuildDefault(): void { $response = $this->subject->format(new Response()); $this->assertSame(200, $response->getStatusCode()); $this->assertSame([], $response->getHeaders()); $this->assertSame('', (string)$response->getBody()); } public function testBuildJson(): void { $response = $this->subject ->withJsonHeader() ->withBody(['a' => 'b']) ->format(new Response()); $this->assertSame(200, $response->getStatusCode()); $this->assertSame(['Content-Type' => ['application/json']], $response->getHeaders()); $this->assertSame('{"a":"b"}', (string)$response->getBody()); } public function testBuildCustomized(): void { $body = ''; $statusCode = 400; $contentType = 'text/html'; $response = $this->subject ->withStatusCode($statusCode) ->addHeader('Content-Type', $contentType) ->withBody($body) ->format(new Response()); $this->assertSame($statusCode, $response->getStatusCode()); $this->assertSame(['Content-Type' => [$contentType]], $response->getHeaders()); $this->assertSame($body, (string)$response->getBody()); } public function testBuildWithExpiration(): void { $response = $this->subject ->withExpiration((new DateTime('2010-10-10 10:10:10'))->getTimestamp()) ->format(new Response()); $this->assertSame('Sun, 10 Oct 2010 10:10:10 GMT', current($response->getHeader('Expires'))); } public function testBuildWithJsonSerializable(): void { $payload = new SuccessJsonResponse(['data']); $response = $this->subject ->withBody($payload) ->format(new Response()); $this->assertSame($payload->jsonSerialize(), json_decode((string)$response->getBody(), true)); } }