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

api-platform / core / 10957611709

20 Sep 2024 10:27AM UTC coverage: 7.372% (-0.3%) from 7.647%
10957611709

push

github

dunglas
Merge branch '4.0'

25 of 40 new or added lines in 9 files covered. (62.5%)

111 existing lines in 4 files now uncovered.

12175 of 165159 relevant lines covered (7.37%)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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