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

api-platform / core / 10639861745

30 Aug 2024 10:21PM UTC coverage: 7.667%. Remained the same
10639861745

push

github

web-flow
test: anonymous bundle class breaks twig extension (#6566)

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

4209 existing lines in 143 files now uncovered.

12514 of 163211 relevant lines covered (7.67%)

22.86 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 ApiPlatform\Tests\PhpUnitBundle;
20
use Doctrine\Bundle\DoctrineBundle\ConnectionFactory;
21
use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
22
use Doctrine\Bundle\MongoDBBundle\Command\TailCursorDoctrineODMCommand;
23
use Doctrine\Bundle\MongoDBBundle\DoctrineMongoDBBundle;
24
use FriendsOfBehat\SymfonyExtension\Bundle\FriendsOfBehatSymfonyExtensionBundle;
25
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
26
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
27
use Symfony\Bundle\MakerBundle\MakerBundle;
28
use Symfony\Bundle\MercureBundle\MercureBundle;
29
use Symfony\Bundle\SecurityBundle\SecurityBundle;
30
use Symfony\Bundle\TwigBundle\TwigBundle;
31
use Symfony\Bundle\WebProfilerBundle\WebProfilerBundle;
32
use Symfony\Component\Config\Loader\LoaderInterface;
33
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
34
use Symfony\Component\DependencyInjection\ContainerBuilder;
35
use Symfony\Component\ErrorHandler\ErrorRenderer\ErrorRendererInterface;
36
use Symfony\Component\HttpClient\Messenger\PingWebhookMessageHandler;
37
use Symfony\Component\HttpFoundation\Session\SessionFactory;
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 PhpUnitBundle(),
×
UNCOV
73
        ];
×
74

75
        if (null === ($_ENV['APP_PHPUNIT'] ?? null)) {
×
76
            $bundles[] = new FriendsOfBehatSymfonyExtensionBundle();
×
77
        }
78

79
        if (class_exists(DoctrineMongoDBBundle::class)) {
×
80
            $bundles[] = new DoctrineMongoDBBundle();
×
81
        }
82

83
        $bundles[] = new TestBundle();
×
84

85
        return $bundles;
×
86
    }
87

88
    public function getProjectDir(): string
89
    {
90
        return __DIR__;
×
91
    }
92

93
    protected function configureRoutes($routes): void
94
    {
95
        $routes->import(__DIR__."/config/routing_{$this->getEnvironment()}.yml");
×
96
    }
97

98
    protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader): void
99
    {
100
        $c->setParameter('kernel.project_dir', __DIR__);
×
101

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

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

106
        $messengerConfig = [
×
107
            'default_bus' => 'messenger.bus.default',
×
108
            'buses' => [
×
109
                'messenger.bus.default' => ['default_middleware' => 'allow_no_handlers'],
×
110
            ],
×
111
        ];
×
112

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

152
        $c->prependExtensionConfig('framework', $config);
×
153

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

201
        if (!class_exists(SymfonyCoreUser::class)) {
×
202
            $securityConfig['role_hierarchy'] = [
×
203
                'ROLE_ADMIN' => ['ROLE_USER'],
×
204
            ];
×
205
            unset($securityConfig['firewalls']['default']['anonymous']);
×
206
            $securityConfig['firewalls']['default']['http_basic'] = [
×
207
                'realm' => 'Secured Area',
×
208
            ];
×
209
        }
210

211
        if (class_exists(NativePasswordHasher::class)) {
×
212
            unset($securityConfig['firewalls']['default']['anonymous']);
×
213
        }
214

215
        $c->prependExtensionConfig('security', $securityConfig);
×
216

217
        if (class_exists(DoctrineMongoDBBundle::class)) {
×
218
            $c->prependExtensionConfig('doctrine_mongodb', [
×
219
                'connections' => [
×
220
                    'default' => null,
×
221
                ],
×
222
                'document_managers' => [
×
223
                    'default' => [
×
224
                        'auto_mapping' => true,
×
225
                    ],
×
226
                ],
×
227
            ]);
×
228
        }
229

230
        $twigConfig = ['strict_variables' => '%kernel.debug%'];
×
231
        if (interface_exists(ErrorRendererInterface::class)) {
×
232
            $twigConfig['exception_controller'] = null;
×
233
        }
234
        $c->prependExtensionConfig('twig', $twigConfig);
×
235

236
        $useSymfonyListeners = (bool) ($_SERVER['USE_SYMFONY_LISTENERS'] ?? false);
×
237

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

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

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

277
        // We reduce the amount of resources to the strict minimum to speed up tests
278
        if (null !== ($_ENV['APP_PHPUNIT'] ?? null)) {
×
279
            $loader->load(__DIR__.'/config/phpunit.yml');
×
280
        }
281

282
        if ('mongodb' === $this->environment) {
×
283
            $c->prependExtensionConfig('api_platform', [
×
284
                'mapping' => [
×
285
                    'paths' => ['%kernel.project_dir%/../TestBundle/Resources/config/api_resources_odm'],
×
286
                ],
×
287
            ]);
×
288

289
            return;
×
290
        }
291

292
        $c->prependExtensionConfig('api_platform', [
×
293
            'mapping' => [
×
294
                'paths' => ['%kernel.project_dir%/../TestBundle/Resources/config/api_resources_orm'],
×
295
            ],
×
296
        ]);
×
297
    }
298

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