Symfony2 functional test authentication Symfony2 functional test authentication symfony symfony

Symfony2 functional test authentication


Either use:

$crawler = $client->followRedirect();

or make that client always redirects:

$client->followRedirects();

Related doc: http://symfony.com/doc/current/book/testing.html#redirecting


You should be able to do the following:

1) 'browse' to the page

$client = static::createClient();$crawler = $client->request('GET', '/login');

2) Select the form via the submit button

$buttonCrawlerNode = $crawler->selectButton('submit');

3) Pass the login credentials as data and submit the form

$form = $buttonCrawlerNode->form();$data = array('username' => 'u@u.com','password' => 'pass');$client->submit($form,$data);

4) Follow the redirect

$crawler = $client->followRedirect();

5) At this point you should be able to check the response code

$this->assertEquals(302, $client->getResponse()->getStatusCode());

or access a secured page

$crawler = $client->request('GET', '/dashboard');//do other stuff