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

api-platform / core / 10014117656

19 Jul 2024 08:44PM UTC coverage: 7.856% (-56.3%) from 64.185%
10014117656

push

github

soyuka
Merge branch 'sf/remove-flag'

0 of 527 new or added lines in 83 files covered. (0.0%)

10505 existing lines in 362 files now uncovered.

12705 of 161727 relevant lines covered (7.86%)

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

44
/**
45
 * AppKernel for tests.
46
 *
47
 * @author Kévin Dunglas <dunglas@gmail.com>
48
 */
49
class AppKernel extends Kernel
50
{
51
    use MicroKernelTrait;
52

53
    public function __construct(string $environment, bool $debug)
54
    {
55
        parent::__construct($environment, $debug);
×
56

57
        // patch for behat/symfony2-extension not supporting %env(APP_ENV)%
58
        $this->environment = $_SERVER['APP_ENV'] ?? $environment;
×
59
    }
60

61
    public function registerBundles(): array
62
    {
63
        $bundles = [
×
64
            new ApiPlatformBundle(),
×
65
            new TwigBundle(),
×
66
            new DoctrineBundle(),
×
67
            new MercureBundle(),
×
68
            new SecurityBundle(),
×
69
            new WebProfilerBundle(),
×
70
            new FrameworkBundle(),
×
71
            new MakerBundle(),
×
NEW
72
            new class() extends Bundle {
×
73
                public function shutdown(): void
74
                {
NEW
75
                    restore_exception_handler();
×
76
                }
NEW
77
            },
×
78
        ];
×
79

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

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

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

90
        return $bundles;
×
91
    }
92

93
    public function getProjectDir(): string
94
    {
95
        return __DIR__;
×
96
    }
97

98
    protected function configureRoutes($routes): void
99
    {
100
        $routes->import(__DIR__."/config/routing_{$this->getEnvironment()}.yml");
×
101
    }
102

103
    protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader): void
104
    {
105
        $c->setParameter('kernel.project_dir', __DIR__);
×
106

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

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

111
        $messengerConfig = [
×
112
            'default_bus' => 'messenger.bus.default',
×
113
            'buses' => [
×
114
                'messenger.bus.default' => ['default_middleware' => 'allow_no_handlers'],
×
115
            ],
×
116
        ];
×
117

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

157
        $c->prependExtensionConfig('framework', $config);
×
158

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

206
        if (!class_exists(SymfonyCoreUser::class)) {
×
207
            $securityConfig['role_hierarchy'] = [
×
208
                'ROLE_ADMIN' => ['ROLE_USER'],
×
209
            ];
×
210
            unset($securityConfig['firewalls']['default']['anonymous']);
×
211
            $securityConfig['firewalls']['default']['http_basic'] = [
×
212
                'realm' => 'Secured Area',
×
213
            ];
×
214
        }
215

216
        if (class_exists(NativePasswordHasher::class)) {
×
217
            unset($securityConfig['firewalls']['default']['anonymous']);
×
218
        }
219

220
        $c->prependExtensionConfig('security', $securityConfig);
×
221

222
        if (class_exists(DoctrineMongoDBBundle::class)) {
×
223
            $c->prependExtensionConfig('doctrine_mongodb', [
×
224
                'connections' => [
×
225
                    'default' => null,
×
226
                ],
×
227
                'document_managers' => [
×
228
                    'default' => [
×
229
                        'auto_mapping' => true,
×
230
                    ],
×
231
                ],
×
232
            ]);
×
233
        }
234

235
        $twigConfig = ['strict_variables' => '%kernel.debug%'];
×
236
        if (interface_exists(ErrorRendererInterface::class)) {
×
237
            $twigConfig['exception_controller'] = null;
×
238
        }
239
        $c->prependExtensionConfig('twig', $twigConfig);
×
240

241
        $useSymfonyListeners = (bool) ($_SERVER['USE_SYMFONY_LISTENERS'] ?? false);
×
242

NEW
243
        $c->prependExtensionConfig('api_platform', [
×
244
            'mapping' => [
×
245
                'paths' => ['%kernel.project_dir%/../TestBundle/Resources/config/api_resources'],
×
246
            ],
×
247
            'graphql' => [
×
248
                'graphql_playground' => false,
×
249
            ],
×
250
            'use_symfony_listeners' => $useSymfonyListeners,
×
251
            'defaults' => [
×
252
                'pagination_client_enabled' => true,
×
253
                'pagination_client_items_per_page' => true,
×
254
                'pagination_client_partial' => true,
×
255
                'pagination_items_per_page' => 3,
×
256
                'cache_headers' => [
×
257
                    'max_age' => 60,
×
258
                    'shared_max_age' => 3600,
×
259
                    'vary' => ['Accept', 'Cookie'],
×
260
                    'public' => true,
×
261
                ],
×
262
                'normalization_context' => ['skip_null_values' => false],
×
263
                'extra_properties' => [
×
264
                    'standard_put' => true,
×
265
                ],
×
266
            ],
×
267
        ]);
×
268

269
        // TODO: remove this check and move this config in config_common.yml when dropping support for DoctrineBundle <2.10
270
        if (defined(ConnectionFactory::class.'::DEFAULT_SCHEME_MAP')) {
×
271
            $c->prependExtensionConfig('doctrine', [
×
272
                'orm' => [
×
273
                    'report_fields_where_declared' => true,
×
274
                    'controller_resolver' => ['auto_mapping' => false],
×
275
                    'enable_lazy_ghost_objects' => true,
×
276
                ],
×
277
            ]);
×
278
        }
279

280
        $loader->load(__DIR__.'/config/config_swagger.php');
×
281

282
        // We reduce the amount of resources to the strict minimum to speed up tests
NEW
283
        if (null !== ($_ENV['APP_PHPUNIT'] ?? null)) {
×
NEW
284
            $loader->load(__DIR__.'/config/phpunit.yml');
×
285
        }
286

287
        if ('mongodb' === $this->environment) {
×
288
            $c->prependExtensionConfig('api_platform', [
×
289
                'mapping' => [
×
290
                    'paths' => ['%kernel.project_dir%/../TestBundle/Resources/config/api_resources_odm'],
×
291
                ],
×
292
            ]);
×
293

294
            return;
×
295
        }
296

297
        $c->prependExtensionConfig('api_platform', [
×
298
            'mapping' => [
×
299
                'paths' => ['%kernel.project_dir%/../TestBundle/Resources/config/api_resources_orm'],
×
300
            ],
×
301
        ]);
×
302
    }
303

304
    protected function build(ContainerBuilder $container): void
305
    {
306
        $container->addCompilerPass(new class() implements CompilerPassInterface {
×
307
            public function process(ContainerBuilder $container): void
308
            {
309
                if ($container->hasDefinition(TailCursorDoctrineODMCommand::class)) { // @phpstan-ignore-line
×
310
                    // Deprecated command triggering a Symfony depreciation
311
                    $container->removeDefinition(TailCursorDoctrineODMCommand::class); // @phpstan-ignore-line
×
312
                }
313
            }
314
        });
×
315
    }
316
}
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