自 Symfony 4.2 以来,在功能测试期间不推荐使用没有根节点的树生成器
Tree builder without a root node deprecated since Symfony 4.2 during functional tests
我正在设置一个 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 版本,它应该已经解决了这个问题,如 here 所述。
这个错误使我的工作每次都失败,即使所有测试都正常。
我确保在我的功能测试中使用 class 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
感谢@SergheiNiculaev,我刚刚在 phpunit.xml.dist
文件中添加了以下行:
<env name="SYMFONY_DEPRECATIONS_HELPER" value="weak"/>
[编辑]
另一种解决方案是在 CI 配置文件中添加以下行:
variables:
# ...
SYMFONY_DEPRECATIONS_HELPER: weak
如果这只是第一次发生,这意味着错误是在缓存预热期间触发的。如果您不想搜索根本原因,请将预热添加到您的 before_script
中,例如:
- php bin/console cache:warmup --env=test
这确保在执行第一个测试用例之前构建缓存容器,这样您的测试用例就不会受到容器构建问题的影响
我正在设置一个 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 版本,它应该已经解决了这个问题,如 here 所述。
这个错误使我的工作每次都失败,即使所有测试都正常。
我确保在我的功能测试中使用 class 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
感谢@SergheiNiculaev,我刚刚在 phpunit.xml.dist
文件中添加了以下行:
<env name="SYMFONY_DEPRECATIONS_HELPER" value="weak"/>
[编辑] 另一种解决方案是在 CI 配置文件中添加以下行:
variables:
# ...
SYMFONY_DEPRECATIONS_HELPER: weak
如果这只是第一次发生,这意味着错误是在缓存预热期间触发的。如果您不想搜索根本原因,请将预热添加到您的 before_script
中,例如:
- php bin/console cache:warmup --env=test
这确保在执行第一个测试用例之前构建缓存容器,这样您的测试用例就不会受到容器构建问题的影响