3

I try make test with authentication fosuserbundle, but still is fail, i find solution for symfony 2.3 but it doesn't works

https://gist.github.com/deltaepsilon/6391565

i also try create client by two funcitons

protected function createAuthorizedClient2()
{
    $client = static::createClient();
    $container = $client->getContainer();

    $session = $container->get('session');
    /** @var $userManager \FOS\UserBundle\Doctrine\UserManager */
    $userManager = $container->get('fos_user.user_manager');
    /** @var $loginManager \FOS\UserBundle\Security\LoginManager */
    $loginManager = $container->get('fos_user.security.login_manager');
    $firewallName = $container->getParameter('fos_user.firewall_name');

    $user = $userManager->findUserBy(array('username' => 'admin'));
    $loginManager->loginUser($firewallName, $user);

    // save the login token into the session and put it in a cookie
    $container->get('session')->set('_security_' . $firewallName, serialize($container->get('security.context')->getToken()));
    $container->get('session')->save();
    $client->getCookieJar()->set(new Cookie($session->getName(), $session->getId()));

    return $client;
}

protected function createAuthorizedClient()
{
    $client = static::createClient();
    $container = static::$kernel->getContainer();
    $session = $container->get('session');
    $person = self::$kernel->getContainer()->get('doctrine')->getRepository('BergUserDataBundle:UserLogin')->findOneByUsername('admin');

    $token = new UsernamePasswordToken($person, null, 'main', $person->getRoles());
    $session->set('_security_main', serialize($token));
    $session->save();

    $client->getCookieJar()->set(new Cookie($session->getName(), $session->getId()));

    return $client;
}
1
  • 1
    Any specific error messages?
    – andy
    Sep 26, 2014 at 15:05

1 Answer 1

0

This is no longer the recommended way of testing with an authenticated client.

The new recommended way is much simpler - to submit plain old HTTP credentials and then tell your test environment firewall to authenticate via this method instead of the FOS User Provider.

See http://symfony.com/doc/current/cookbook/testing/http_authentication.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.