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

api-platform / core / 10737347263

06 Sep 2024 11:11AM UTC coverage: 7.645% (-0.01%) from 7.655%
10737347263

push

github

web-flow
Merge pull request #6591 from soyuka/merge-34

Merge 3.4

10 of 526 new or added lines in 17 files covered. (1.9%)

9692 existing lines in 300 files now uncovered.

12523 of 163810 relevant lines covered (7.64%)

22.82 hits per line

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

0.0
/tests/Symfony/Bundle/DependencyInjection/ConfigurationTest.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\Tests\Symfony\Bundle\DependencyInjection;
15

16
use ApiPlatform\Metadata\Exception\InvalidArgumentException;
17
use ApiPlatform\Symfony\Bundle\DependencyInjection\Configuration;
18
use Doctrine\ORM\OptimisticLockException;
19
use PHPUnit\Framework\TestCase;
20
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
21
use Symfony\Component\Config\Definition\ConfigurationInterface;
22
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
23
use Symfony\Component\Config\Definition\Exception\InvalidTypeException;
24
use Symfony\Component\Config\Definition\Processor;
25
use Symfony\Component\HttpFoundation\Response;
26
use Symfony\Component\Serializer\Exception\ExceptionInterface;
27

28
/**
29
 * @author Kévin Dunglas <dunglas@gmail.com>
30
 * @author Baptiste Meyer <baptiste.meyer@gmail.com>
31
 */
