symfony2 测试 flashbag 或会话数据

Vio*_*rel 4 phpunit symfony symfony-2.3

您如何测试 FlashBag 消息?

试过这个:

public function testInvalidLogin()
{
    $session = $this->client->getContainer()->get('session');
    $crawler = $this->client->request('GET', '/login');

    $this->assertTrue($this->client->getResponse()->isSuccessful());
    $form = $crawler->filter('form');
    $this->assertGreaterThan(0, $form->count());

    $form = $form->form();
    $this->assertNotEmpty($form);

    $form['_username'] = 'username';
    $form['_password'] = 'password';

    $this->client->submit($form);
    $this->assertTrue($this->client->getResponse()->isRedirect('http://localhost/login'));
    $this->client->followRedirect();

    $session = $this->client->getContainer()->get('session');
    var_dump($session->getBag('flashes')->all()); // this print an empty array
}
Run Code Online (Sandbox Code Playgroud)

登录控制器设置了一条闪存消息“凭据错误”,但在测试期间我看不到它。

Cyp*_*ian 5

可能是因为在您重定向后,您正在弹出 Flash 消息,例如。在您的模板中的某处。Flash bag 容器在您调用get方法后立即删除 Flash 消息(要指定 - 在 get 方法中实现删除...)。如果您只想获取消息而不弹出它,您应该使用peek方法。

我想如果你var_dumpfollowRedirect那之前搬家,你会得到你期望的结果。