Skip to content

Commit 500062d

Browse files
fix(symfony): add a alwaysBootKernel property for BC layer (api-platform#7007)
* fix(symfony): implement $alwaysBootKernel property for bc-layer * fix(symfony): ensures that getContainer is not overriden * fix(symfony): update test for kernel boot strategy * fix(symfony): cs for trigger_deprecation * fix(symfony): possibility to hide deprecation and set alwaysBootKernel to false by default * fix(symfony): emulate 5.0 kernel behaviour for functional tests * fix(symfony): emulate 5.0 kernel behaviour for functional tests
1 parent 2771363 commit 500062d

28 files changed

+76
-2
lines changed

src/Symfony/Bundle/Test/ApiTestCase.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ abstract class ApiTestCase extends KernelTestCase
2828
{
2929
use ApiTestAssertionsTrait;
3030

31+
/**
32+
* If you're using RecreateDatabaseTrait, RefreshDatabaseTrait, ReloadDatabaseTrait from theofidry/AliceBundle, you
33+
* probably need to set this property to false in your test class to avoid recreating the database on each client creation.
34+
*
35+
* - `null` triggers a deprecation message and always boots the kernel
36+
* - `false` does not boot the kernel if it's already booted
37+
* - `true` always boots the kernel without any deprecation message
38+
*/
39+
protected static ?bool $alwaysBootKernel = null;
40+
3141
/**
3242
* Creates a Client.
3343
*
@@ -36,15 +46,23 @@ abstract class ApiTestCase extends KernelTestCase
3646
*/
3747
protected static function createClient(array $kernelOptions = [], array $defaultOptions = []): Client
3848
{
39-
if (!static::$booted) {
49+
if (null === static::$alwaysBootKernel) {
50+
trigger_deprecation(
51+
'api-platform/symfony',
52+
'4.1.0',
53+
'In API Platform 5.0, the kernel will not always be booted when a new client is created (see https://github.com/api-platform/core/issues/6971).',
54+
);
55+
}
56+
57+
if (static::$alwaysBootKernel || null === static::$alwaysBootKernel) {
4058
static::bootKernel($kernelOptions);
4159
}
4260

4361
try {
4462
/**
4563
* @var Client
4664
*/
47-
$client = static::$kernel->getContainer()->get('test.api_platform.client');
65+
$client = self::getContainer()->get('test.api_platform.client');
4866
} catch (ServiceNotFoundException) {
4967
if (!class_exists(AbstractBrowser::class) || !trait_exists(HttpClientTrait::class)) {
5068
throw new \LogicException('You cannot create the client used in functional tests if the BrowserKit and HttpClient components are not available. Try running "composer require --dev symfony/browser-kit symfony/http-client".');

tests/Functional/ArrayDtoTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ final class ArrayDtoTest extends ApiTestCase
2121
{
2222
use SetupClassResourcesTrait;
2323

24+
protected static ?bool $alwaysBootKernel = false;
25+
2426
/**
2527
* @return class-string[]
2628
*/

tests/Functional/BackedEnumPropertyTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ final class BackedEnumPropertyTest extends ApiTestCase
2828
use RecreateSchemaTrait;
2929
use SetupClassResourcesTrait;
3030

31+
protected static ?bool $alwaysBootKernel = false;
32+
3133
/**
3234
* @return class-string[]
3335
*/

tests/Functional/BackedEnumResourceTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ final class BackedEnumResourceTest extends ApiTestCase
2828
{
2929
use SetupClassResourcesTrait;
3030

31+
protected static ?bool $alwaysBootKernel = false;
32+
3133
/**
3234
* @return class-string[]
3335
*/

tests/Functional/ErrorTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ final class ErrorTest extends ApiTestCase
2222
{
2323
use SetupClassResourcesTrait;
2424

25+
protected static ?bool $alwaysBootKernel = false;
26+
2527
/**
2628
* @return class-string[]
2729
*/

tests/Functional/FormatTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ final class FormatTest extends ApiTestCase
2121
{
2222
use SetupClassResourcesTrait;
2323

24+
protected static ?bool $alwaysBootKernel = false;
25+
2426
/**
2527
* @return class-string[]
2628
*/

tests/Functional/HALCircularReference.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
class HALCircularReference extends ApiTestCase
2121
{
22+
protected static ?bool $alwaysBootKernel = false;
23+
2224
public function testIssue4358(): void
2325
{
2426
$r1 = self::createClient()->request('GET', '/resource_a', ['headers' => ['Accept' => 'application/hal+json']]);

tests/Functional/HydraTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ class HydraTest extends ApiTestCase
2222
{
2323
use SetupClassResourcesTrait;
2424

25+
protected static ?bool $alwaysBootKernel = false;
26+
2527
/**
2628
* @return class-string[]
2729
*/

tests/Functional/Issues/Issue6926Test.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ class Issue6926Test extends ApiTestCase
2222
{
2323
use SetupClassResourcesTrait;
2424

25+
protected static ?bool $alwaysBootKernel = false;
26+
2527
/**
2628
* @return class-string[]
2729
*/

tests/Functional/ItemUriTemplateTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ class ItemUriTemplateTest extends ApiTestCase
2121
{
2222
use SetupClassResourcesTrait;
2323

24+
protected static ?bool $alwaysBootKernel = false;
25+
2426
/**
2527
* @return class-string[]
2628
*/

0 commit comments

Comments
 (0)