I'm doing integration testing of Symfony2 controllers, inheriting my test classes from this:
class InsecureWebTestCase extends WebTestCase {
protected $client = null;
public function setUp() {
$this->client = static::createClient();
$session = $this->client->getContainer()->get('session');
$firewall = 'default';
$token = new UsernamePasswordToken(
'norbert.scrunge@gmail.com',
null,
$firewall,
array('ROLE_USER', 'ROLE_ADMIN')
);
// $this->client->getContainer()->get('security.context')->setToken($token);
$session->set("_security_$firewall", serialize($token));
$session->save();
$cookie = new Cookie($session->getName(), $session->getId());
$this->client->getCookieJar()->set($cookie);
}
}
If I use the controller as part of the app:
$this->container->get('security.token_storage')->getToken()->getUser()
and $this->getUser()
are instances of my Doctrine "user" entity.
But when running integration tests:
$this->container->get('security.token_storage')->getToken()->getUser()
is a string containing the user name and $this->getUser()
is NULL
.
What do I need to do to make behaviour consistent in my app and functional tests?