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