Symfony 4:WebTestCase失败(无法启动会话)

时间:2018-01-01 19:25:46

标签: php symfony phpunit

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

  

由于已经发送了标头,因此无法启动会话   \厂商\ 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中没有。这个测试或配置有什么问题?

1 个答案:

答案 0 :(得分:3)

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

session:
        storage_id: session.storage.mock_file