gpt4 book ai didi

php - 如何在 Symfony 2 中进行需要验证的功能测试?

转载 作者:可可西里 更新时间:2023-11-01 13:28:49 25 4
gpt4 key购买 nike

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

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

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

有什么想法吗?

编辑:这是代码:

    // 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'));

最后一个断言失败

最佳答案

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

security:    ...    firewalls:        functional_test:            pattern: /secure/.*            stateless: true            http_basic:                provider: provider_name    ...

Then create a client like this:

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

编码愉快!

关于php - 如何在 Symfony 2 中进行需要验证的功能测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6727579/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com