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

api-platform / core / 15966359730

30 Jun 2025 07:13AM UTC coverage: 22.064% (-0.02%) from 22.082%
15966359730

push

github

soyuka
ci: distribution update on new tag only

11518 of 52203 relevant lines covered (22.06%)

22.04 hits per line

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

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

16
use ApiPlatform\Metadata\Exception\ExceptionInterface;
17
use ApiPlatform\Metadata\Exception\InvalidArgumentException;
18
use ApiPlatform\Metadata\IdentifiersExtractorInterface;
19
use ApiPlatform\Metadata\IriConverterInterface;
20
use ApiPlatform\Metadata\ResourceClassResolverInterface;
21
use ApiPlatform\Metadata\UrlGeneratorInterface;
22
use ApiPlatform\Serializer\Filter\GroupFilter;
23
use ApiPlatform\Serializer\Filter\PropertyFilter;
24
use ApiPlatform\State\Pagination\Pagination;
25
use ApiPlatform\State\Pagination\PaginationOptions;
26
use ApiPlatform\State\SerializerContextBuilderInterface;
27
use ApiPlatform\Symfony\Action\NotFoundAction;
28
use ApiPlatform\Symfony\Bundle\DependencyInjection\ApiPlatformExtension;
29
use ApiPlatform\Tests\Fixtures\TestBundle\TestBundle;
30
use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
31
use Doctrine\ORM\OptimisticLockException;
32
use PHPUnit\Framework\TestCase;
33
use Symfony\Bundle\SecurityBundle\SecurityBundle;
34
use Symfony\Bundle\TwigBundle\TwigBundle;
35
use Symfony\Component\DependencyInjection\ContainerBuilder;
36
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
37
use Symfony\Component\HttpFoundation\Response;
38

