Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony 4 phpunit : KERNEL_CLASS env variable error

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 !

like image 736
Rayhan Avatar asked Jan 21 '26 08:01

Rayhan


2 Answers

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>
[...]
like image 108
Vincent PHILIPPE Avatar answered Jan 23 '26 20:01

Vincent PHILIPPE


<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.

like image 30
Rayan arnel Avatar answered Jan 23 '26 20:01

Rayan arnel