Passed
Pull Request — master (#13)
by
unknown
02:44
created

ResourceDenormalizerTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 212
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 119
dl 0
loc 212
rs 10
c 0
b 0
f 0
wmc 7

7 Methods

Rating   Name   Duplication   Size   Complexity  
A itThrowAnExceptionWhenSchemaTypeCannotBeExtracted() 0 26 1
A itShouldSupportResourceType() 0 6 1
A itShouldProvideAResourceOfTypeCollection() 0 30 1
A itShouldThrowAnExceptionWhenNoResponseSchemaIsDefinedInTheResponseDefinition() 0 34 1
A itCanExtractTypeFromAnAllOfSchema() 0 33 1
A itShouldProvideAResourceOfTypeCollectionWithPagination() 0 34 1
A itShouldProvideAResourceOfTypeItem() 0 26 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ElevenLabs\Api\Service\Tests\Denormalizer;
6
7
use ElevenLabs\Api\Definition\ResponseDefinition;
8
use ElevenLabs\Api\Service\Denormalizer\ResourceDenormalizer;
9
use ElevenLabs\Api\Service\Pagination\Pagination;
10
use ElevenLabs\Api\Service\Pagination\Provider\PaginationProviderInterface;
11
use ElevenLabs\Api\Service\Resource\Collection;
12
use ElevenLabs\Api\Service\Resource\Item;
13
use ElevenLabs\Api\Service\Resource\ResourceInterface;
14
use PHPUnit\Framework\TestCase;
15
use Psr\Http\Message\RequestInterface;
16
use Psr\Http\Message\ResponseInterface;
17
use Psr\Http\Message\UriInterface;
18
19
/**
20
 * Class ResourceDenormalizerTest.
21
 */
22
class ResourceDenormalizerTest extends TestCase
23
{
24
    /** @test */
25
    public function itShouldSupportResourceType()
26
    {
27
        $paginationProvider = $this->prophesize(PaginationProviderInterface::class);
28
        $denormalizer = new ResourceDenormalizer($paginationProvider->reveal());
29
30
        $this->assertTrue($denormalizer->supportsDenormalization([], ResourceInterface::class));
31
    }
32
33
    /** @test */
34
    public function itShouldProvideAResourceOfTypeItem()
35
    {
36
        $response = $this->prophesize(ResponseInterface::class);
37
38
        $request = $this->prophesize(RequestInterface::class);
39
40
        $responseDefinition = $this->prophesize(ResponseDefinition::class);
41
        $responseDefinition->hasBodySchema()->willReturn(true);
42
        $responseDefinition->getBodySchema()->willReturn((object) ['type' => 'object']);
43
44
        $paginationProvider = $this->prophesize(PaginationProviderInterface::class);
45
        $paginationProvider->supportPagination()->shouldNotBeCalled();
46
47
        $denormalizer = new ResourceDenormalizer($paginationProvider->reveal());
48
        $resource = $denormalizer->denormalize(
49
            ['foo' => 'bar'],
50
            ResourceInterface::class,
51
            null,
52
            [
53
                'response' => $response->reveal(),
54
                'responseDefinition' => $responseDefinition->reveal(),
55
                'request' => $request->reveal(),
56
            ]
57
        );
58
59
        $this->assertInstanceOf(Item::class, $resource);
60
    }
61
62
    /** @test */
63
    public function itShouldThrowAnExceptionWhenNoResponseSchemaIsDefinedInTheResponseDefinition()
64
    {
65
        $this->expectException(\LogicException::class);
66
        $this->expectExceptionMessage(
67
            'Cannot transform the response into a resource. '.
68
            'You need to provide a schema for response 200 in GET /foo'
69
        );
70
71
        $requestPath = '/foo';
72
73
        $uri = $this->prophesize(UriInterface::class);
74
        $uri->getPath()->willReturn($requestPath);
75
76
        $response = $this->prophesize(ResponseInterface::class);
77
        $response->getStatusCode()->willReturn(200);
78
79
        $request = $this->prophesize(RequestInterface::class);
80
        $request->getUri()->willreturn($uri);
81
        $request->getMethod()->willReturn('GET');
82
83
        $responseDefinition = $this->prophesize(ResponseDefinition::class);
84
        $responseDefinition->hasBodySchema()->willReturn(false);
85
86
        $paginationProvider = $this->prophesize(PaginationProviderInterface::class);
87
88
        $denormalizer = new ResourceDenormalizer($paginationProvider->reveal());
89
        $denormalizer->denormalize(
90
            [],
91
            ResourceInterface::class,
92
            null,
93
            [
94
                'response' => $response->reveal(),
95
                'responseDefinition' => $responseDefinition->reveal(),
96
                'request' => $request->reveal(),
97
            ]
98
        );
99
    }
100
101
    /** @test */
102
    public function itShouldProvideAResourceOfTypeCollection()
103
    {
104
        $data = [
105
            ['foo' => 'bar'],
106
        ];
107
108
        $response = $this->prophesize(ResponseInterface::class);
109
110
        $request = $this->prophesize(RequestInterface::class);
111
112
        $responseDefinition = $this->prophesize(ResponseDefinition::class);
113
        $responseDefinition->hasBodySchema()->willReturn(true);
114
        $responseDefinition->getBodySchema()->willReturn((object) ['type' => 'array']);
115
116
        $paginationProvider = $this->prophesize(PaginationProviderInterface::class);
117
        $paginationProvider->supportPagination($data, $response, $responseDefinition)->willReturn(false);
118
119
        $denormalizer = new ResourceDenormalizer($paginationProvider->reveal());
120
        $resource = $denormalizer->denormalize(
121
            $data,
122
            ResourceInterface::class,
123
            null,
124
            [
125
                'response' => $response->reveal(),
126
                'responseDefinition' => $responseDefinition->reveal(),
127
                'request' => $request->reveal(),
128
            ]
129
        );
130
131
        $this->assertInstanceOf(Collection::class, $resource);
132
    }
133
134
    /** @test */
135
    public function itShouldProvideAResourceOfTypeCollectionWithPagination()
136
    {
137
        $data = [
138
            ['foo' => 'bar'],
139
        ];
140
141
        $response = $this->prophesize(ResponseInterface::class);
142
143
        $request = $this->prophesize(RequestInterface::class);
144
145
        $responseDefinition = $this->prophesize(ResponseDefinition::class);
146
        $responseDefinition->hasBodySchema()->willReturn(true);
147
        $responseDefinition->getBodySchema()->willReturn((object) ['type' => 'array']);
148
149
        $pagination = $this->prophesize(Pagination::class);
150
151
        $paginationProvider = $this->prophesize(PaginationProviderInterface::class);
152
        $paginationProvider->supportPagination($data, $response, $responseDefinition)->willReturn(true);
153
        $paginationProvider->getPagination($data, $response, $responseDefinition)->willReturn($pagination);
154
155
        $denormalizer = new ResourceDenormalizer($paginationProvider->reveal());
156
        $resource = $denormalizer->denormalize(
157
            $data,
158
            ResourceInterface::class,
159
            null,
160
            [
161
                'response' => $response->reveal(),
162
                'responseDefinition' => $responseDefinition->reveal(),
163
                'request' => $request->reveal(),
164
            ]
165
        );
166
167
        $this->assertInstanceOf(Collection::class, $resource);
168
        $this->assertSame($pagination->reveal(), $resource->getPagination());
0 ignored issues
show
introduced by
The method getPagination() does not exist on ElevenLabs\Api\Service\Resource\Item. Maybe you want to declare this class abstract? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

168
        $this->assertSame($pagination->reveal(), $resource->/** @scrutinizer ignore-call */ getPagination());
Loading history...
169
    }
170
171
    /** @test */
172
    public function itCanExtractTypeFromAnAllOfSchema()
173
    {
174
        $jsonSchema = (object) [
175
            'allOf' => [
176
                (object) ['type' => 'object'],
177
            ],
178
        ];
179
180
        $response = $this->prophesize(ResponseInterface::class);
181
182
        $request = $this->prophesize(RequestInterface::class);
183
184
        $responseDefinition = $this->prophesize(ResponseDefinition::class);
185
        $responseDefinition->hasBodySchema()->willReturn(true);
186
        $responseDefinition->getBodySchema()->willReturn($jsonSchema);
187
188
        $paginationProvider = $this->prophesize(PaginationProviderInterface::class);
189
        $paginationProvider->supportPagination()->shouldNotBeCalled();
190
191
        $denormalizer = new ResourceDenormalizer($paginationProvider->reveal());
192
        $resource = $denormalizer->denormalize(
193
            ['foo' => 'bar'],
194
            ResourceInterface::class,
195
            null,
196
            [
197
                'response' => $response->reveal(),
198
                'responseDefinition' => $responseDefinition->reveal(),
199
                'request' => $request->reveal(),
200
            ]
201
        );
202
203
        assertThat($resource, isInstanceOf(Item::class));
204
        $this->assertSame(['headers' => null], $resource->getMeta());
205
    }
206
207
    /** @test */
208
    public function itThrowAnExceptionWhenSchemaTypeCannotBeExtracted()
209
    {
210
        $this->expectException(\RuntimeException::class);
211
        $this->expectExceptionMessage('Cannot extract type from schema');
212
213
        $jsonSchema = (object) ['invalid' => 'invalid'];
214
215
        $response = $this->prophesize(ResponseInterface::class);
216
        $request = $this->prophesize(RequestInterface::class);
217
218
        $responseDefinition = $this->prophesize(ResponseDefinition::class);
219
        $responseDefinition->hasBodySchema()->willReturn(true);
220
        $responseDefinition->getBodySchema()->willReturn($jsonSchema);
221
222
        $paginationProvider = $this->prophesize(PaginationProviderInterface::class);
223
        $paginationProvider->supportPagination()->shouldNotBeCalled();
224
225
        $denormalizer = new ResourceDenormalizer($paginationProvider->reveal());
226
        $denormalizer->denormalize(
227
            ['foo' => 'bar'],
228
            ResourceInterface::class,
229
            null,
230
            [
231
                'response' => $response->reveal(),
232
                'responseDefinition' => $responseDefinition->reveal(),
233
                'request' => $request->reveal(),
234
            ]
235
        );
236
    }
237
}
238