How to register a user in a session in a functional test in Symfony 2.3?

I read a lot of stackoverflow posts about this. But most of the methods are not useful in Symfony 2.3. So I tried manually entering the system manually to do some actions in the background. Here is my security.yml

security: ... role_hierarchy: ROLE_SILVER: [ROLE_BRONZE] ROLE_GOLD: [ROLE_BRONZE, ROLE_SILVER] ROLE_PLATINUM: [ROLE_BRONZE, ROLE_SILVER, ROLE_GOLD] ROLE_ADMIN: [ROLE_BRONZE, ROLE_SILVER, ROLE_GOLD, ROLE_PLATINUM, ROLE_ALLOWED_TO_SWITCH] providers: database: entity: { class: Fox\PersonBundle\Entity\Person, property: username } firewalls: dev: pattern: ^/(_(profiler|wdt)|css|images|js)/ security: false login: pattern: ^/person/login$ security: false main: pattern: ^/ provider: database form_login: check_path: /person/login-check login_path: /person/login default_target_path: /person/view always_use_default_target_path: true logout: path: /person/logout target: / anonymous: true access_control: - { path: ^/, roles: IS_AUTHENTICATED_ANONYMOUSLY } - { path: ^/person/registration, roles: IS_AUTHENTICATED_ANONYMOUSLY } - { path: ^/person, roles: ROLE_BRONZE } 

Here is my test:

 class ProfileControllerTest extends WebTestCase { public function setUp() { $kernel = self::getKernelClass(); self::$kernel = new $kernel('dev', true); self::$kernel->boot(); } public function testView() { $client = static::createClient(); $person = self::$kernel->getContainer()->get('doctrine')->getRepository('FoxPersonBundle:Person')->findOneByUsername('master'); $token = new UsernamePasswordToken($person, $person->getPassword(), 'main', $person->getRoles()); self::$kernel->getContainer()->get('security.context')->setToken($token); self::$kernel->getContainer()->get('event_dispatcher')->dispatch( AuthenticationEvents::AUTHENTICATION_SUCCESS, new AuthenticationEvent($token)); $crawler = $client->request('GET', '/person/view'); } 

And when I run this test, the method $person = $this->get(security.context)->getToken()->getUser(); does not work in the test controller. Let's say if in the controller call $person->getId(); I will have the error Call to a member function getId() on a non-object in...

So, can you correctly specify the way a user enters a functional test in Symfony 2.3?

Thanks!

EDIT_1 : If I changed Symfony/Component/Security/Http/Firewall/ContextListener.php and commented on one line:

 if (null === $session || null === $token = $session->get('_security_'.$this->contextKey)) { // $this->context->setToken(null); return; } 

all tests continue without errors.

EDIT_2 : This is the link I'm trying to use: first second third fourth fifth fifth sixth seventh eighth nineth

+4
source share
2 answers

Finally I solve it! This is an example of working code:

 use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; use Symfony\Component\BrowserKit\Cookie; class ProfileControllerTest extends WebTestCase { protected function createAuthorizedClient() { $client = static::createClient(); $container = static::$kernel->getContainer(); $session = $container->get('session'); $person = self::$kernel->getContainer()->get('doctrine')->getRepository('FoxPersonBundle:Person')->findOneByUsername('master'); $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; } public function testView() { $client = $this->createAuthorizedClient(); $crawler = $client->request('GET', '/person/view'); $this->assertEquals( 200, $client->getResponse()->getStatusCode() ); } 

Hope this helps save you time and nerves;)

+10
source

In addition to the decision I will show my user login function in the controller .

 // <!-- Symfony 2.4 --> // use Symfony\Component\Security\Core\AuthenticationEvents; use Symfony\Component\Security\Core\Event\AuthenticationEvent; use Symfony\Component\Security\Http\Event\InteractiveLoginEvent; use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; private function loginUser(UsernamePasswordToken $token, Request $request) { $this->get('security.context')->setToken($token); $s = $this->get('session'); $s->set('_security_main', serialize($token)); // `main` is firewall name $s->save(); $ed = $this->get('event_dispatcher'); $ed->dispatch( AuthenticationEvents::AUTHENTICATION_SUCCESS, new AuthenticationEvent($token) ); $ed->dispatch( "security.interactive_login", new InteractiveLoginEvent($request, $token) ); } 
+1
source

All Articles