php - 测试失败,显示 "Booting the kernel before calling "...\WebTestCase::createClient()"不受支持

标签 php symfony testing

Symfony 5.0.5 和 5.0.8 之间的某个地方是异常(exception)

LogicException: Booting the kernel before calling "Symfony\Bundle\FrameworkBundle\Test\WebTestCase::createClient()" is not supported, the kernel should only be booted once.

生成。在 5.05 中,下面显示的测试通过了。更新到 5.08 后测试失败。而异常出现elsewhere在 SO 中,答案确实解决了当前的问题。为了让 5.08 通过这个和类似的测试,还缺少什么?

namespace App\Tests\Controller;

use Liip\TestFixturesBundle\Test\FixturesTrait;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class AdminControllerTest extends WebTestCase
{
    use FixturesTrait;

    public function setup(): void
    {
        $this->fixtures = $this->loadFixtures([
                    'App\DataFixtures\Test\OptionsFixture',
                    'App\DataFixtures\Test\NonprofitFixture',
                    'App\DataFixtures\Test\UserFixture',
                ])
                ->getReferenceRepository();
        $this->client = static::createClient();  // <-- this line causes failure
        $this->client->followRedirects();
        $this->client->request('GET', '/login');
        $this->client->submitForm('Sign in', [
            'email' => 'admin@bogus.info',
            'password' => '123Abc',
        ]);
    }

    public function testLogin()
    {
        $this->client->request('GET', '/admin/dashboard');

        $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
    }
...
}

最佳答案

移动行 $this->client = static::createClient();

在以下 block 之前:

$this->fixtures = $this->loadFixtures([
        'App\DataFixtures\Test\OptionsFixture',
        'App\DataFixtures\Test\NonprofitFixture',
        'App\DataFixtures\Test\UserFixture',
    ])
    ->getReferenceRepository(); 

load fixtures 正在引导内核,如果它还没有被引导的话。如果您先启动它,那么它不会这样做。

它可能不应该在 Symfony 5.05 中工作并且不确定它为什么会通过。

关于php - 测试失败,显示 "Booting the kernel before calling "...\WebTestCase::createClient()"不受支持,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61965329/

相关文章:

mysql - 表结构变更时的Doctrine和Mysql数据迁移

php - 在 yii2 中自动化代码接收测试

php - Apache2 升级后不执行 PHP 5.6 脚本

php - 用具体值模拟 atLeastOnce,其余的不重要

code-generation - 无法覆盖 Symfony2 GeneratorBundle 中的标准骨架 View

javascript - 事务编号只允许在支持文档级锁定的存储引擎上使用 - MongodbMemoryServer/Mochai/Chai/Supertest

android - 模拟器测试APK

php - 在 PHP 中使用分块上传 1GB 文件

javascript - strip 结帐后页面未重定向

php - 如何使用 symfony 3 在 php 中将日期属性设置为当前日期