Testing fails with "Booting the kernel before calling "...\WebTestCase::createClient()" is not supported

Viewed 2136

Somewhere between Symfony 5.0.5 and 5.0.8 the exception

LogicException: Booting the kernel before calling "Symfony\Bundle\FrameworkBundle\Test\WebTestCase::createClient()" is not supported, the kernel should only be booted once.

is generated. In 5.05 the test shown below passes. After updating to 5.08 the test fails. While the exception appears elsewhere in SO the answer does solve the present issue. What is missing in order to allow 5.08 to pass this and similar tests?

namespace App\Tests\Controller;

use Liip\TestFixturesBundle\Test\FixturesTrait;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class AdminControllerTest extends WebTestCase
{
    use FixturesTrait;

    public function setup(): void
    {
        $this->fixtures = $this->loadFixtures([
                    'App\DataFixtures\Test\OptionsFixture',
                    'App\DataFixtures\Test\NonprofitFixture',
                    'App\DataFixtures\Test\UserFixture',
                ])
                ->getReferenceRepository();
        $this->client = static::createClient();  // <-- this line causes failure
        $this->client->followRedirects();
        $this->client->request('GET', '/login');
        $this->client->submitForm('Sign in', [
            'email' => 'admin@bogus.info',
            'password' => '123Abc',
        ]);
    }

    public function testLogin()
    {
        $this->client->request('GET', '/admin/dashboard');

        $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
    }
...
}
1 Answers

Move the line $this->client = static::createClient();

before the following block:

$this->fixtures = $this->loadFixtures([
        'App\DataFixtures\Test\OptionsFixture',
        'App\DataFixtures\Test\NonprofitFixture',
        'App\DataFixtures\Test\UserFixture',
    ])
    ->getReferenceRepository(); 

The load fixtures is booting the kernel if it's not already booted. If you boot it first then it won't do it.

It probably shouldn't have worked in Symfony 5.05 and not sure why it was passing.

Related