This is the tricky issue discussed here: https://github.com/symfony/symfony/issues/5228 Although it's 2.1, I still happen to 2.2.
This is how I perform a test check:
// Create a new client to browse the application $client = static::createClient(); $client->getCookieJar()->set(new Cookie(session_name(), true)); // dummy call to bypass the hasPreviousSession check $crawler = $client->request('GET', '/'); $em = $client->getContainer()->get('doctrine')->getEntityManager(); $user = $em->getRepository('MyOwnBundle:User')->findOneByUsername('username'); $token = new UsernamePasswordToken($user, $user->getPassword(), 'main_firewall', $user->getRoles()); self::$kernel->getContainer()->get('security.context')->setToken($token); $session = $client->getContainer()->get('session'); $session->set('_security_' . 'main_firewall', serialize($token)); $session->save(); $crawler = $client->request('GET', '/login/required/page/'); $this->assertTrue(200 === $client->getResponse()->getStatusCode()); // perform tests in the /login/required/page here..
Oh, and usage statements:
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; use Symfony\Bundle\FrameworkBundle\Client; use Symfony\Component\BrowserKit\Cookie;
ihsan source share