I use FOSUserBundle
for my User Authentication
I have a controller, let's call it adminController
which is reserved for User granted User::ADMIN_ROLE
Everything works fine but I have an error when I try to write my functional Test
Inside my AdminControllerTest
I have a method that try to test a page that need User::ADMIN_ROLE
My testAdminAccess()
method
public function testAdminAccess()
{
$session = $this->client->getContainer()->get('session');
// the firewall context defaults to the firewall name
= 'main';
$user = $this->getUserByUsername('admin@yopmail.com');
$token = new UsernamePasswordToken($user, null, $firewallContext, $user->getRoles());
$session->set('_security_'.$firewallContext, serialize($token));
$session->save();
$cookie = new Cookie($session->getName(), $session->getId());
$this->client->getCookieJar()->set($cookie);
$this->client->followRedirects();
$crawler = $this->client->request(
'GET',
'http://localhost/admin'
);
dump($crawler);
}
I'm always redirected to my login page
How can I keep the session to access some page that's protected by a specific Role?
What I'm already tried:
- http://kristiankaa.dk/symfony-authentication-controller-testing
- How to log in User in Session within a Functional Test in Symfony 2.3?
- https://symfony-docs-zh-cn.readthedocs.io/cookbook/testing/simulating_authentication.html
- https://symfony.com/doc/2.6/cookbook/testing/simulating_authentication.html
- How to programmatically login/authenticate a user?
I'm using Symfony version 3.4