lundi 9 juillet 2018

Test user connected in Symfony / PhpUnit

In my controller, I use the security.token_storage service to get the user connected like :

$this->container->get('security.token_storage')->getToken()->getUser();

In my functional test, I try to log in a user to retrieve it in my controller but If I debug inside, it says me that i'm anonymous :

private function logIn()
{
    $client = static::createClient();
    $firewallName = 'main';
    $token = new UsernamePasswordToken("admin", "admin", "main", array());
    static::$kernel->getContainer()->get('security.token_storage')->setToken($token);
    $session = static::$kernel->getContainer()->get('session');
    $session->set('_security_'.$firewallName, serialize($token));
    $session->save();
    $cookie = new Cookie($session->getName(), $session->getId());
    $client->getCookieJar()->set($cookie);

    return $client;
}

How i can pass my user of my test to my main code ?

Aucun commentaire:

Enregistrer un commentaire