eleven-labs /
api-service
| 1 | <?php |
||
| 2 | namespace ElevenLabs\Api\Service\Denormalizer; |
||
| 3 | |||
| 4 | use ElevenLabs\Api\Definition\ResponseDefinition; |
||
| 5 | use ElevenLabs\Api\Service\Pagination\Pagination; |
||
| 6 | use ElevenLabs\Api\Service\Pagination\PaginationProvider; |
||
| 7 | use ElevenLabs\Api\Service\Resource\Collection; |
||
| 8 | use ElevenLabs\Api\Service\Resource\Item; |
||
| 9 | use ElevenLabs\Api\Service\Resource\Resource; |
||
| 10 | use PHPUnit\Framework\TestCase; |
||
| 11 | use Psr\Http\Message\RequestInterface; |
||
| 12 | use Psr\Http\Message\ResponseInterface; |
||
| 13 | use Psr\Http\Message\UriInterface; |
||
| 14 | use Prophecy\Prophecy\ObjectProphecy; |
||
| 15 | |||
| 16 | class ResourceDenormalizerTest extends TestCase |
||
| 17 | { |
||
| 18 | /** @test */ |
||
| 19 | public function itShouldSupportResourceType() |
||
| 20 | { |
||
| 21 | $paginationProvider = $this->prophesize(PaginationProvider::class); |
||
| 22 | $denormalizer = new ResourceDenormalizer($paginationProvider->reveal()); |
||
| 23 | |||
| 24 | assertThat( |
||
| 25 | $denormalizer->supportsDenormalization([], Resource::class), |
||
| 26 | isTrue() |
||
| 27 | ); |
||
| 28 | } |
||
| 29 | /** @test */ |
||
| 30 | public function itShouldProvideAResourceOfTypeItem() |
||
| 31 | { |
||
| 32 | $response = $this->prophesize(ResponseInterface::class); |
||
| 33 | |||
| 34 | $request = $this->prophesize(RequestInterface::class); |
||
| 35 | |||
| 36 | $responseDefinition = $this->prophesize(ResponseDefinition::class); |
||
| 37 | $responseDefinition->hasBodySchema()->willReturn(true); |
||
| 38 | $responseDefinition->getBodySchema()->willReturn((object) ['type' => 'object']); |
||
| 39 | |||
| 40 | $paginationProvider = $this->prophesize(PaginationProvider::class); |
||
| 41 | $paginationProvider->supportPagination()->shouldNotBeCalled(); |
||
| 42 | |||
| 43 | $denormalizer = new ResourceDenormalizer($paginationProvider->reveal()); |
||
| 44 | $resource = $denormalizer->denormalize( |
||
| 45 | ['foo' => 'bar'], |
||
| 46 | Resource::class, |
||
| 47 | null, |
||
| 48 | [ |
||
| 49 | 'response' => $response->reveal(), |
||
| 50 | 'responseDefinition' => $responseDefinition->reveal(), |
||
| 51 | 'request' => $request->reveal() |
||
| 52 | ] |
||
| 53 | ); |
||
| 54 | |||
| 55 | assertThat($resource, isInstanceOf(Item::class)); |
||
| 56 | } |
||
| 57 | |||
| 58 | /** @test */ |
||
| 59 | public function itShouldThrowAnExceptionWhenNoResponseSchemaIsDefinedInTheResponseDefinition() |
||
| 60 | { |
||
| 61 | $this->expectException(\LogicException::class); |
||
| 62 | $this->expectExceptionMessage( |
||
| 63 | 'Cannot transform the response into a resource. '. |
||
| 64 | 'You need to provide a schema for response 200 in GET /foo' |
||
| 65 | ); |
||
| 66 | |||
| 67 | $requestPath = '/foo'; |
||
| 68 | |||
| 69 | $uri = $this->prophesize(UriInterface::class); |
||
| 70 | $uri->getPath()->willReturn($requestPath); |
||
| 71 | |||
| 72 | $response = $this->prophesize(ResponseInterface::class); |
||
| 73 | $response->getStatusCode()->willReturn(200); |
||
| 74 | |||
| 75 | $request = $this->prophesize(RequestInterface::class); |
||
| 76 | $request->getUri()->willreturn($uri); |
||
| 77 | $request->getMethod()->willReturn('GET'); |
||
| 78 | |||
| 79 | $responseDefinition = $this->prophesize(ResponseDefinition::class); |
||
| 80 | $responseDefinition->hasBodySchema()->willReturn(false); |
||
| 81 | |||
| 82 | $paginationProvider = $this->prophesize(PaginationProvider::class); |
||
| 83 | |||
| 84 | $denormalizer = new ResourceDenormalizer($paginationProvider->reveal()); |
||
| 85 | $denormalizer->denormalize( |
||
| 86 | [], |
||
| 87 | Resource::class, |
||
| 88 | null, |
||
| 89 | [ |
||
| 90 | 'response' => $response->reveal(), |
||
| 91 | 'responseDefinition' => $responseDefinition->reveal(), |
||
| 92 | 'request' => $request->reveal() |
||
| 93 | ] |
||
| 94 | ); |
||
| 95 | } |
||
| 96 | |||
| 97 | /** @test */ |
||
| 98 | public function itShouldProvideAResourceOfTypeCollection() |
||
| 99 | { |
||
| 100 | $data = [ |
||
| 101 | ['foo' => 'bar'] |
||
| 102 | ]; |
||
| 103 | |||
| 104 | $response = $this->prophesize(ResponseInterface::class); |
||
| 105 | |||
| 106 | $request = $this->prophesize(RequestInterface::class); |
||
| 107 | |||
| 108 | $responseDefinition = $this->prophesize(ResponseDefinition::class); |
||
| 109 | $responseDefinition->hasBodySchema()->willReturn(true); |
||
| 110 | $responseDefinition->getBodySchema()->willReturn((object) ['type' => 'array']); |
||
| 111 | |||
| 112 | $paginationProvider = $this->prophesize(PaginationProvider::class); |
||
| 113 | $paginationProvider->supportPagination($data, $response, $responseDefinition)->willReturn(false); |
||
| 114 | |||
| 115 | $denormalizer = new ResourceDenormalizer($paginationProvider->reveal()); |
||
| 116 | $resource = $denormalizer->denormalize( |
||
| 117 | $data, |
||
| 118 | Resource::class, |
||
| 119 | null, |
||
| 120 | [ |
||
| 121 | 'response' => $response->reveal(), |
||
| 122 | 'responseDefinition' => $responseDefinition->reveal(), |
||
| 123 | 'request' => $request->reveal() |
||
| 124 | ] |
||
| 125 | ); |
||
| 126 | |||
| 127 | assertThat($resource, isInstanceOf(Collection::class)); |
||
| 128 | } |
||
| 129 | |||
| 130 | /** @test */ |
||
| 131 | public function itShouldProvideAResourceOfTypeCollectionWithPagination() |
||
| 132 | { |
||
| 133 | $data = [ |
||
| 134 | ['foo' => 'bar'] |
||
| 135 | ]; |
||
| 136 | |||
| 137 | $response = $this->prophesize(ResponseInterface::class); |
||
| 138 | |||
| 139 | $request = $this->prophesize(RequestInterface::class); |
||
| 140 | |||
| 141 | $responseDefinition = $this->prophesize(ResponseDefinition::class); |
||
| 142 | $responseDefinition->hasBodySchema()->willReturn(true); |
||
| 143 | $responseDefinition->getBodySchema()->willReturn((object) ['type' => 'array']); |
||
| 144 | |||
| 145 | $pagination = $this->prophesize(Pagination::class); |
||
| 146 | |||
| 147 | $paginationProvider = $this->prophesize(PaginationProvider::class); |
||
| 148 | $paginationProvider->supportPagination($data, $response, $responseDefinition)->willReturn(true); |
||
| 149 | $paginationProvider->getPagination($data, $response, $responseDefinition)->willReturn($pagination); |
||
| 150 | |||
| 151 | $denormalizer = new ResourceDenormalizer($paginationProvider->reveal()); |
||
| 152 | $resource = $denormalizer->denormalize( |
||
| 153 | $data, |
||
| 154 | Resource::class, |
||
| 155 | null, |
||
| 156 | [ |
||
| 157 | 'response' => $response->reveal(), |
||
| 158 | 'responseDefinition' => $responseDefinition->reveal(), |
||
| 159 | 'request' => $request->reveal() |
||
| 160 | ] |
||
| 161 | ); |
||
| 162 | |||
| 163 | assertThat($resource, isInstanceOf(Collection::class)); |
||
| 164 | assertThat($resource->getPagination(), equalTo($pagination->reveal())); |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 165 | } |
||
| 166 | |||
| 167 | /** @test */ |
||
| 168 | public function itCanExtractTypeFromAnAllOfSchema() |
||
| 169 | { |
||
| 170 | $jsonSchema = (object) [ |
||
| 171 | 'allOf' => [ |
||
| 172 | (object) ['type'=> 'object'], |
||
| 173 | (object) ['type'=> 'object'], |
||
| 174 | ] |
||
| 175 | ]; |
||
| 176 | |||
| 177 | $response = $this->prophesize(ResponseInterface::class); |
||
| 178 | |||
| 179 | $request = $this->prophesize(RequestInterface::class); |
||
| 180 | |||
| 181 | $responseDefinition = $this->prophesize(ResponseDefinition::class); |
||
| 182 | $responseDefinition->hasBodySchema()->willReturn(true); |
||
| 183 | $responseDefinition->getBodySchema()->willReturn($jsonSchema); |
||
| 184 | |||
| 185 | $paginationProvider = $this->prophesize(PaginationProvider::class); |
||
| 186 | $paginationProvider->supportPagination()->shouldNotBeCalled(); |
||
| 187 | |||
| 188 | $denormalizer = new ResourceDenormalizer($paginationProvider->reveal()); |
||
| 189 | $resource = $denormalizer->denormalize( |
||
| 190 | ['foo' => 'bar'], |
||
| 191 | Resource::class, |
||
| 192 | null, |
||
| 193 | [ |
||
| 194 | 'response' => $response->reveal(), |
||
| 195 | 'responseDefinition' => $responseDefinition->reveal(), |
||
| 196 | 'request' => $request->reveal() |
||
| 197 | ] |
||
| 198 | ); |
||
| 199 | |||
| 200 | assertThat($resource, isInstanceOf(Item::class)); |
||
| 201 | } |
||
| 202 | |||
| 203 | /** @test */ |
||
| 204 | public function itThrowAnExceptionWhenSchemaTypeCannotBeExtracted() |
||
| 205 | { |
||
| 206 | $this->expectException(\RuntimeException::class); |
||
| 207 | $this->expectExceptionMessage('Cannot extract type from schema'); |
||
| 208 | |||
| 209 | $jsonSchema = (object) ['invalid' => 'invalid']; |
||
| 210 | |||
| 211 | $response = $this->prophesize(ResponseInterface::class); |
||
| 212 | $request = $this->prophesize(RequestInterface::class); |
||
| 213 | |||
| 214 | $responseDefinition = $this->prophesize(ResponseDefinition::class); |
||
| 215 | $responseDefinition->hasBodySchema()->willReturn(true); |
||
| 216 | $responseDefinition->getBodySchema()->willReturn($jsonSchema); |
||
| 217 | |||
| 218 | $paginationProvider = $this->prophesize(PaginationProvider::class); |
||
| 219 | $paginationProvider->supportPagination()->shouldNotBeCalled(); |
||
| 220 | |||
| 221 | $denormalizer = new ResourceDenormalizer($paginationProvider->reveal()); |
||
| 222 | $resource = $denormalizer->denormalize( |
||
|
0 ignored issues
–
show
|
|||
| 223 | ['foo' => 'bar'], |
||
| 224 | Resource::class, |
||
| 225 | null, |
||
| 226 | [ |
||
| 227 | 'response' => $response->reveal(), |
||
| 228 | 'responseDefinition' => $responseDefinition->reveal(), |
||
| 229 | 'request' => $request->reveal() |
||
| 230 | ] |
||
| 231 | ); |
||
| 232 | } |
||
| 233 | } |
||
| 234 |