4

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' => '[email protected]',
            'password' => '123Abc',
        ]);
    }

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

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

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks. Moving the block allowed tests that do not require any of the fixtures to pass. Unfortunately, other tests within the test class fail as it appears the fixtures are not loaded.
Reading the Liip documentation again I get the impression that the client & fixtures code should be in each test rather than in setup().
Answer is accepted as it resolved the question as presented. There remains other problems created by the answer though, but that's a different issue.
The setup() should be called before every test so I doubt that would be the issue.
Maybe overwrite the tear down method so the fixtures are not removed, run one of the tests and see if the database is populated.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.