Symfony2:无法在功能测试中模拟 HTTP 身份验证 [英] Symfony2: Unable to simulate HTTP authentication in functional test

查看:34
本文介绍了Symfony2:无法在功能测试中模拟 HTTP 身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 symfony.com 上描述的以下技术:http://symfony.com/doc/current/cookbook/testing/http_authentication.html试图对需要用户登录的控制器进行功能测试.

I'm trying to use the following technique described on symfony.com : http://symfony.com/doc/current/cookbook/testing/http_authentication.html in an attempt to functionally test a controller that requires a user to be logged in.

到目前为止,我的登录表单正在运行,我可以登录,并且 Symfony2 调试 Web 工具栏显示我的用户已通过身份验证.另外,我已经为登录过程本身编写了一个功能测试,这通过了.所以我现在通过两个场景,我的登录正在工作.

So far, my login form is working, I can login, and the Symfony2 debug web toolbar is showing my user as authenticated. Also, I have already written a functional test for the login process itself, this passes. So I now by two scenarios, that my login is working.

我遇到的唯一问题是在尝试为其他控制器模拟 HTTP 身份验证时:

The only problem I'm having, is when trying to simulate HTTP authentication like so for other controllers:

$client = static::createClient(array(), array(
    'PHP_AUTH_USER' => 'tester',
    'PHP_AUTH_PW'   => 'testpass',
));

我可以通过检查 $client 看到,我正在被重定向到我的登录页面,当我尝试这样的事情时:

I can see by inspecting the $client, that I am being redirected to my login page, the moment I try something like this:

$crawler = $client->request('GET', '/');

我知道数据库中存在密码为 testpass 的用户 tester,因为我也可以通过浏览器使用该帐户登录.

I know that the user tester with password testpass exists in the DB, as I can login with that account via browser as well.

我可以使用安全控制器测试中的以下代码:

I can use the following code from the security controller test:

    $client = $this->createClient(array(), array('HTTP_HOST' => 'myapp.test'));

    // Visit user login page and login
    $crawler = $client->request('GET', '/login');
    $form = $crawler->selectButton('Login')->form();
    $crawler = $client->submit($form, array('_username' => 'tester', '_password' => 'testpass'));

    // ... carry on with remainder of tests

但我不太确定这是否是最有效的方法.

but I'm not too sure if this is the most efficient way to do this.

我有点不知所措.有任何想法吗?是否对 Symfony2 进行了更改,这意味着此过程已更改并且 HTTP 身份验证模拟现在不起作用或工作方式有所不同?

I'm a bit stunned as to what is wrong. Any ideas? Was there a change applied to Symfony2 that means that this process has changed and the HTTP authentication simulation now doesn't work or works differently?

推荐答案

考虑一下,我可能只是使用以下 setUp 方法进行登录:

Thinking about it, I might just do the login using the following setUp method:

public function setUp()
{
    // Perform user login.
    $this->client = $this->createClient(array(), array('HTTP_HOST' => 'scire.test'));
    $crawler = $this->client->request('GET', '/login');
    $form = $crawler->selectButton('Login')->form();
    $this->client->submit($form, array('_username' => 'tester', '_password' => 'tester'));
}

HTTP 身份验证在这里不起作用,除非我使用一些安全设置更改我的 config_test.yml 以允许 HTTP 身份验证.

The HTTP authentication won't work here, unless I alter my config_test.yml with some security setup to allow HTTP authentication.

自我注意:HTTP 身份验证与使用 Doctrine 用户提供程序不同!!!

Note to self: HTTP authentication is different from using a Doctrine user provider!!!

这篇关于Symfony2:无法在功能测试中模拟 HTTP 身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