• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

api-platform / core / 6145149255

11 Sep 2023 10:27AM UTC coverage: 37.185% (+0.002%) from 37.183%
6145149255

push

github

web-flow
feat: deprecate not setting formats manually (#5808)

introduces documentation formats

Co-authored-by: Sarahshr <saraah.sahraoui@gmail.com>

47 of 47 new or added lines in 8 files covered. (100.0%)

10063 of 27062 relevant lines covered (37.18%)

19.79 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/OpenApi/Tests/Serializer/ApiGatewayNormalizerTest.php
1
<?php
2

3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <dunglas@gmail.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
declare(strict_types=1);
13

14
namespace ApiPlatform\OpenApi\Tests\Serializer;
15

16
use ApiPlatform\OpenApi\Model\Info;
17
use ApiPlatform\OpenApi\Model\Paths;
18
use ApiPlatform\OpenApi\OpenApi;
19
use ApiPlatform\OpenApi\Serializer\ApiGatewayNormalizer;
20
use ApiPlatform\OpenApi\Serializer\OpenApiNormalizer;
21
use PHPUnit\Framework\TestCase;
22
use Prophecy\Argument;
23
use Prophecy\PhpUnit\ProphecyTrait;
24
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
25
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
26

27
final class ApiGatewayNormalizerTest extends TestCase
28
{
29
    use ProphecyTrait;
30

31
    /**
32
     * @group legacy
33
     */
34
    public function testSupportsNormalization(): void
35
    {
36
        $normalizerProphecy = $this->prophesize(NormalizerInterface::class);
×
37
        $normalizerProphecy->willImplement(CacheableSupportsMethodInterface::class);
×
38
        $normalizerProphecy->supportsNormalization(OpenApiNormalizer::FORMAT, OpenApi::class)->willReturn(true);
×
39
        $normalizerProphecy->hasCacheableSupportsMethod()->willReturn(true);
×
40

41
        $normalizer = new ApiGatewayNormalizer($normalizerProphecy->reveal());
×
42

43
        $this->assertTrue($normalizer->supportsNormalization(OpenApiNormalizer::FORMAT, OpenApi::class));
×
44
        $this->assertTrue($normalizer->hasCacheableSupportsMethod());
×
45
    }
46

47
    public function testNormalize(): void
48
    {
49
        $swaggerDocument = [
×
50
            'paths' => new \ArrayObject([
×
51
                '/dummies' => [
×
52
                    'post' => new \ArrayObject([
×
53
                        'parameters' => [
×
54
                            [
×
55
                                'name' => 'dummy',
×
56
                                'in' => 'body',
×
57
                                'schema' => [
×
58
                                    '$ref' => '#/definitions/Dummy',
×
59
                                ],
×
60
                            ],
×
61
                        ],
×
62
                        'responses' => [
×
63
                            201 => [
×
64
                                'schema' => [
×
65
                                    '$ref' => '#/definitions/Dummy-list_details',
×
66
                                ],
×
67
                            ],
×
68
                        ],
×
69
                    ]),
×
70
                    'get' => new \ArrayObject([
×
71
                        'parameters' => [
×
72
                            [
×
73
                                'name' => 'relatedDummy',
×
74
                                'in' => 'query',
×
75
                                'required' => false,
×
76
                                'type' => 'string',
×
77
                            ],
×
78
                            [
×
79
                                'name' => 'relatedDummy[]',
×
80
                                'in' => 'query',
×
81
                                'required' => false,
×
82
                                'type' => 'string',
×
83
                            ],
×
84
                        ],
×
85
                        'responses' => [
×
86
                            200 => [
×
87
                                'schema' => [
×
88
                                    'type' => 'array',
×
89
                                    'items' => [
×
90
                                        '$ref' => '#/definitions/Dummy-list',
×
91
                                    ],
×
92
                                ],
×
93
                            ],
×
94
                        ],
×
95
                    ]),
×
96
                ],
×
97
                '/dummies/{id}' => [
×
98
                    'get' => new \ArrayObject([
×
99
                        'parameters' => [
×
100
                            [
×
101
                                'name' => 'id',
×
102
                                'in' => 'path',
×
103
                                'required' => true,
×
104
                                'type' => 'string',
×
105
                            ],
×
106
                        ],
×
107
                        'responses' => [
×
108
                            200 => [
×
109
                                'schema' => [
×
110
                                    '$ref' => '#/definitions/Dummy-list_details',
×
111
                                ],
×
112
                            ],
×
113
                        ],
×
114
                    ]),
×
115
                ],
×
116
                '/dummies/{id}/what' => [
×
117
                    'post' => new \ArrayObject([
×
118
                        'parameters' => [
×
119
                            [
×
120
                                'name' => 'dummy',
×
121
                                'in' => 'body',
×
122
                                'schema' => [
×
123
                                    '$ref' => '#/definitions/Dummy:InputDto',
×
124
                                ],
×
125
                            ],
×
126
                        ],
×
127
                        'responses' => [
×
128
                            200 => [
×
129
                                'schema' => [
×
130
                                    '$ref' => '#/definitions/Dummy:OutputDto',
×
131
                                ],
×
132
                            ],
×
133
                        ],
×
134
                    ]),
×
135
                ],
×
136
            ]),
×
137
            'components' => ['schemas' => [
×
138
                'Dummy' => new \ArrayObject([
×
139
                    'properties' => [
×
140
                        'id' => [
×
141
                            'readOnly' => true,
×
142
                            'type' => 'integer',
×
143
                        ],
×
144
                        'description' => [
×
145
                            'type' => 'string',
×
146
                        ],
×
147
                    ],
×
148
                ]),
×
149
                'Dummy-list' => new \ArrayObject([
×
150
                    'properties' => [
×
151
                        'id' => [
×
152
                            'readOnly' => true,
×
153
                            'type' => 'integer',
×
154
                        ],
×
155
                        'description' => [
×
156
                            'type' => 'string',
×
157
                        ],
×
158
                    ],
×
159
                ]),
×
160
                'Dummy-list_details' => new \ArrayObject([
×
161
                    'properties' => [
×
162
                        'id' => [
×
163
                            'readOnly' => true,
×
164
                            'type' => 'integer',
×
165
                        ],
×
166
                        'description' => [
×
167
                            'type' => 'string',
×
168
                        ],
×
169
                        'relatedDummy' => new \ArrayObject([
×
170
                            '$ref' => '#/definitions/RelatedDummy-list_details',
×
171
                        ]),
×
172
                    ],
×
173
                ]),
×
174
                'Dummy:OutputDto' => new \ArrayObject([
×
175
                    'type' => 'object',
×
176
                    'properties' => [
×
177
                        'baz' => new \ArrayObject([
×
178
                            'readOnly' => true,
×
179
                            'type' => 'string',
×
180
                        ]),
×
181
                        'bat' => new \ArrayObject([
×
182
                            'type' => 'integer',
×
183
                        ]),
×
184
                    ],
×
185
                ]),
×
186
                'Dummy:InputDto' => new \ArrayObject([
×
187
                    'type' => 'object',
×
188
                    'properties' => [
×
189
                        'foo' => new \ArrayObject([
×
190
                            'type' => 'string',
×
191
                        ]),
×
192
                        'bar' => new \ArrayObject([
×
193
                            'type' => 'integer',
×
194
                        ]),
×
195
                    ],
×
196
                ]),
×
197
                'RelatedDummy-list_details' => new \ArrayObject([
×
198
                    'type' => 'object',
×
199
                    'properties' => [
×
200
                        'name' => new \ArrayObject([
×
201
                            'type' => 'string',
×
202
                        ]),
×
203
                    ],
×
204
                ]),
×
205
            ]],
×
206
        ];
×
207

208
        $modifiedSwaggerDocument = [
×
209
            'paths' => new \ArrayObject([
×
210
                '/dummies' => [
×
211
                    'post' => new \ArrayObject([
×
212
                        'parameters' => [
×
213
                            [
×
214
                                'name' => 'dummy',
×
215
                                'in' => 'body',
×
216
                                'schema' => [
×
217
                                    '$ref' => '#/definitions/Dummy',
×
218
                                ],
×
219
                            ],
×
220
                        ],
×
221
                        'responses' => [
×
222
                            201 => [
×
223
                                'schema' => [
×
224
                                    '$ref' => '#/definitions/Dummylistdetails',
×
225
                                ],
×
226
                            ],
×
227
                        ],
×
228
                    ]),
×
229
                    'get' => new \ArrayObject([
×
230
                        'parameters' => [
×
231
                            [
×
232
                                'name' => 'relatedDummy',
×
233
                                'in' => 'query',
×
234
                                'required' => false,
×
235
                                'type' => 'string',
×
236
                            ],
×
237
                        ],
×
238
                        'responses' => [
×
239
                            200 => [
×
240
                                'schema' => [
×
241
                                    'type' => 'array',
×
242
                                    'items' => [
×
243
                                        '$ref' => '#/definitions/Dummylist',
×
244
                                    ],
×
245
                                ],
×
246
                            ],
×
247
                        ],
×
248
                    ]),
×
249
                ],
×
250
                '/dummies/{id}' => [
×
251
                    'get' => new \ArrayObject([
×
252
                        'parameters' => [
×
253
                            [
×
254
                                'name' => 'id',
×
255
                                'in' => 'path',
×
256
                                'required' => true,
×
257
                                'type' => 'string',
×
258
                            ],
×
259
                        ],
×
260
                        'responses' => [
×
261
                            200 => [
×
262
                                'schema' => [
×
263
                                    '$ref' => '#/definitions/Dummylistdetails',
×
264
                                ],
×
265
                            ],
×
266
                        ],
×
267
                    ]),
×
268
                ],
×
269
                '/dummies/{id}/what' => [
×
270
                    'post' => new \ArrayObject([
×
271
                        'parameters' => [
×
272
                            [
×
273
                                'name' => 'dummy',
×
274
                                'in' => 'body',
×
275
                                'schema' => [
×
276
                                    '$ref' => '#/definitions/DummyInputDto',
×
277
                                ],
×
278
                            ],
×
279
                        ],
×
280
                        'responses' => [
×
281
                            200 => [
×
282
                                'schema' => [
×
283
                                    '$ref' => '#/definitions/DummyOutputDto',
×
284
                                ],
×
285
                            ],
×
286
                        ],
×
287
                    ]),
×
288
                ],
×
289
            ]),
×
290
            'components' => ['schemas' => [
×
291
                'Dummy' => new \ArrayObject([
×
292
                    'properties' => [
×
293
                        'id' => [
×
294
                            'type' => 'integer',
×
295
                        ],
×
296
                        'description' => [
×
297
                            'type' => 'string',
×
298
                        ],
×
299
                    ],
×
300
                ]),
×
301
                'Dummylist' => new \ArrayObject([
×
302
                    'properties' => [
×
303
                        'id' => [
×
304
                            'type' => 'integer',
×
305
                        ],
×
306
                        'description' => [
×
307
                            'type' => 'string',
×
308
                        ],
×
309
                    ],
×
310
                ]),
×
311
                'Dummylistdetails' => new \ArrayObject([
×
312
                    'properties' => [
×
313
                        'id' => [
×
314
                            'type' => 'integer',
×
315
                        ],
×
316
                        'description' => [
×
317
                            'type' => 'string',
×
318
                        ],
×
319
                        'relatedDummy' => new \ArrayObject([
×
320
                            '$ref' => '#/definitions/RelatedDummylistdetails',
×
321
                        ]),
×
322
                    ],
×
323
                ]),
×
324
                'DummyOutputDto' => new \ArrayObject([
×
325
                    'type' => 'object',
×
326
                    'properties' => [
×
327
                        'baz' => new \ArrayObject([
×
328
                            'type' => 'string',
×
329
                        ]),
×
330
                        'bat' => new \ArrayObject([
×
331
                            'type' => 'integer',
×
332
                        ]),
×
333
                    ],
×
334
                ]),
×
335
                'DummyInputDto' => new \ArrayObject([
×
336
                    'type' => 'object',
×
337
                    'properties' => [
×
338
                        'foo' => new \ArrayObject([
×
339
                            'type' => 'string',
×
340
                        ]),
×
341
                        'bar' => new \ArrayObject([
×
342
                            'type' => 'integer',
×
343
                        ]),
×
344
                    ],
×
345
                ]),
×
346
                'RelatedDummylistdetails' => new \ArrayObject([
×
347
                    'type' => 'object',
×
348
                    'properties' => [
×
349
                        'name' => new \ArrayObject([
×
350
                            'type' => 'string',
×
351
                        ]),
×
352
                    ],
×
353
                ]),
×
354
            ]],
×
355
            'basePath' => '/',
×
356
        ];
×
357

358
        $documentation = $this->getOpenApi();
×
359
        $normalizerProphecy = $this->prophesize(NormalizerInterface::class);
×
360
        $normalizerProphecy->normalize($documentation, OpenApiNormalizer::FORMAT, [
×
361
            ApiGatewayNormalizer::API_GATEWAY => true,
×
362
        ])->willReturn($swaggerDocument);
×
363

364
        $normalizer = new ApiGatewayNormalizer($normalizerProphecy->reveal());
×
365

366
        $this->assertEquals($modifiedSwaggerDocument, $normalizer->normalize($documentation, OpenApiNormalizer::FORMAT, [
×
367
            ApiGatewayNormalizer::API_GATEWAY => true,
×
368
        ]));
×
369
    }
370

371
    public function testNormalizeNotInApiGatewayContext(): void
372
    {
373
        $documentation = $this->getOpenApi();
×
374

375
        $swaggerDocument = [
×
376
            'paths' => new \ArrayObject([
×
377
                '/dummies' => [
×
378
                    'post' => new \ArrayObject([
×
379
                        'parameters' => [
×
380
                            [
×
381
                                'name' => 'dummy',
×
382
                                'in' => 'body',
×
383
                                'schema' => [
×
384
                                    '$ref' => '#/definitions/Dummy',
×
385
                                ],
×
386
                            ],
×
387
                        ],
×
388
                        'responses' => [
×
389
                            201 => [
×
390
                                'schema' => [
×
391
                                    '$ref' => '#/definitions/Dummy-list_details',
×
392
                                ],
×
393
                            ],
×
394
                        ],
×
395
                    ]),
×
396
                    'get' => new \ArrayObject([
×
397
                        'parameters' => [
×
398
                            [
×
399
                                'name' => 'relatedDummy',
×
400
                                'in' => 'query',
×
401
                                'required' => false,
×
402
                                'type' => 'string',
×
403
                            ],
×
404
                            [
×
405
                                'name' => 'relatedDummy[]',
×
406
                                'in' => 'query',
×
407
                                'required' => false,
×
408
                                'type' => 'string',
×
409
                            ],
×
410
                        ],
×
411
                        'responses' => [
×
412
                            200 => [
×
413
                                'schema' => [
×
414
                                    'type' => 'array',
×
415
                                    'items' => [
×
416
                                        '$ref' => '#/definitions/Dummy-list',
×
417
                                    ],
×
418
                                ],
×
419
                            ],
×
420
                        ],
×
421
                    ]),
×
422
                ],
×
423
                '/dummies/{id}' => [
×
424
                    'get' => new \ArrayObject([
×
425
                        'parameters' => [
×
426
                            [
×
427
                                'name' => 'id',
×
428
                                'in' => 'path',
×
429
                                'required' => true,
×
430
                                'type' => 'string',
×
431
                            ],
×
432
                        ],
×
433
                        'responses' => [
×
434
                            200 => [
×
435
                                'schema' => [
×
436
                                    '$ref' => '#/definitions/Dummy-list_details',
×
437
                                ],
×
438
                            ],
×
439
                        ],
×
440
                    ]),
×
441
                ],
×
442
                '/dummies/{id}/what' => [
×
443
                    'post' => new \ArrayObject([
×
444
                        'parameters' => [
×
445
                            [
×
446
                                'name' => 'dummy',
×
447
                                'in' => 'body',
×
448
                                'schema' => [
×
449
                                    '$ref' => '#/definitions/Dummy:InputDto',
×
450
                                ],
×
451
                            ],
×
452
                        ],
×
453
                        'responses' => [
×
454
                            200 => [
×
455
                                'schema' => [
×
456
                                    '$ref' => '#/definitions/Dummy:OutputDto',
×
457
                                ],
×
458
                            ],
×
459
                        ],
×
460
                    ]),
×
461
                ],
×
462
            ]),
×
463
            'components' => ['schemas' => new \ArrayObject([
×
464
                'Dummy' => new \ArrayObject([
×
465
                    'properties' => [
×
466
                        'id' => [
×
467
                            'readOnly' => true,
×
468
                            'type' => 'integer',
×
469
                        ],
×
470
                        'description' => [
×
471
                            'type' => 'string',
×
472
                        ],
×
473
                    ],
×
474
                ]),
×
475
                'Dummy-list' => new \ArrayObject([
×
476
                    'properties' => [
×
477
                        'id' => [
×
478
                            'readOnly' => true,
×
479
                            'type' => 'integer',
×
480
                        ],
×
481
                        'description' => [
×
482
                            'type' => 'string',
×
483
                        ],
×
484
                    ],
×
485
                ]),
×
486
                'Dummy-list_details' => new \ArrayObject([
×
487
                    'properties' => [
×
488
                        'id' => [
×
489
                            'readOnly' => true,
×
490
                            'type' => 'integer',
×
491
                        ],
×
492
                        'description' => [
×
493
                            'type' => 'string',
×
494
                        ],
×
495
                        'relatedDummy' => new \ArrayObject([
×
496
                            '$ref' => '#/definitions/RelatedDummy-list_details',
×
497
                        ]),
×
498
                    ],
×
499
                ]),
×
500
                'Dummy:OutputDto' => new \ArrayObject([
×
501
                    'type' => 'object',
×
502
                    'properties' => [
×
503
                        'baz' => new \ArrayObject([
×
504
                            'readOnly' => true,
×
505
                            'type' => 'string',
×
506
                        ]),
×
507
                        'bat' => new \ArrayObject([
×
508
                            'type' => 'integer',
×
509
                        ]),
×
510
                    ],
×
511
                ]),
×
512
                'Dummy:InputDto' => new \ArrayObject([
×
513
                    'type' => 'object',
×
514
                    'properties' => [
×
515
                        'foo' => new \ArrayObject([
×
516
                            'type' => 'string',
×
517
                        ]),
×
518
                        'bar' => new \ArrayObject([
×
519
                            'type' => 'integer',
×
520
                        ]),
×
521
                    ],
×
522
                ]),
×
523
                'RelatedDummy-list_details' => new \ArrayObject([
×
524
                    'type' => 'object',
×
525
                    'properties' => [
×
526
                        'name' => new \ArrayObject([
×
527
                            'type' => 'string',
×
528
                        ]),
×
529
                    ],
×
530
                ]),
×
531
            ])],
×
532
        ];
×
533

534
        $normalizerProphecy = $this->prophesize(NormalizerInterface::class);
×
535
        $normalizerProphecy->normalize($documentation, OpenApiNormalizer::FORMAT, Argument::type('array'))->willReturn($swaggerDocument);
×
536

537
        $normalizer = new ApiGatewayNormalizer($normalizerProphecy->reveal());
×
538

539
        $this->assertEquals($swaggerDocument, $normalizer->normalize($documentation, OpenApiNormalizer::FORMAT));
×
540
    }
541

542
    private function getOpenApi(): OpenApi
543
    {
544
        return new OpenApi(new Info('test', '0'), [], new Paths());
×
545
    }
546
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc