1

I have implemented a listener on the security.interactive_login event to set one session variable that is used all over my app, as advised in Symfony2: After successful login event, perform set of actions. The problem is that the security.interactive_login event is not fired in functional tests (I guess this is logical since there is no actual interactive login). Given this restriction: How can I run functional tests with session variables set upon user login?

Community
  • 1
  • 1
Alberto Gaona
  • 2,427
  • 25
  • 20

2 Answers2

1

If u use basic authentication you can eventualy manualy fire event handler for testing but i think more proper way is to log in to app throu login form with submiting it with client - event listener should be fired that way.

mrMantir
  • 2,285
  • 1
  • 17
  • 20
  • Manually firing would be nice. I prefer doing tricks in tests to adapt to app instead of tricking the app to adapt to tests. Can you point me to a code sample where such firing is performed? Thanks in advance. – Alberto Gaona Oct 31 '12 at 22:53
  • 1
    @AlbertoGaona I mention manual firing event just to show that you can do it but proper way is to login with your test client. Im doing that successfuly myself for testing login events :) For manual firing events you can adapt code from [here](https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Security/Http/Firewall/AbstractAuthenticationListener.php#L209). – mrMantir Nov 05 '12 at 14:51
  • 1
    one more thing - this dispatcher is a service in container named 'event_dispatcher' – mrMantir Nov 05 '12 at 14:55
  • Throwing away the basic authentication does the trick. My test looks awful, though – Alberto Gaona Nov 05 '12 at 18:29
0

Just to bring detail on the workaround:

In the functional test case I changed

$client = static::createClient(array(), array(
    'PHP_AUTH_USER'=>'alberto'
    ,'PHP_AUTH_PW'=>'********'
    )
);

For

$client = static::createClient();
$crawler = $client->request('GET','/secured/login');
$form = $crawler->selectButton('Aceptar')->form();
$form['_username'] = 'alberto';
$form['_password'] = '********';
$crawler = $client->submit($form);      
Alberto Gaona
  • 2,427
  • 25
  • 20