繁体   English   中英

PhpUnit 弃用通知:错误猜测内核目录

[英]PhpUnit deprecation notice: error guessing kernel directory

这是我的 PhpUnit 测试类:

<?php

namespace tests\AppBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

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

        ...
    }
}

但是当我尝试运行它时,我得到了一个错误,其跟踪是:

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

但问题是我不明白如何解决跟踪中指出的弃用问题......

编辑:

按照官方文档,我在 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>

然而:

1)我仍然遇到同样的错误

2) 代码http://schema.phpunit.de/4.8/phpunit.xsd显示为红色,并显示消息“URI 未注册(设置 | 语言和框架 | 模式和 DTD)。所以我去了那个地方并在“外部架构和 DTD”中,我添加了那个 URI,位置是我的项目根目录。但是,这个红色警告仍然存在,我怀疑它与位置有关,但它在哪里?

正如文档所述

要运行您的功能测试,WebTestCase 类需要知道哪个是应用程序内核来引导它。 内核类通常定义在KERNEL_CLASS环境变量中(包含在 Symfony 提供的默认 phpunit.xml.dist 文件中):

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

phpunit.xml.dist设置它使它在 symfony 3.4 中对我phpunit.xml.dist

<php>
    <!-- the value is the FQCN of the application kernel -->
    <env name="KERNEL_CLASS" value="AppKernel" />
</php>

Nikita U. 在这里是正确的,但我想指出一些可能在未来对其他人有所帮助的小事情,尤其是位置。

根据我发现的许多注释,尤其是来自 repo 的注释,这应该是<server name ... ,但我发现<env name ...也有效。 这应该位于项目根目录下phpunit.xml.distphpunit.xml文件(只能使用其中之一)的<php>块中。 如果两个文件都不存在,请创建一个(似乎可以工作)并使用 Nikita 的示例作为文本。

另外,请注意,从带有 Flex 的 Symfony 4 开始,该值是“App\\Kernel”,而不是“AppKernel”。 更多可以在这里找到: https : //github.com/symfony/symfony-standard/issues/1073

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM