|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace TraderInteractive\Api; |
| 4 | + |
| 5 | +use Fig\Http\Message\StatusCodeInterface as StatusCodes; |
| 6 | +use GuzzleHttp\Psr7; |
| 7 | +use PHPUnit\Framework\TestCase; |
| 8 | +use Psr\Http\Message\ResponseInterface; |
| 9 | + |
| 10 | +/** |
| 11 | + * @coversDefaultClass \TraderInteractive\Api\ResponseSerializer |
| 12 | + * @covers ::<private> |
| 13 | + */ |
| 14 | +final class ResponseSerializerTest extends TestCase |
| 15 | +{ |
| 16 | + /** |
| 17 | + * @param ResponseInterface $response The PSR response to serialize. |
| 18 | + * @param array $expectedData The expected serialized data. |
| 19 | + * @test |
| 20 | + * @covers ::serialize |
| 21 | + * |
| 22 | + * @dataProvider provideValidSerializationData |
| 23 | + */ |
| 24 | + public function serializeRepsonse(ResponseInterface $response, array $expectedData) |
| 25 | + { |
| 26 | + $serializer = new ResponseSerializer(); |
| 27 | + $actualData = $serializer->serialize($response); |
| 28 | + $this->assertSame($expectedData, $actualData); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * @param ResponseInterface $expectedResponse The expected PSR response after unserializing. |
| 33 | + * @param array $serailizedData The data to unserialize. |
| 34 | + * |
| 35 | + * @test |
| 36 | + * @covers ::unserialize |
| 37 | + * @dataProvider provideValidSerializationData |
| 38 | + */ |
| 39 | + public function unserializeResponse(ResponseInterface $expectedResponse, array $serailizedData) |
| 40 | + { |
| 41 | + $serializer = new ResponseSerializer(); |
| 42 | + $actualResponse = $serializer->unserialize($serailizedData); |
| 43 | + $this->assertSame($expectedResponse->getStatusCode(), $actualResponse->getStatusCode()); |
| 44 | + $this->assertSame($expectedResponse->getHeaders(), $actualResponse->getHeaders()); |
| 45 | + $this->assertSame($expectedResponse->getBody()->getContents(), $actualResponse->getBody()->getContents()); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * @return array |
| 50 | + */ |
| 51 | + public function provideValidSerializationData() : array |
| 52 | + { |
| 53 | + return [ |
| 54 | + [ |
| 55 | + 'response' => new Psr7\Response( |
| 56 | + StatusCodes::STATUS_OK, |
| 57 | + ['Content-Type' => ['application/json']], |
| 58 | + Psr7\stream_for('{"success": true}') |
| 59 | + ), |
| 60 | + 'data' => [ |
| 61 | + 'statusCode' => StatusCodes::STATUS_OK, |
| 62 | + 'headers' => ['Content-Type' => ['application/json']], |
| 63 | + 'body' => '{"success": true}', |
| 64 | + ], |
| 65 | + ], |
| 66 | + [ |
| 67 | + 'response' => new Psr7\Response( |
| 68 | + StatusCodes::STATUS_NO_CONTENT, |
| 69 | + ['X-PHPUnit' => ['testing']] |
| 70 | + ), |
| 71 | + 'data' => [ |
| 72 | + 'statusCode' => StatusCodes::STATUS_NO_CONTENT, |
| 73 | + 'headers' => ['X-PHPUnit' => ['testing']], |
| 74 | + 'body' => '', |
| 75 | + ], |
| 76 | + ], |
| 77 | + ]; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * @test |
| 82 | + * @covers ::serialize |
| 83 | + * @expectedException \TraderInteractive\Api\SerializerException |
| 84 | + * @expectedExceptionMessage Cannot serialize value of type 'array' |
| 85 | + */ |
| 86 | + public function serializeAcceptsOnlyResponses() |
| 87 | + { |
| 88 | + $serializer = new ResponseSerializer(); |
| 89 | + $serializer->serialize(['foo']); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * @param mixed $serializedData The data to be unserialized |
| 94 | + * @param string $expectedExceptionMessage The expected message for the SerializerException |
| 95 | + * |
| 96 | + * @test |
| 97 | + * @covers ::unserialize |
| 98 | + * @dataProvider provideInvalidSerializedData |
| 99 | + */ |
| 100 | + public function unserializeEnforcesKeyRequirements($serializedData, string $expectedExceptionMessage) |
| 101 | + { |
| 102 | + $this->expectException(SerializerException::class); |
| 103 | + $this->expectExceptionMessage($expectedExceptionMessage); |
| 104 | + $serializer = new ResponseSerializer(); |
| 105 | + $serializer->unserialize($serializedData); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * @return array |
| 110 | + */ |
| 111 | + public function provideInvalidSerializedData() : array |
| 112 | + { |
| 113 | + return [ |
| 114 | + 'missing statusCode' => [ |
| 115 | + 'data' => [ |
| 116 | + 'headers' => ['X-PHPUnit' => ['testing']], |
| 117 | + 'body' => '', |
| 118 | + ], |
| 119 | + 'message' => "Data is missing 'statusCode' value", |
| 120 | + ], |
| 121 | + 'missing headers' => [ |
| 122 | + 'data' => [ |
| 123 | + 'statusCode' => StatusCodes::STATUS_NO_CONTENT, |
| 124 | + 'body' => '', |
| 125 | + ], |
| 126 | + 'message' => "Data is missing 'headers' value", |
| 127 | + ], |
| 128 | + 'missing body' => [ |
| 129 | + 'data' => [ |
| 130 | + 'statusCode' => StatusCodes::STATUS_NO_CONTENT, |
| 131 | + 'headers' => ['X-PHPUnit' => ['testing']], |
| 132 | + ], |
| 133 | + 'message' => "Data is missing 'body' value", |
| 134 | + ], |
| 135 | + 'data is not an array' => [ |
| 136 | + 'data' => '{"foo": "bar"}', |
| 137 | + 'message' => 'Serialized data is not an array', |
| 138 | + ], |
| 139 | + ]; |
| 140 | + } |
| 141 | +} |
0 commit comments