Skip to content

Commit 6c2e431

Browse files
test: boot kernel only if it's necessary in createClient (#6976)
1 parent 95fa383 commit 6c2e431

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

src/Symfony/Bundle/Test/ApiTestCase.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,15 @@ abstract class ApiTestCase extends KernelTestCase
3636
*/
3737
protected static function createClient(array $kernelOptions = [], array $defaultOptions = []): Client
3838
{
39-
$kernel = static::bootKernel($kernelOptions);
39+
if (!static::$booted) {
40+
static::bootKernel($kernelOptions);
41+
}
4042

4143
try {
4244
/**
4345
* @var Client
4446
*/
45-
$client = $kernel->getContainer()->get('test.api_platform.client');
47+
$client = static::$kernel->getContainer()->get('test.api_platform.client');
4648
} catch (ServiceNotFoundException) {
4749
if (!class_exists(AbstractBrowser::class) || !trait_exists(HttpClientTrait::class)) {
4850
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/Symfony/Bundle/Test/ApiTestCaseTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
use ApiPlatform\Tests\SetupClassResourcesTrait;
3434
use Doctrine\ORM\EntityManagerInterface;
3535
use PHPUnit\Framework\ExpectationFailedException;
36+
use Symfony\Component\HttpKernel\KernelInterface;
3637

3738
class ApiTestCaseTest extends ApiTestCase
3839
{
@@ -386,4 +387,20 @@ public function testMissingMethod(): void
386387
self::createClient([], ['headers' => ['accept' => 'application/json']])->request('DELETE', '/something/that/does/not/exist/ever');
387388
$this->assertResponseStatusCodeSame(404);
388389
}
390+
391+
public function testDoNotRebootKernelOnCreateClient(): void
392+
{
393+
self::bootKernel();
394+
395+
$mock = $this->createMock(KernelInterface::class);
396+
397+
// Client need to be retrieved so we must configure the `getContainer`
398+
// method
399+
$mock->method('getContainer')->willReturn(self::getContainer());
400+
$mock->expects($this->never())->method('boot');
401+
402+
self::$kernel = $mock;
403+
404+
self::createClient();
405+
}
389406
}

0 commit comments

Comments
 (0)