首页 > 解决方案 > 自 Symfony 4.2 在功能测试期间不推荐使用没有根节点的树构建器

问题描述

我正在设置一个 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/framework-extra-bundle 的 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());
    }
}

这是我的 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

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

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

标签: phpsymfony4gitlab-ci-runner

解决方案


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

<env name="SYMFONY_DEPRECATIONS_HELPER" value="weak"/>

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

variables:
  # ...
  SYMFONY_DEPRECATIONS_HELPER: weak

推荐阅读