自从Symfony 4.2以来在功能测试期间不推荐使用没有根节点的树生成器

lob*_*dol 5 php gitlab-ci-runner symfony4

我正在设置一个Symfony 4.2.2应用程序,并且想要使用Gitlab-CI运行功能测试。但是我面临着这个问题:

A tree builder without a root node is deprecated since Symfony 4.2 and will not be supported anymore in 5.0.

奇怪的是,我在本地遇到此问题,但是只有在缓存重建后第一次运行单元测试时才出现。第二次运行单元测试时,不再触发错误。

我使用SENSIO /框架-EXTRA束的5.2.4版本,它应该有解决这个问题,因为说这里

此错误使我的工作每次都失败,即使所有测试都可以。

我确保Symfony\Bundle\FrameworkBundle\Test\WebTestCase在功能测试中使用该类。我还确保所有依赖项都是最新的。

这是我编写的功能测试的示例:

<?php

namespace App\Tests\Controller;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

/**
 * Class MigrationControllerTest
 *
 * @group functional
 */
class MigrationControllerTest extends WebTestCase
{
    public function testNotAllowed()
    {
        $client = static::createClient();

        $client->request('UPDATE', '/migrate');
        $this->assertEquals(405, $client->getResponse()->getStatusCode());
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的CI配置:

image: my.private.repo/images/php/7.2/symfony:latest

cache:
  paths:
    - vendor/

before_script:
  - composer install

services:
  - mysql:5.7

unit_test:
  script:
    # Set up database
    - bin/console doctrine:schema:update --env=test --force
    # Load fixtures
    - bin/console doctrine:fixtures:load --env=test --no-interaction
    # Build assets with Webpack Encore
    - npm install
    - npm run build
    # Enable xdebug for code coverage
    - docker-php-ext-enable xdebug
    # Run unit tests
    - php bin/phpunit --coverage-text --colors=never
Run Code Online (Sandbox Code Playgroud)

我希望输出显示所有通过的测试,但实际输出是:

PHPUnit 6.5.14 by Sebastian Bergmann and contributors.

Testing Project Test Suite
.2019-02-04T14:48:29+01:00 [error] Uncaught PHP Exception Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException: "No route found for "UPDATE /migrate": Method Not Allowed (Allow: GET, POST)" at /home/goulven/PhpStorm/user-balancer/vendor/symfony/http-kernel/EventListener/RouterListener.php line 143
.........                                                        10 / 10 (100%)

Time: 8.44 seconds, Memory: 52.25MB

OK (10 tests, 17 assertions)

Remaining deprecation notices (4)

  4x: A tree builder without a root node is deprecated since Symfony 4.2 and will not be supported anymore in 5.0.
    4x in MigrationControllerTest::testMigrateFail from App\Tests\Controller
Run Code Online (Sandbox Code Playgroud)

lob*_*dol 3

感谢@SergheiNiculaev,我刚刚在phpunit.xml.dist文件中添加了以下行:

<env name="SYMFONY_DEPRECATIONS_HELPER" value="weak"/>
Run Code Online (Sandbox Code Playgroud)

[编辑] 另一个解决方案是在 CI 配置文件中添加以下行:

variables:
  # ...
  SYMFONY_DEPRECATIONS_HELPER: weak
Run Code Online (Sandbox Code Playgroud)

  • 我不想无礼,但这不是一个好主意。检查该通知的真正来源并解决该问题。它不是为了惹恼你,而是告诉你在升级到下一个 Symfony 版本之前需要采取行动 (4认同)