【问题标题】:Symfony 4: WebTestCase fails (Failed to start the session)Symfony 4:WebTestCase 失败(无法启动会话)
【发布时间】:2018-06-11 15:55:51
【问题描述】:

我正在尝试设置一个简单的 WebTestCase,它使用 Symfony 4(和 "phpunit/phpunit": "^6.5")向给定的 URL 发出 GET 请求。但是,测试失败:

未能启动会话,因为标头已由 \vendor\phpunit\phpunit\src\Util\Printer.php;在第 112 行。(500 内部服务器错误)

测试类:

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

控制器(包含articles路由:

/** @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()
    ));
}

framework.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 中则不然。这个测试或配置有什么问题?

【问题讨论】:

  • 好的,谢谢。但我不明白为什么这适用于 SF 3 但不使用 SF 4 而不使用例如@runInSeparateProcess 注释。这个错误发生在我的所有测试中(使用 SF 4),所以我必须用 @runInSeparateProcess 注释每个测试?我觉得有些不对劲:/

标签: php symfony phpunit


【解决方案1】:

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

session:
        storage_id: session.storage.mock_file

【讨论】:

猜你喜欢
  • 2017-04-08
  • 1970-01-01
  • 2019-04-22
  • 2019-04-17
  • 2015-10-28
  • 2017-07-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多