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

api-platform / core / 13203378522

07 Feb 2025 03:56PM UTC coverage: 8.501% (+0.7%) from 7.837%
13203378522

push

github

soyuka
Merge 4.1

111 of 490 new or added lines in 51 files covered. (22.65%)

5590 existing lines in 163 files now uncovered.

13345 of 156987 relevant lines covered (8.5%)

22.88 hits per line

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

0.0
/tests/Fixtures/app/AppKernel.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
use ApiPlatform\Metadata\Delete;
15
use ApiPlatform\Metadata\Get;
16
use ApiPlatform\Metadata\GetCollection;
17
use ApiPlatform\Metadata\Patch;
18
use ApiPlatform\Metadata\Post;
19
use ApiPlatform\Metadata\Put;
20
use ApiPlatform\Symfony\Bundle\ApiPlatformBundle;
21
use ApiPlatform\Tests\Behat\DoctrineContext;
22
use ApiPlatform\Tests\Fixtures\TestBundle\Document\User as UserDocument;
23
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\User;
24
use ApiPlatform\Tests\Fixtures\TestBundle\TestBundle;
25
use Doctrine\Bundle\DoctrineBundle\ConnectionFactory;
26
use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
27
use Doctrine\Bundle\MongoDBBundle\Command\TailCursorDoctrineODMCommand;
28
use Doctrine\Bundle\MongoDBBundle\DoctrineMongoDBBundle;
29
use FriendsOfBehat\SymfonyExtension\Bundle\FriendsOfBehatSymfonyExtensionBundle;
30
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
31
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
32
use Symfony\Bundle\MakerBundle\MakerBundle;
33
use Symfony\Bundle\MercureBundle\MercureBundle;
34
use Symfony\Bundle\SecurityBundle\SecurityBundle;
35
use Symfony\Bundle\TwigBundle\TwigBundle;
36
use Symfony\Bundle\WebProfilerBundle\WebProfilerBundle;
37
use Symfony\Component\Config\Loader\LoaderInterface;
38
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
39
use Symfony\Component\DependencyInjection\ContainerBuilder;
40
use Symfony\Component\ErrorHandler\ErrorRenderer\ErrorRendererInterface;
41
use Symfony\Component\HttpClient\Messenger\PingWebhookMessageHandler;
42
use Symfony\Component\HttpFoundation\Session\SessionFactory;
43
use Symfony\Component\HttpKernel\Kernel;
44
use Symfony\Component\PasswordHasher\Hasher\NativePasswordHasher;
45
use Symfony\Component\Security\Core\Authorization\Strategy\AccessDecisionStrategyInterface;
46
use Symfony\Component\Security\Core\User\User as SymfonyCoreUser;
47
use Symfony\Component\Security\Core\User\UserInterface;
48

49
/**
50
 * AppKernel for tests.
51
 *
52
 * @author Kévin Dunglas <dunglas@gmail.com>
53
 */
54
class AppKernel extends Kernel
55
{
56
    use MicroKernelTrait;
57

58
    public function __construct(string $environment, bool $debug)
59
    {
60
        parent::__construct($environment, $debug);
×
61

62
        // patch for behat/symfony2-extension not supporting %env(APP_ENV)%
63
        $this->environment = $_SERVER['APP_ENV'] ?? $environment;
×
64
    }
65

66
    public function registerBundles(): array
67
    {
68
        $bundles = [
×
69
            new ApiPlatformBundle(),
×
70
            new TwigBundle(),
×
71
            new DoctrineBundle(),
×
72
            new MercureBundle(),
×
73
            new SecurityBundle(),
×
74
            new WebProfilerBundle(),
×
75
            new FrameworkBundle(),
×
76
            new MakerBundle(),
×
77
        ];
×
78

79
        if (null === ($_ENV['APP_PHPUNIT'] ?? null)) {
×
80
            $bundles[] = new FriendsOfBehatSymfonyExtensionBundle();
×
81
        }
82

83
        if (class_exists(DoctrineMongoDBBundle::class)) {
×
84
            $bundles[] = new DoctrineMongoDBBundle();
×
85
        }
86

87
        $bundles[] = new TestBundle();
×
88

89
        return $bundles;
×
90
    }
91

92
    public function shutdown(): void
93
    {
94
        parent::shutdown();
×
95
        restore_exception_handler();
×
96
    }
97

98
    public function getProjectDir(): string
99
    {
100
        return __DIR__;
×
101
    }
102

103
    protected function configureRoutes($routes): void
104
    {
105
        $routes->import(__DIR__."/config/routing_{$this->getEnvironment()}.yml");
×
106
    }
107

108
    protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader): void
