PHPUnit Symfony v3.4 继承已弃用

PHPUnit Symfony v3.4 inheritance is deprecated

升级到 Symfony v3.4 后。我在 PHPUnit 测试中不断收到以下错误。

Remaining deprecation notices (1)

1x: Bundle inheritance is deprecated as of 3.4 and will be removed in 4.0. 1x in GeoTest::testDistanceDuration from Tests\AppBundle\Util\Geo

以下是测试文件

namespace Tests\AppBundle\Util\Geo;

use AppBundle\Util\Geo\GeoException;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class GeoTest extends WebTestCase {
    public function testDistanceDuration ()
    {
        $client = $this->createClient();

        $geo = $client->getContainer()->get('geo');

        $returnData = $geo->getDistanceDuration([51, -0.34], [51, -0.11]);

        $this->assertArrayHasKey('distance', $returnData);
        $this->assertArrayHasKey('duration', $returnData);
        $this->assertGreaterThan(0, $returnData['distance']);
        $this->assertGreaterThan(0, $returnData['duration']);
    } 
}

我该如何解决这个问题?

已编辑,另一个简单的测试未通过并给出错误。

public function testShowPost()
{
    $client = static::createClient(); // < this is causing the error

    $this->assertEquals(200, 200);
}

它不完全是错误,即使它显示为红色背景。是一个通知,但是你的测试通过了。

根据Symfony 3.4 Bundle Inheritance documentation:

Bundle inheritance is deprecated since Symfony 3.4 and will be removed in 4.0.

所以也许按照您自己的逻辑,您扩展了另一个包。您应该检查您的 AppBundle 或任何您拥有的东西。或者您使用第三方捆绑包(与 Composer 一起安装),其功能相同。

您有 2 个解决方案:

  1. 通过更新您自己的逻辑或更新导致此弃用的第三方包来解决弃用问题。如果这个包没有任何更新,你应该考虑停止使用它。
  2. 你应该使用 PHPUnit Bridge to silent this deprecation notice if you don't want to fix it right now (at your own risks obviously). Especially by Marking the Test as Legacy

编辑:

but I found the error is caused by ` $client = static::createClient();

还要检查您是否在 phpunit.xml.dist:

中为内核设置了正确的路径
<?xml version="1.0" charset="utf-8" ?>
<phpunit>
    <php>
        <server name="KERNEL_DIR" value="/path/to/your/app/" />
    </php>
    <!-- ... -->
</phpunit>

眼见为实.

让我们假设弃用消息听起来像这样:

Bundle inheritance is deprecated as of 3.4 and will be removed in 4.0.

这是什么意思?这可能意味着 you/someone 已经创建了一个覆盖 getParent() 函数的包(例如 MyBundle),如下所示:

class MyBundle extends Bundle
{
    /**
    * Returns the bundle parent name.
    *
    * @return string|null The Bundle parent name it overrides or null if no parent
    */
    public function getParent()
    {
        return 'Some3rdPartyBundleName';
    }
...

从 Symfony 4.0 开始不再支持此功能。因此,只需删除此实现,弃用警告就会停止。

但是 why have we used bundle inheritance 直到 Symfony 4,无论如何?

This inheritance mechanism was traditionally used to override some templates, controllers and other elements of third-party bundles. In Symfony 4.0 you'll need to use alternative solutions to override those elements.