I want to prepare simple function test in PHPUnit for my symfony 2 project: authenticate user and display some page without redirecting to login form. I use LDAP user provider (IMAG\LdapBundle\Provider\LdapUserProvider). My firewalls configuration in security.yml:
firewalls:
secured_area:
pattern: ^/
provider: ldap
imag_ldap:
login_path: /login
logout:
path: /logout
target: /
In my test controller I have following methods:
protected function createClientWithAuthentication($firewallName, array $options = array(), array $server = array())
{
$client = $this->createClient($options, $server);
$client->getCookieJar()->set(new \Symfony\Component\BrowserKit\Cookie(session_name(), true));
$user = $this->getCurrentUser($client);
$token = new UsernamePasswordToken($user, null, $firewallName, $user->getRoles());
$client->getContainer()->get('session')->set('_security_' . $firewallName, serialize($token));
return $client;
}
protected function getCurrentUser($client) {
$userProvider = $client->getContainer()->get('imag_ldap.security.user.provider');
$user = $userProvider->loadUserByUsername('my.login');
return $user;
}
Then I have simple test:
public function testProfile()
{
$c = $this->createClientWithAuthentication('secured_area');
$c->request('GET', '/profile');
$this->assertEquals($c->getResponse()->getStatusCode(), 200);
}
Test fails:
Failed asserting that 200 matches expected 302.
I digged it and I found out that I receive complete user from user provider (all fields are properly filled). But response is redirecting to localhost/login page.
Please tell me what else I can test... where is the place where decision is made if login page should (or not) be displayed?