109
    {
110
        $c->setParameter('kernel.project_dir', __DIR__);
×
111

112
        $loader->load(__DIR__."/config/config_{$this->getEnvironment()}.yml");
×
113

114
        $c->getDefinition(DoctrineContext::class)->setArgument('$passwordHasher', class_exists(NativePasswordHasher::class) ? 'security.user_password_encoder' : 'security.user_password_hasher');
×
115

116
        $messengerConfig = [
×
117
            'default_bus' => 'messenger.bus.default',
×
118
            'buses' => [
×
119
                'messenger.bus.default' => ['default_middleware' => 'allow_no_handlers'],
×
120
            ],
×
121
        ];
×
122

123
        $cookie = ['cookie_secure' => true, 'cookie_samesite' => 'lax', 'handler_id' => 'session.handler.native_file'];
×
124
        // This class is introduced in Symfony 6.4 just using it to use the new configuration and to avoid unnecessary deprecations
125
        if (class_exists(PingWebhookMessageHandler::class)) {
×
126
            $config = [
×
127
                'secret' => 'dunglas.fr',
×
128
                'validation' => ['enable_attributes' => true, 'email_validation_mode' => 'html5'],
×
129
                'serializer' => ['enable_attributes' => true],
×
130
                'test' => null,
×
131
                'session' => class_exists(SessionFactory::class) ? ['storage_factory_id' => 'session.storage.factory.mock_file'] + $cookie : ['storage_id' => 'session.storage.mock_file'] + $cookie,
×
132
                'profiler' => [
×
133
                    'enabled' => true,
×
134
                    'collect' => false,
×
135
                ],
×
136
                'php_errors' => ['log' => true],
×
137
                'messenger' => $messengerConfig,
×
138
                'router' => ['utf8' => true],
×
139
                'http_method_override' => false,
×
140
                'annotations' => false,
×
141
                'handle_all_throwables' => true,
×
142
                'uid' => ['default_uuid_version' => 7, 'time_based_uuid_version' => 7],
×
143
            ];
×
144
        } else {
145
            $config = [
×
146
                'secret' => 'dunglas.fr',
×
147
                'validation' => ['enable_annotations' => true],
×
148
                'serializer' => ['enable_annotations' => true],
×
149
                'test' => null,
×
150
                'session' => class_exists(SessionFactory::class) ? ['storage_factory_id' => 'session.storage.factory.mock_file'] : ['storage_id' => 'session.storage.mock_file'],
×
151
                'profiler' => [
×
152
                    'enabled' => true,
×
153
                    'collect' => false,
×
154
                ],
×
155
                'messenger' => $messengerConfig,
×
156
                'router' => ['utf8' => true],
×
157
                'http_method_override' => false,
×
158
                'annotations' => false,
×
159
            ];
×
160
        }
161

162
        $c->prependExtensionConfig('framework', $config);
×
163

164
        $alg = class_exists(NativePasswordHasher::class, false) || class_exists('Symfony\Component\Security\Core\Encoder\NativePasswordEncoder') ? 'auto' : 'bcrypt';
×
165
        $securityConfig = [
×
166
            class_exists(NativePasswordHasher::class) ? 'password_hashers' : 'encoders' => [
×
167
                User::class => $alg,
×
168
                UserDocument::class => $alg,
×
169
                // Don't use plaintext in production!
170
                UserInterface::class => 'plaintext',
×
171
            ],
×
172
            'providers' => [
×
173
                'chain_provider' => [
×
174
                    'chain' => [
×
175
                        'providers' => ['in_memory', 'entity'],
×
176
                    ],
×
177
                ],
×
178
                'in_memory' => [
×
179
                    'memory' => [
×
180
                        'users' => [
×
181
                            'dunglas' => ['password' => 'kevin', 'roles' => 'ROLE_USER'],
×
182
                            'admin' => ['password' => 'kitten', 'roles' => 'ROLE_ADMIN'],
×
183
                        ],
×
184
                    ],
×
185
                ],
×
186
                'entity' => [
×
187
                    'entity' => [
×
188
                        'class' => User::class,
×
189
                        'property' => 'email',
×
190
                    ],
×
191
                ],
×
192
            ],
×
193
            'firewalls' => [
×
194
                'dev' => [
×
195
                    'pattern' => '^/(_(profiler|wdt|error)|css|images|js)/',
×
196
                    'security' => false,
×
197
                ],
×
198
                'default' => [
×
199
                    'provider' => 'chain_provider',
×
200
                    'stateless' => true,
×
201
                    'http_basic' => null,
×
202
                    'anonymous' => null,
×
203
                    'entry_point' => 'app.security.authentication_entrypoint',
×
204
                ],
×
205
            ],
×
206
            'access_control' => [
×
207
                ['path' => '^/', 'role' => interface_exists(AccessDecisionStrategyInterface::class) ? 'PUBLIC_ACCESS' : 'IS_AUTHENTICATED_ANONYMOUSLY'],
×
208
            ],
×
209
        ];
×
210

211
        if (!class_exists(SymfonyCoreUser::class)) {
×
212
            $securityConfig['role_hierarchy'] = [
×
213
                'ROLE_ADMIN' => ['ROLE_USER'],
×
214
            ];
×
215
            unset($securityConfig['firewalls']['default']['anonymous']);
×
216
            $securityConfig['firewalls']['default']['http_basic'] = [
×
217
                'realm' => 'Secured Area',
×
218
            ];
×
219
        }
220

221
        if (class_exists(NativePasswordHasher::class)) {
×
222
            unset($securityConfig['firewalls']['default']['anonymous']);
×
223
        }
224

225
        $c->prependExtensionConfig('security', $securityConfig);
×
226

227
        if (class_exists(DoctrineMongoDBBundle::class)) {
×
228
            $c->prependExtensionConfig('doctrine_mongodb', [
×
229
                'connections' => [
×
230
                    'default' => null,
×
231
                ],
×
232
                'document_managers' => [
×
233
                    'default' => [
×
234
                        'auto_mapping' => true,
×
235
                    ],
×
236
                ],
×
237
            ]);
×
238
        }
239

240
        $twigConfig = ['strict_variables' => '%kernel.debug%'];
×
241
        if (interface_exists(ErrorRendererInterface::class)) {
×
242
            $twigConfig['exception_controller'] = null;
×
243
        }
244
        $c->prependExtensionConfig('twig', $twigConfig);
×
245

246
        $useSymfonyListeners = (bool) ($_SERVER['USE_SYMFONY_LISTENERS'] ?? false);
×
247

248
        $c->prependExtensionConfig('api_platform', [
×
249
            'mapping' => [
×
250
                'paths' => ['%kernel.project_dir%/../TestBundle/Resources/config/api_resources'],
×
251
            ],
×
252
            'graphql' => [
×
253
                'graphql_playground' => false,
×
254
                'max_query_depth' => 200,
×
255
            ],
×
256
            'use_symfony_listeners' => $useSymfonyListeners,
×
257
            'defaults' => [
×
258
                'pagination_client_enabled' => true,
×
259
                'pagination_client_items_per_page' => true,
×
260
                'pagination_client_partial' => true,
×
261
                'pagination_items_per_page' => 3,
×
262
                'cache_headers' => [
×
263
                    'max_age' => 60,
×
264
                    'shared_max_age' => 3600,
×
265
                    'vary' => ['Accept', 'Cookie'],
×
266
                    'public' => true,
×
267
                ],
×
268
                'normalization_context' => ['skip_null_values' => false],
×
269
                'operations' => [
×
270
                    Get::class,
×
271
                    GetCollection::class,
×
272
                    Post::class,
×
273
                    Put::class,
×
274
                    Patch::class,
×
275
                    Delete::class,
×
276
                ],
×
277
            ],
×
278
            'serializer' => [
×
279
                'hydra_prefix' => true,
×
280
            ],
×
UNCOV
281
        ]);
×
282

283
        // TODO: remove this check and move this config in config_common.yml when dropping support for DoctrineBundle <2.10
284
        if (defined(ConnectionFactory::class.'::DEFAULT_SCHEME_MAP')) {
×
285
            $c->prependExtensionConfig('doctrine', [
×
286
                'orm' => [
×
287
                    'report_fields_where_declared' => true,
×
288
                    'controller_resolver' => ['auto_mapping' => false],
×
289
                    'enable_lazy_ghost_objects' => true,
×
290
                ],
×
UNCOV
291
            ]);
×
292
        }
293

UNCOV
294
        $loader->load(__DIR__.'/config/config_swagger.php');
×
295

296
        // We reduce the amount of resources to the strict minimum to speed up tests
297
        if (null !== ($_ENV['APP_PHPUNIT'] ?? null)) {
×
UNCOV
298
            $loader->load(__DIR__.'/config/phpunit.yml');
×
299
        }
300

301
        if ('mongodb' === $this->environment) {
×
302
            $c->prependExtensionConfig('api_platform', [
×
303
                'mapping' => [
×
304
                    'paths' => ['%kernel.project_dir%/../TestBundle/Resources/config/api_resources_odm'],
×
305
                ],
×
UNCOV
306
            ]);
×
307

UNCOV
308
            return;
×
309
        }
310

311
        $c->prependExtensionConfig('api_platform', [
×
312
            'mapping' => [
×
313
                'paths' => ['%kernel.project_dir%/../TestBundle/Resources/config/api_resources_orm'],
×
314
            ],
×
UNCOV
315
        ]);
×
316
    }
317

318
    protected function build(ContainerBuilder $container): void
319
    {
UNCOV
320
        $container->addCompilerPass(new class implements CompilerPassInterface {
×
321
            public function process(ContainerBuilder $container): void
322
            {
UNCOV
323
                if ($container->hasDefinition(TailCursorDoctrineODMCommand::class)) { // @phpstan-ignore-line
×
324
                    // Deprecated command triggering a Symfony depreciation
UNCOV
325
                    $container->removeDefinition(TailCursorDoctrineODMCommand::class); // @phpstan-ignore-line
×
326
                }
327
            }
UNCOV
328
        });
×
329
    }
330
}
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

© 2025 Coveralls, Inc