Symfony 4 phpunit : KERNEL_CLASS env variable error
Asked Answered
T

3

14

I'm trying to learn how to make phpunit by testing the User class made by "make:user" but i'm facing this problem when extending "KernelTestCase" and run the test :

LogicException: You must set the KERNEL_CLASS environment variable to the fully-qualified class name of your Kernel in phpunit.xml / phpunit.xml.dist or override the "App\tests\Entity
UsersTest::createKernel()" or "App\tests\Entity\UsersTest::getKernelClass()" method.

Here is my test, i'm trying to test an Entity :

I'm trying to fix it since 2 days with my friend Google but I didn't found any solution. Can you help me ? Thank you guys !

Tacitus answered 15/2, 2021 at 20:11 Comment(0)
B
32

I just got the same problem.

The solution is to update your ./phpunit.xml.dist file to set the KERNEL_CLASS environment variable :

[...]
    <php>
        <ini name="error_reporting" value="-1" />
        <server name="APP_ENV" value="test" force="true" />
        <server name="SHELL_VERBOSITY" value="-1" />
        <server name="SYMFONY_PHPUNIT_REMOVE" value="" />
        <server name="SYMFONY_PHPUNIT_VERSION" value="7.5" />
        <server name="KERNEL_CLASS" value="App\Kernel" /> <--- SET KERNEL_CLASS AT THIS LINE
    </php>
[...]
Banyan answered 12/3, 2021 at 11:18 Comment(0)
B
5

In my case the tests/bootstrap.php file was not in sync with the one of the phpunit-bridge recipe. The recipe was not executed because I updated Symfony 3.4 (without flex) to Symfony 4.4.

After installing the recipe with composer recipes:install symfony/phpunit-bridge --force -v the following files were overridden:

  • .env.test
  • phpunit.xml.dist
  • tests/bootstrap.php

Now the .env.test gets loaded and the KERNEL_CLASS env variable, which is defined there, is respected correctly.

Broderic answered 31/1, 2022 at 10:11 Comment(0)
T
5
<php>
    <env name="KERNEL_CLASS" value="App\Kernel" />
</php>

In addition to the solution mentioned by other individuals, it is necessary for the file to be considered in order to use the environment variables from the "phpunit.xml.dist" file.

For example, in PhpStorm, we need to target it in Settings -> Test Frameworks and define the path of phpunit.xml.dist in the "Default configuration file".

Now, you need to find out how to link the phpUnit.xml.dist file with your IDE or CLI.

Twain answered 22/11, 2023 at 10:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.