Symfony2:无法在功能测试中模拟HTTP身份验证

jos*_*erk 4 php symfony

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

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

我遇到的唯一问题是,当尝试模拟HTTP身份验证时,就像其他控制器一样:

$client = static::createClient(array(), array(
    'PHP_AUTH_USER' => 'tester',
    'PHP_AUTH_PW'   => 'testpass',
));
Run Code Online (Sandbox Code Playgroud)

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

$crawler = $client->request('GET', '/');
Run Code Online (Sandbox Code Playgroud)

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

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

    $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
Run Code Online (Sandbox Code Playgroud)

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

我有点惊讶于什么是错的.有任何想法吗?是否对Symfony2应用了更改,这意味着此过程已更改,并且HTTP身份验证模拟现在不起作用或工作方式不同?

jos*_*erk 5

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

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'));
}
Run Code Online (Sandbox Code Playgroud)

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

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