Mike
Mike

Reputation: 6934

Symfony 3 functional test: authenticate user of own User class

I'd like to run functional tests on a section of my website which requires authentication. I found a solution for Symfony 2.x here. However, this does not work for Symfony3 as this line is now deprecated:

self::$kernel->getContainer()->get('security.context')->setToken($token);

My question is, how do I go around this and make it work with Symfony3? Thank you.

Upvotes: 0

Views: 521

Answers (1)

xabbuh
xabbuh

Reputation: 5881

In Symfony 3, the SecurityContext was split into the TokenStorage and the AuthorizationChecker. Thus, you need to use the security.token_storage service:

self::$kernel->getContainer()->get('security.token_storage')->setToken($token);

However, a simpler approach would be to switch to HTTP Basic auth in your tests and configure the logged in user as described in http://symfony.com/doc/current/cookbook/testing/http_authentication.html.

Upvotes: 1

Related Questions