【问题标题】:How to make functional tests that require to be authenticated, in Symfony 2?如何在 Symfony 2 中进行需要验证的功能测试?
【发布时间】:2011-07-17 23:58:32
【问题描述】:

我找不到任何关于此的文档。

我正在使用客户端对象向 /login 发送请求,填写表单并提交。这很好用,但我收到了对 /login 的 302 响应,好像凭据不正确一样。

无论如何,我认为在第一次请求之后会话表中应该至少有一行,但没有。这怎么可能?

有什么想法吗?

编辑:这是代码:

    // Go to login page
    $client = $this->createClient();
    $crawler = $client->request('GET', '/login');
    $this->assertTrue($crawler->filter('html:contains("Username")')->count() > 0);

    // Fill in the form and submit it
    $form = $crawler->selectButton('login')->form();
    $form['_username'] = 'admin';
    $form['_password'] = 'admin';
    $client->submit($form);
    $this->assertEquals(302,$client->getResponse()->getStatusCode());
    $this->assertFalse($client->getResponse()->isRedirect('http://localhost/login'));

最后一个断言失败

【问题讨论】:

  • 你能显示运行这个测试的代码部分吗?
  • @Ibu 当然,你来了
  • 你最终通过了这个测试吗?我知道这是一个很长的机会,假设你会记得,但也许......?

标签: php testing symfony


【解决方案1】:

您应该使用 http 基本身份验证创建一个新的防火墙:

安全: ... 防火墙: 功能测试: 模式:/安全/.* 无状态:真 http_basic: 提供者:提供者名称 ...

然后像这样创建一个客户端:

$client = $this->createClient(array(), array(
    'PHP_AUTH_USER' => 'username',
    'PHP_AUTH_PW' => 'password',
));

编码愉快!

【讨论】:

    【解决方案2】:

    到目前为止,官方文档包括文章How to simulate Authentication with a Token in a Functional Test。可能对某人有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-12
      • 2015-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多