PhpUnit deprecation notice: error guessing kernel directory

9.1k views Asked by At

This is my PhpUnit test class:

<?php

namespace tests\AppBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class SendEmailControllerTest extends WebTestCase
{
    public function testMailIsSentAndContentIsCorrect()
    {
        $client = static:: createClient();

        ...
    }
}

But when I try to run it, I get the error whose trace is:

Unable to guess the Kernel directory.
 C:\myProject\vendor\symfony\symfony\src\Symfony\Bundle\FrameworkBundle\Test\KernelTestCase.php:62
 C:\myProject\vendor\symfony\symfony\src\Symfony\Bundle\FrameworkBundle\Test\KernelTestCase.php:138
 C:\myProject\vendor\symfony\symfony\src\Symfony\Bundle\FrameworkBundle\Test\KernelTestCase.php:184
 C:\myProject\vendor\symfony\symfony\src\Symfony\Bundle\FrameworkBundle\Test\KernelTestCase.php:165
 C:\myProject\vendor\symfony\symfony\src\Symfony\Bundle\FrameworkBundle\Test\WebTestCase.php:33
 C:\myProject\tests\AppBundle\Controller\SendEmailControllerTest.php:12

ERRORS!
Tests: 1, Assertions: 0, Errors: 1.

Remaining deprecation notices (3)

  1x: Using the KERNEL_DIR environment variable or the automatic guessing based on the phpunit.xml / phpunit.xml.dist file location is deprecated since Symfony 3.4. Set the KERNEL_CLASS environment variable to the fully-qualified class name of your Kernel instead. Not setting the KERNEL_CLASS environment variable will throw an exception on 4.0 unless you override the :createKernel() or :getKernelClass() method.
    1x in SendEmailControllerTest::testMailIsSentAndContentIsCorrect from tests\AppBundle\Controller

  1x: The Symfony\Bundle\FrameworkBundle\Test\KernelTestCase::getPhpUnitXmlDir() method is deprecated since Symfony 3.4 and will be removed in 4.0.
    1x in SendEmailControllerTest::testMailIsSentAndContentIsCorrect from tests\AppBundle\Controller

  1x: The Symfony\Bundle\FrameworkBundle\Test\KernelTestCase::getPhpUnitCliConfigArgument() method is deprecated since Symfony 3.4 and will be removed in 4.0.
    1x in SendEmailControllerTest::testMailIsSentAndContentIsCorrect from tests\AppBundle\Controller
C:\xampp\php\php.exe C:/myProject/vendor/phpunit/phpunit/phpunit --no-configuration tests\AppBundle\Controller\SendEmailControllerTest C:\myProject\tests\AppBundle\Controller\SendEmailControllerTest.php --teamcity
Testing started at 23:09 ...
PHPUnit 5.6.0 by Sebastian Bergmann and contributors.


Process finished with exit code 2

But the problem is that I don't understand how to fix the deprecation issues indicated in the trace...

Edit:

Following the official documentation, I've added the in the phpunit.xml.dist

<?xml version="1.0" encoding="UTF-8"?>

<!-- https://phpunit.de/manual/current/en/appendixes.configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.8/phpunit.xsd"
         backupGlobals="false"
         colors="true"
         bootstrap="vendor/autoload.php"
>
    <php>
        <ini name="error_reporting" value="-1" />
        <env name="KERNEL_CLASS" value="App\Kernel" />
        <server name="KERNEL_CLASS" value="AppKernel" />
    </php>

    <testsuites>
        <testsuite name="Project Test Suite">
            <directory>tests</directory>
        </testsuite>
    </testsuites>

    <filter>
        <whitelist>
            <directory>src</directory>
            <exclude>
                <directory>src/*Bundle/Resources</directory>
                <directory>src/*/*Bundle/Resources</directory>
                <directory>src/*/Bundle/*Bundle/Resources</directory>
            </exclude>
        </whitelist>
    </filter>
</phpunit>

However:

1) I still get the same error

2) The code http://schema.phpunit.de/4.8/phpunit.xsd appears in red, with the message "URI is not registered (Settings | Languages & Frameworks | Schemas and DTDs). So I went to that place and in "External Schemas and DTDs" I added that URI, and the location being my project root. However, this red warning persists and I suspect it is related to the location, but where is it?

3

There are 3 answers

0
Apolymoxic On

Nikita U. is correct here, but I wanted to point out a couple small things that might help someone else in the future, especially location.

According to the many of the notes I have found, especially the comments from the repo, this should be <server name ..., but I have found that <env name ... works, too. This should be located in the <php> block of the phpunit.xml.dist or phpunit.xml files (only one of them should be used) in the root directory of the project. If neither file is there, create one (either seem to work)and use Nikita's example for the text.

Also, note that as of Symfony 4 with Flex, the value is "App\Kernel", not "AppKernel". More can be found here: https://github.com/symfony/symfony-standard/issues/1073

1
Plamen Vasilev On

Setting this in phpunit.xml.dist make it work for me with symfony 3.4

<php>
    <!-- the value is the FQCN of the application kernel -->
    <env name="KERNEL_CLASS" value="AppKernel" />
</php>
0
Nikita U. On

As documentation states

To run your functional tests, the WebTestCase class needs to know which is the application kernel to bootstrap it. The kernel class is usually defined in the KERNEL_CLASS environment variable (included in the default phpunit.xml.dist file provided by Symfony):

<?xml version="1.0" charset="utf-8" ?>
<phpunit>
    <php>
        <!-- the value is the FQCN of the application kernel -->
        <env name="KERNEL_CLASS" value="App\Kernel" />
    </php>
    <!-- ... -->
</phpunit>