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

api-platform / core / 10315659289

09 Aug 2024 07:49AM UTC coverage: 7.841% (-0.006%) from 7.847%
10315659289

push

github

soyuka
style: cs fixes

70 of 529 new or added lines in 176 files covered. (13.23%)

160 existing lines in 58 files now uncovered.

12688 of 161818 relevant lines covered (7.84%)

26.86 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\Serializer\SerializerContextBuilderInterface;
25
use ApiPlatform\State\Pagination\Pagination;
26
use ApiPlatform\State\Pagination\PaginationOptions;
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
        'formats' => [
46
            'json' => ['mime_types' => ['json']],
47
            'jsonld' => ['mime_types' => ['application/ld+json']],
48
            'jsonhal' => ['mime_types' => ['application/hal+json']],
49
        ],
50
        'http_cache' => ['invalidation' => [
51
            'enabled' => true,
52
            'purger' => 'api_platform.http_cache.purger.varnish.ban',
53
            'request_options' => [
54
                'allow_redirects' => [
55
                    'max' => 5,
56
                    'protocols' => ['http', 'https'],
57
                    'stric' => false,
58
                    'referer' => false,
59
                    'track_redirects' => false,
60
                ],
61
                'http_errors' => true,
62
                'decode_content' => false,
63
                'verify' => false,
64
                'cookies' => true,
65
                'headers' => [
66
                    'User-Agent' => 'none',
67
                ],
68
            ],
69
        ]],
70
        'doctrine_mongodb_odm' => [
71
            'enabled' => true,
72
        ],
73
        'defaults' => [
74
            'extra_properties' => [],
75
            'url_generation_strategy' => UrlGeneratorInterface::ABS_URL,
76
        ],
77
        'collection' => [
78
            'exists_parameter_name' => 'exists',
79
            'order' => 'ASC',
80
            'order_parameter_name' => 'order',
81
            'order_nulls_comparison' => null,
82
            'pagination' => [
83
                'page_parameter_name' => 'page',
84
                'enabled_parameter_name' => 'pagination',
85
                'items_per_page_parameter_name' => 'itemsPerPage',
86
                'partial_parameter_name' => 'partial',
87
            ],
88
        ],
89
        'error_formats' => [
90
            'jsonproblem' => ['application/problem+json'],
91
            'jsonld' => ['application/ld+json'],
92
        ],
93
        'patch_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
        'show_webby' => true,
100
        'eager_loading' => [
101
            'enabled' => true,
102
            'max_joins' => 30,
103
            'force_eager' => true,
104
            'fetch_partial' => false,
105
        ],
106
        'asset_package' => null,
107
        'enable_entrypoint' => true,
108
        'enable_docs' => true,
109
        'graphql' => [
110
            'graphql_playground' => ['enabled' => false],
111
        ],
112
        'use_symfony_listeners' => false,
113
    ]];
114

115
    private ContainerBuilder $container;
116

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

137
        $this->container = new ContainerBuilder($containerParameterBag);
×
138
    }
139

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

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

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

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

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

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

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

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

202
            'api_platform.state_provider.content_negotiation',
×
203
            'api_platform.state_provider.deserialize',
×
204
            'api_platform.state_processor.respond',
×
205
            'api_platform.state_processor.add_link_header',
×
206
            'api_platform.state_processor.serialize',
×
207
        ];
×
208

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

229
        $this->assertContainerHas($services, $aliases);
×
230

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

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

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

250
        foreach ($services as $service) {
×
251
            $this->assertNotContainerHasService($service);
×
252
        }
253
    }
254

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

261
        $services = [
×
262
            'api_platform.listener.request.read',
×
263
            'api_platform.listener.request.deserialize',
×
264
            'api_platform.listener.request.add_format',
×
265
            'api_platform.listener.view.write',
×
266
            'api_platform.listener.view.serialize',
×
267
            'api_platform.listener.view.respond',
×
268

269
            'api_platform.state_provider.content_negotiation',
×
270
            'api_platform.state_provider.deserialize',
×
271
            'api_platform.state_processor.respond',
×
272
            'api_platform.state_processor.add_link_header',
×
273
            'api_platform.state_processor.serialize',
×
274
        ];
×
275

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

285
        $this->assertContainerHas($services, $aliases);
×
286
    }
287
}
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