39
class ApiPlatformExtensionTest extends TestCase
40
{
41
    final public const DEFAULT_CONFIG = ['api_platform' => [
42
        'title' => 'title',
43
        'description' => 'description',
44
        'version' => 'version',
45
        'serializer' => ['hydra_prefix' => true],
46
        'formats' => [
47
            'json' => ['mime_types' => ['json']],
48
            'jsonld' => ['mime_types' => ['application/ld+json']],
49
            'jsonhal' => ['mime_types' => ['application/hal+json']],
50
        ],
51
        'http_cache' => ['invalidation' => [
52
            'enabled' => true,
53
            'purger' => 'api_platform.http_cache.purger.varnish.ban',
54
            'request_options' => [
55
                'allow_redirects' => [
56
                    'max' => 5,
57
                    'protocols' => ['http', 'https'],
58
                    'stric' => false,
59
                    'referer' => false,
60
                    'track_redirects' => false,
61
                ],
62
                'http_errors' => true,
63
                'decode_content' => false,
64
                'verify' => false,
65
                'cookies' => true,
66
                'headers' => [
67
                    'User-Agent' => 'none',
68
                ],
69
            ],
70
        ]],
71
        'doctrine_mongodb_odm' => [
72
            'enabled' => true,
73
        ],
74
        'defaults' => [
75
            'extra_properties' => [],
76
            'url_generation_strategy' => UrlGeneratorInterface::ABS_URL,
77
        ],
78
        'collection' => [
79
            'exists_parameter_name' => 'exists',
80
            'order' => 'ASC',
81
            'order_parameter_name' => 'order',
82
            'order_nulls_comparison' => null,
83
            'pagination' => [
84
                'page_parameter_name' => 'page',
85
                'enabled_parameter_name' => 'pagination',
86
                'items_per_page_parameter_name' => 'itemsPerPage',
87
                'partial_parameter_name' => 'partial',
88
            ],
89
        ],
90
        'error_formats' => [
91
            'jsonproblem' => ['application/problem+json'],
92
            'jsonld' => ['application/ld+json'],
93
        ],
94
        'patch_formats' => [],
95
        'exception_to_status' => [
96
            ExceptionInterface::class => Response::HTTP_BAD_REQUEST,
97
            InvalidArgumentException::class => Response::HTTP_BAD_REQUEST,
98
            OptimisticLockException::class => Response::HTTP_CONFLICT,
99
        ],
100
        'show_webby' => true,
101
        'eager_loading' => [
102
            'enabled' => true,
103
            'max_joins' => 30,
104
            'force_eager' => true,
105
            'fetch_partial' => false,
106
        ],
107
        'asset_package' => null,
108
        'enable_entrypoint' => true,
109
        'enable_docs' => true,
110
        'use_symfony_listeners' => false,
111
    ]];
112

113
    private ContainerBuilder $container;
114

115
    protected function setUp(): void
116
    {
117
        $containerParameterBag = new ParameterBag([
×
118
            'kernel.bundles' => [
×
119
                'DoctrineBundle' => DoctrineBundle::class,
×
120
                'SecurityBundle' => SecurityBundle::class,
×
121
                'TwigBundle' => TwigBundle::class,
×
122
            ],
×
123
            'kernel.bundles_metadata' => [
×
124
                'TestBundle' => [
×
125
                    'parent' => null,
×
126
                    'path' => realpath(__DIR__.'/../../../Fixtures/TestBundle'),
×
127
                    'namespace' => TestBundle::class,
×
128
                ],
×
129
            ],
×
130
            'kernel.project_dir' => __DIR__.'/../../../Fixtures/app',
×
131
            'kernel.debug' => false,
×
132
            'kernel.environment' => 'test',
×
133
        ]);
×
134

135
        $this->container = new ContainerBuilder($containerParameterBag);
×
136
    }
137

138
    private function assertContainerHas(array $services, array $aliases = []): void
139
    {
140
        foreach ($services as $service) {
×
141
            $this->assertTrue($this->container->hasDefinition($service), \sprintf('Definition "%s" not found.', $service));
×
142
        }
143

144
        foreach ($aliases as $alias) {
×
145
            $this->assertContainerHasAlias($alias);
×
146
        }
147
    }
148

149
    private function assertContainerHasService(string $service): void
150
    {
151
        $this->assertTrue($this->container->hasDefinition($service), \sprintf('Service "%s" not found.', $service));
×
152
    }
153

154
    private function assertNotContainerHasService(string $service): void
155
    {
156
        $this->assertFalse($this->container->hasDefinition($service), \sprintf('Service "%s" found.', $service));
×
157
    }
158

159
    private function assertContainerHasAlias(string $alias): void
160
    {
161
        $this->assertTrue($this->container->hasAlias($alias), \sprintf('Alias "%s" not found.', $alias));
×
162
    }
163

164
    private function assertServiceHasTags(string $service, array $tags = []): void
165
    {
166
        $serviceTags = $this->container->getDefinition($service)->getTags();
×
167

168
        foreach ($tags as $tag) {
×
169
            $this->assertArrayHasKey($tag, $serviceTags, \sprintf('Tag "%s" not found on the service "%s".', $tag, $service));
×
170
        }
171
    }
172

173
    public function testCommonConfiguration(): void
174
    {
175
        $config = self::DEFAULT_CONFIG;
×
176
        (new ApiPlatformExtension())->load($config, $this->container);
×
177

178
        $services = [
×
179
            'api_platform.action.documentation',
×
180
            'api_platform.action.entrypoint',
×
181
            'api_platform.action.not_found',
×
182
            'api_platform.api.identifiers_extractor',
×
183
            'api_platform.filter_locator',
×
184
            'api_platform.negotiator',
×
185
            'api_platform.pagination',
×
186
            'api_platform.pagination_options',
×
187
            'api_platform.path_segment_name_generator.dash',
×
188
            'api_platform.path_segment_name_generator.underscore',
×
189
            'api_platform.metadata.inflector',
×
190
            'api_platform.resource_class_resolver',
×
191
            'api_platform.route_loader',
×
192
            'api_platform.router',
×
193
            'api_platform.serializer.context_builder',
×
194
            'api_platform.serializer.context_builder.filter',
×
195
            'api_platform.serializer.group_filter',
×
196
            'api_platform.serializer.mapping.class_metadata_factory',
×
197
            'api_platform.serializer.normalizer.item',
×
198
            'api_platform.serializer.property_filter',
×
199
            'api_platform.serializer_locator',
×
200
            'api_platform.symfony.iri_converter',
×
201
            'api_platform.uri_variables.converter',
×
202
            'api_platform.uri_variables.transformer.date_time',
×
203
            'api_platform.uri_variables.transformer.integer',
×
204

205
            'api_platform.state_provider.content_negotiation',
×
206
            'api_platform.state_provider.deserialize',
×
207
            'api_platform.state_processor.respond',
×
208
            'api_platform.state_processor.add_link_header',
×
209
            'api_platform.state_processor.serialize',
×
210
        ];
×
211

212
        $aliases = [
×
213
            NotFoundAction::class,
×
214
            IdentifiersExtractorInterface::class,
×
215
            IriConverterInterface::class,
×
216
            ResourceClassResolverInterface::class,
×
217
            UrlGeneratorInterface::class,
×
218
            GroupFilter::class,
×
219
            PropertyFilter::class,
×
220
            SerializerContextBuilderInterface::class,
×
221
            Pagination::class,
×
222
            PaginationOptions::class,
×
223
            'api_platform.identifiers_extractor',
×
224
            'api_platform.iri_converter',
×
225
            'api_platform.path_segment_name_generator',
×
226
            'api_platform.property_accessor',
×
227
            'api_platform.property_info',
×
228
            'api_platform.serializer',
×
229
            'api_platform.inflector',
×
230
        ];
×
231

232
        $this->assertContainerHas($services, $aliases);
×
233

234
        $this->assertServiceHasTags('api_platform.cache.route_name_resolver', ['cache.pool']);
×
235
        $this->assertServiceHasTags('api_platform.serializer.normalizer.item', ['serializer.normalizer']);
×
236
        $this->assertServiceHasTags('api_platform.serializer_locator', ['container.service_locator']);
×
237
        $this->assertServiceHasTags('api_platform.filter_locator', ['container.service_locator']);
×
238

239
        // api.xml
240
        $this->assertServiceHasTags('api_platform.route_loader', ['routing.loader']);
×
241
        $this->assertServiceHasTags('api_platform.uri_variables.transformer.integer', ['api_platform.uri_variables.transformer']);
×
242
        $this->assertServiceHasTags('api_platform.uri_variables.transformer.date_time', ['api_platform.uri_variables.transformer']);
×
243

244
        $services = [
×
245
            'api_platform.listener.request.read',
×
246
            'api_platform.listener.request.deserialize',
×
247
            'api_platform.listener.request.add_format',
×
248
            'api_platform.listener.view.write',
×
249
            'api_platform.listener.view.serialize',
×
250
            'api_platform.listener.view.respond',
×
251
        ];
×
252

253
        foreach ($services as $service) {
×
254
            $this->assertNotContainerHasService($service);
×
255
        }
256
    }
257

258
    public function testEventListenersConfiguration(): void
259
    {
260
        $config = self::DEFAULT_CONFIG;
×
261
        $config['api_platform']['use_symfony_listeners'] = true;
×
262
        (new ApiPlatformExtension())->load($config, $this->container);
×
263

264
        $services = [
×
265
            'api_platform.listener.request.read',
×
266
            'api_platform.listener.request.deserialize',
×
267
            'api_platform.listener.request.add_format',
×
268
            'api_platform.listener.view.write',
×
269
            'api_platform.listener.view.serialize',
×
270
            'api_platform.listener.view.respond',
×
271

272
            'api_platform.state_provider.content_negotiation',
×
273
            'api_platform.state_provider.deserialize',
×
274
            'api_platform.state_processor.respond',
×
275
            'api_platform.state_processor.add_link_header',
×
276
            'api_platform.state_processor.serialize',
×
277
        ];
×
278

279
        $aliases = [
×
280
            'api_platform.action.delete_item',
×
281
            'api_platform.action.get_collection',
×
282
            'api_platform.action.get_item',
×
283
            'api_platform.action.patch_item',
×
284
            'api_platform.action.post_collection',
×
285
            'api_platform.action.put_item',
×
286
        ];
×
287

288
        $this->assertContainerHas($services, $aliases);
×
289
        $this->container->hasParameter('api_platform.swagger.http_auth');
×
290
    }
291

292
    public function testItRegistersMetadataConfiguration(): void
293
    {
294
        $config = self::DEFAULT_CONFIG;
×
295
        $config['api_platform']['mapping']['imports'] = [__DIR__.'/php'];
×
296
        (new ApiPlatformExtension())->load($config, $this->container);
×
297

298
        $emptyPhpFile = realpath(__DIR__.'/php/empty_file.php');
×
299
        $this->assertContainerHasService('api_platform.metadata.resource_extractor.php_file');
×
300
        $this->assertSame([$emptyPhpFile], $this->container->getDefinition('api_platform.metadata.resource_extractor.php_file')->getArgument(0));
×
301
    }
302
}
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