Since all my project resources are protected so user needs to get authenticated. In my tests I authenticate once in the setUp function. This works only for the first $client->request
, if I try another $client->request
after the first request, $client->getResponse()->getContent()
says that I'm not authenticated!!
If I call the auth function (definition's below) to get authenticated, the second request works.
So to sum up, currently I'm compelled to authenticate for every $client->request(..
.
Here is my function I'm using for authentication (How to log in User in Session within a Functional Test in Symfony 2.3?)
public function auth($username, &$client)
{
$session = $client->getContainer()->get('session');
$firewall = 'front';
$user = $this->doctrine->getRepository('FOSUserBundle:User')->findOneBy(array('username' => $username));
$token = new UsernamePasswordToken($user, null, $firewall, $user->getRoles());
$client->getContainer()->get('security.context')->setToken($token);
$session->set('_security_'.$firewall, serialize($token));
$session->save();
$cookie = new Cookie(session_name(), $session->getId());
$client->getCookieJar()->set($cookie);
}
Why do I need to authenticate for every request? Is it because somehow the session or the cookiejar generated by public function auth
are not passed to the other requests ?