32
class ConfigurationTest extends TestCase
33
{
34
    private Configuration $configuration;
35

36
    private Processor $processor;
37

38
    protected function setUp(): void
39
    {
40
        $this->configuration = new Configuration();
×
41
        $this->processor = new Processor();
×
42
    }
43

44
    public function testDefaultConfig(): void
45
    {
46
        $this->runDefaultConfigTests();
×
47
    }
48

49
    public function testDefaultConfigWithMongoDbOdm(): void
50
    {
51
        $this->runDefaultConfigTests(['orm', 'odm']);
×
52
    }
53

54
    private function runDefaultConfigTests(array $doctrineIntegrationsToLoad = ['orm']): void
55
    {
56
        $treeBuilder = $this->configuration->getConfigTreeBuilder();
×
57
        $config = $this->processor->processConfiguration($this->configuration, [
×
58
            'api_platform' => [
×
59
                'title' => 'title',
×
60
                'description' => 'description',
×
61
                'version' => '1.0.0',
×
62
                'doctrine' => [
×
63
                    'enabled' => \in_array('orm', $doctrineIntegrationsToLoad, true),
×
64
                ],
×
65
                'doctrine_mongodb_odm' => [
×
66
                    'enabled' => \in_array('odm', $doctrineIntegrationsToLoad, true),
×
67
                ],
×
68
            ],
×
69
        ]);
×
70

71
        $this->assertInstanceOf(ConfigurationInterface::class, $this->configuration);
×
72
        $this->assertInstanceOf(TreeBuilder::class, $treeBuilder);
×
73
        $this->assertEquals([
×
74
            'title' => 'title',
×
75
            'description' => 'description',
×
76
            'version' => '1.0.0',
×
77
            'show_webby' => true,
×
78
            'formats' => [],
×
79
            'docs_formats' => [
×
80
                'jsonopenapi' => ['mime_types' => ['application/vnd.openapi+json']],
×
81
                'yamlopenapi' => ['mime_types' => ['application/vnd.openapi+yaml']],
×
82
                'jsonld' => ['mime_types' => ['application/ld+json']],
×
83
                'html' => ['mime_types' => ['text/html']],
×
84
            ],
×
85
            'patch_formats' => [
×
86
                'json' => ['mime_types' => ['application/merge-patch+json']],
×
87
            ],
×
88
            'error_formats' => [
×
89
                'jsonproblem' => ['mime_types' => ['application/problem+json']],
×
90
                'jsonld' => ['mime_types' => ['application/ld+json']],
×
91
                'json' => ['mime_types' => ['application/problem+json', 'application/json']],
×
92
            ],
×
93
            'jsonschema_formats' => [],
×
94
            'exception_to_status' => [
×
95
                ExceptionInterface::class => Response::HTTP_BAD_REQUEST,
×
96
                InvalidArgumentException::class => Response::HTTP_BAD_REQUEST,
×
97
                OptimisticLockException::class => Response::HTTP_CONFLICT,
×
98
            ],
×
99
            'path_segment_name_generator' => 'api_platform.metadata.path_segment_name_generator.underscore',
×
100
            'inflector' => 'api_platform.metadata.inflector',
×
101
            'validator' => [
×
102
                'serialize_payload_fields' => [],
×
103
                'query_parameter_validation' => true,
×
104
            ],
×
105
            'name_converter' => null,
×
106
            'enable_swagger' => true,
×
107
            'enable_swagger_ui' => true,
×
108
            'enable_entrypoint' => true,
×
109
            'enable_re_doc' => true,
×
110
            'enable_docs' => true,
×
111
            'enable_profiler' => true,
×
112
            'graphql' => [
×
113
                'enabled' => true,
×
114
                'default_ide' => 'graphiql',
×
115
                'graphql_playground' => [
×
116
                    'enabled' => true,
×
117
                ],
×
118
                'graphiql' => [
×
119
                    'enabled' => true,
×
120
                ],
×
121
                'introspection' => [
×
122
                    'enabled' => true,
×
123
                ],
×
124
                'nesting_separator' => '_',
×
125
                'collection' => [
×
126
                    'pagination' => [
×
127
                        'enabled' => true,
×
128
                    ],
×
129
                ],
×
130
            ],
×
131
            'elasticsearch' => [
×
132
                'enabled' => false,
×
133
                'hosts' => [],
×
134
            ],
×
135
            'oauth' => [
×
136
                'enabled' => false,
×
137
                'clientId' => '',
×
138
                'clientSecret' => '',
×
139
                'type' => 'oauth2',
×
140
                'flow' => 'application',
×
141
                'tokenUrl' => '',
×
142
                'authorizationUrl' => '',
×
143
                'refreshUrl' => '',
×
144
                'scopes' => [],
×
145
                'pkce' => false,
×
146
            ],
×
147
            'swagger' => [
×
148
                'versions' => [3],
×
149
                'api_keys' => [],
×
150
                'swagger_ui_extra_configuration' => [],
×
151
            ],
×
152
            'eager_loading' => [
×
153
                'enabled' => true,
×
154
                'max_joins' => 30,
×
155
                'force_eager' => true,
×
156
                'fetch_partial' => false,
×
157
            ],
×
158
            'collection' => [
×
159
                'exists_parameter_name' => 'exists',
×
160
                'order' => 'ASC',
×
161
                'order_parameter_name' => 'order',
×
162
                'order_nulls_comparison' => null,
×
163
                'pagination' => [
×
164
                    'enabled' => true,
×
165
                    'page_parameter_name' => 'page',
×
166
                    'enabled_parameter_name' => 'pagination',
×
167
                    'items_per_page_parameter_name' => 'itemsPerPage',
×
168
                    'partial_parameter_name' => 'partial',
×
169
                ],
×
170
            ],
×
171
            'mapping' => [
×
172
                'paths' => [],
×
173
            ],
×
174
            'http_cache' => [
×
175
                'invalidation' => [
×
176
                    'enabled' => false,
×
177
                    'varnish_urls' => [],
×
178
                    'request_options' => [],
×
179
                    'max_header_length' => 7500,
×
180
                    'purger' => 'api_platform.http_cache.purger.varnish',
×
181
                    'xkey' => ['glue' => ' '],
×
182
                    'urls' => [],
×
183
                    'scoped_clients' => [],
×
184
                ],
×
185
                'public' => null,
×
186
            ],
×
187
            'doctrine' => [
×
188
                'enabled' => \in_array('orm', $doctrineIntegrationsToLoad, true),
×
189
            ],
×
190
            'doctrine_mongodb_odm' => [
×
191
                'enabled' => \in_array('odm', $doctrineIntegrationsToLoad, true),
×
192
            ],
×
193
            'messenger' => [
×
194
                'enabled' => true,
×
195
            ],
×
196
            'mercure' => [
×
197
                'enabled' => true,
×
198
                'hub_url' => null,
×
199
                'include_type' => false,
×
200
            ],
×
201
            'resource_class_directories' => [],
×
202
            'asset_package' => null,
×
203
            'openapi' => [
×
204
                'contact' => [
×
205
                    'name' => null,
×
206
                    'url' => null,
×
207
                    'email' => null,
×
208
                ],
×
209
                'termsOfService' => null,
×
210
                'license' => [
×
211
                    'name' => null,
×
212
                    'url' => null,
×
213
                ],
×
214
                'swagger_ui_extra_configuration' => [],
×
215
                'overrideResponses' => true,
×
216
            ],
×
217
            'maker' => [
×
218
                'enabled' => true,
×
219
            ],
×
220
            'use_symfony_listeners' => false,
×
221
            'handle_symfony_errors' => false,
×
222
            'enable_link_security' => false,
×
NEW
223
            'serializer' => [
×
NEW
224
                'hydra_prefix' => null,
×
NEW
225
            ],
×
UNCOV
226
        ], $config);
×
227
    }
228

229
    public static function invalidHttpStatusCodeProvider(): array
230
    {
231
        return [
×
232
            [0],
×
233
            [99],
×
234
            [700],
×
235
            [1000],
×
236
        ];
×
237
    }
238

239
    #[\PHPUnit\Framework\Attributes\DataProvider('invalidHttpStatusCodeProvider')]
240
    public function testExceptionToStatusConfigWithInvalidHttpStatusCode($invalidHttpStatusCode): void
241
    {
242
        $this->expectException(InvalidConfigurationException::class);
×
243
        $this->expectExceptionMessageMatches('/The HTTP status code ".+" is not valid\\./');
×
244

245
        $this->processor->processConfiguration($this->configuration, [
×
246
            'api_platform' => [
×
247
                'exception_to_status' => [
×
248
                    \Exception::class => $invalidHttpStatusCode,
×
249
                ],
×
250
            ],
×
251
        ]);
×
252
    }
253

254
    public static function invalidHttpStatusCodeValueProvider(): array
255
    {
256
        return [
×
257
            [true],
×
258
            [null],
×
259
            [-\INF],
×
260
            [40.4],
×
261
            ['foo'],
×
262
            ['HTTP_FOO_BAR'],
×
263
        ];
×
264
    }
265

266
    #[\PHPUnit\Framework\Attributes\DataProvider('invalidHttpStatusCodeValueProvider')]
267
    public function testExceptionToStatusConfigWithInvalidHttpStatusCodeValue($invalidHttpStatusCodeValue): void
268
    {
269
        $this->expectException(InvalidTypeException::class);
×
270
        $this->expectExceptionMessageMatches('/Invalid type for path "api_platform\\.exception_to_status\\.Exception". Expected "?int"?, but got .+\\./');
×
271

272
        $this->processor->processConfiguration($this->configuration, [
×
273
            'api_platform' => [
×
274
                'exception_to_status' => [
×
275
                    \Exception::class => $invalidHttpStatusCodeValue,
×
276
                ],
×
277
            ],
×
278
        ]);
×
279
    }
280

281
    /**
282
     * Test config for api keys.
283
     */
284
    public function testInvalidApiKeysConfig(): void
285
    {
286
        $this->expectExceptionMessage('The api keys "key" is not valid according to the pattern enforced by OpenAPI 3.1 ^[a-zA-Z0-9._-]+$.');
×
287
        $exampleConfig = [
×
288
            'name' => 'Authorization',
×
289
            'type' => 'query',
×
290
        ];
×
291

292
        $config = $this->processor->processConfiguration($this->configuration, [
×
293
            'api_platform' => [
×
294
                'swagger' => [
×
295
                    'api_keys' => ['Some Authorization name, like JWT' => $exampleConfig, 'Another-Auth' => $exampleConfig],
×
296
                ],
×
297
            ],
×
298
        ]);
×
299
    }
300

301
    /**
302
     * Test config for api keys.
303
     */
304
    public function testApiKeysConfig(): void
305
    {
306
        $exampleConfig = [
×
307
            'name' => 'Authorization',
×
308
            'type' => 'query',
×
309
        ];
×
310

311
        $config = $this->processor->processConfiguration($this->configuration, [
×
312
            'api_platform' => [
×
313
                'swagger' => [
×
314
                    'api_keys' => ['authorization_name_like_JWT' => $exampleConfig],
×
315
                ],
×
316
            ],
×
317
        ]);
×
318

319
        $this->assertArrayHasKey('api_keys', $config['swagger']);
×
320
        $this->assertSame($exampleConfig, $config['swagger']['api_keys']['authorization_name_like_JWT']);
×
321
    }
322

323
    /**
324
     * Test config for disabled swagger versions.
325
     */
326
    public function testDisabledSwaggerVersionConfig(): void
327
    {
328
        $config = $this->processor->processConfiguration($this->configuration, [
×
329
            'api_platform' => [
×
330
                'enable_swagger' => false,
×
331
                'swagger' => [
×
332
                    'versions' => [3],
×
333
                ],
×
334
            ],
×
335
        ]);
×
336

337
        $this->assertArrayHasKey('versions', $config['swagger']);
×
338
        $this->assertEmpty($config['swagger']['versions']);
×
339
    }
340

341
    /**
342
     * Test config for swagger versions.
343
     */
344
    public function testSwaggerVersionConfig(): void
345
    {
346
        $config = $this->processor->processConfiguration($this->configuration, [
×
347
            'api_platform' => [
×
348
                'swagger' => [
×
349
                    'versions' => [3],
×
350
                ],
×
351
            ],
×
352
        ]);
×
353

354
        $this->assertArrayHasKey('versions', $config['swagger']);
×
355
        $this->assertEquals([3], $config['swagger']['versions']);
×
356

357
        $this->expectException(InvalidConfigurationException::class);
×
358
        $this->expectExceptionMessageMatches('/Only the versions .+ are supported. Got .+./');
×
359

360
        $this->processor->processConfiguration($this->configuration, [
×
361
            'api_platform' => [
×
362
                'swagger' => [
×
363
                    'versions' => [1],
×
364
                ],
×
365
            ],
×
366
        ]);
×
367
    }
368

369
    /**
370
     * Test config for empty title and description.
371
     */
372
    public function testEmptyTitleDescriptionConfig(): void
373
    {
374
        $config = $this->processor->processConfiguration($this->configuration, [
×
375
            'api_platform' => [],
×
376
        ]);
×
377

378
        $this->assertSame('', $config['title']);
×
379
        $this->assertSame('', $config['description']);
×
380
    }
381

382
    public function testEnableElasticsearch(): void
383
    {
384
        $config = $this->processor->processConfiguration($this->configuration, [
×
385
            'api_platform' => [
×
386
                'elasticsearch' => true,
×
387
            ],
×
388
        ]);
×
389

390
        $this->assertTrue($config['elasticsearch']['enabled']);
×
391
    }
392
}
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