Webtests symfony 2.5 with Fosuserbundle

I am trying to verify with fosuserbundle authentication but it still fails, I find solution for symfony 2.3 but it doesn't work

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

I will also try to create a client with two functions

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;
}

      

+3


source to share


1 answer


This is no longer the recommended way to test with an authenticated client.

The new recommended way is much simpler is to send plain old HTTP credentials and then tell the test environment firewall to authenticate with that method instead of the FOS service provider.



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

0


source







All Articles