I want to write functional tests for controllers that are protected by Symfony2 authentication mechanisms. I read a lot of tutorials describing this, but unfortunately, all of them do not work for me with the current version of Symfony2 (tested from 2.0.4 to 2.0.7)
What i have done so far:
Adding security settings to config_test.yml
security: encoders: Symfony\Component\Security\Core\User\User: plaintext providers: main: users: admin: { password: adminsmurf, roles: [ 'ROLE_USER' ] } inventory: { password: inventorysmurf, roles: [ 'ROLE_ADMIN', 'ROLE_USER', 'ROLE_INVENTORY' ] } andon: { password: andonsmurf, roles: [ 'ROLE_ADMIN', 'ROLE_ANDON' ] } firewalls: main: pattern: /.* http_basic: realm: "Secured Area" provider: main logout: true security: true stateless: true anonymous: true
Unit test
class DefaultControllerTest extends WebTestCase { public function testCorrectAuthentificationCredentials() { $client = static::createClient(); $crawler = $client->request('GET', '/inventory/index', array(), array(), array( 'PHP_AUTH_USER' => 'admin', 'PHP_AUTH_PW' => 'adminsmurf' )); $response = $client->getResponse(); $this->assertEquals(200, $response->getStatusCode()); } }
This test failed (200 expected, get 302). Can someone help what I am doing wrong?
Update 16/12
I took one more step. Since I use the form login in my regular security file. Symfony just added http_basic login to the firewall. This caused a redirect (302). I just added
form_login: false
to testing security settings. Now there is no redirection, but the result is a 401 status code.
source share