php - 交响乐 4 : WebTestCase fails (Failed to start the session)

标签 php symfony phpunit

我正在尝试设置一个简单的 WebTestCase,它使用 Symfony 4(和 "phpunit/phpunit": "^6.5")。但是,测试失败:

Failed to start the session because headers have already been sent by \vendor\phpunit\phpunit\src\Util\Printer.php; at line 112. (500 Internal Server Error)

测试类:

class ControllerTest extends WebTestCase
{
    public function testGetArticles()
    {
        $client = static::createClient();
        $client->request('GET', '/articles');
        $this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode());
    }
}

Controller (包含文章路由:

/** @Route(path="articles", name="article_list") */  
public function article_list(Request $request)
{
    return $this->render("article/list.html.twig", array(
        "articles" => $this->entityManager->getRepository("App:Article")->getArticles()
    ));
}

框架.yaml:

framework:
    secret: '%env(APP_SECRET)%'
    csrf_protection: ~
    session:
        # With this config, PHP's native session handling is used
        handler_id: ~

这些测试在 Symfony 3 中运行良好,在 SF 4 中则不然。这个测试或配置有什么问题?

最佳答案

您必须取消注释 config\Packages\test\framework.yaml 中的 session 部分。

session:
        storage_id: session.storage.mock_file

关于php - 交响乐 4 : WebTestCase fails (Failed to start the session),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48052577/

相关文章:

javascript - 使用 AJAX 在 codeigniter 中更新表单模态 Bootstrap

php - PDO 包装器返回 NULL

php - 从 MySQL 数据库中选择复杂范围

symfony - 尝试将 FOSUserBundle 安装到 Symfony2 时遇到问题

php - 使用 yaml 路由描述时如何在运行时获取 Symfony2 中的路由名称?

PHPUnit - 试图@cover 不存在的方法

php - 是否可以将 javascript 值分配给 php 变量?

symfony - FOS UserBundle -> 无法创建新用户

phpunit 您无法序列化或反序列化 PDO 实例

php - 使用 PHPUnit 进行单元或集成测试(私有(private)方法依赖项)