3
votes

who can help me with authentication in php functional test. I make authorization and all my controller tests failed because they don't have get nothing after localhost/ because need roles ROLE_USER.

I don't know how i can it in controller tests. My authorization in steps looks like this put in input form email and submit enter if this email exist in db create session with user entities and redirect.

But how emulate in test it?

For example part of my test.

public function testUnlinkWatcher()
{
    $client = static::createClient();
    $client->request('POST', 'project/client/unlink_watcher');


    $this->assertTrue($client->getResponse()->isSuccessful()); // this false because access denied

}

I try to do with 2 example, but me don't work. What can to do?

private $client = null;

public function setUp()
{
    $this->client = static::createClient();
}

private function logIn()
{
    $session = $this->client->getContainer()->get('session');

    $firewall = 'secured_area';
    $token = new UsernamePasswordToken('login', null, $firewall, array('ROLE_USER'));
    $session->set('_security_'.$firewall, serialize($token));
    $session->save(); // It's here object session with roles
}

public function testUnlinkWatcher()
    {

        $this->logIn();                           
        $this->client->request('POST', 'testProject/client/unlink_watcher');

        var_dump($client->getResponse()->isSuccessful());die; //false

My security

security:

providers:
    in_memory:
        memory: ~

    hwi:
        entity: { class: Test\testProjectBundle\Entity\User }

firewalls:
    dev:
        pattern: ^/(_(profiler|wdt|error)|css|images|js)/
        security: false

    secured_area:

        anonymous: ~

        oauth:
            resource_owners:
                google:             "/login/check-google"
                facebook:           "/login/check-facebook"
            login_path:        /
            failure_path:      /


            oauth_user_provider:
                service: testProjectbundle.oauth_provider

        logout: 
            path: /logout


access_control:
    - { path: ^/testProject(.+), roles: ROLE_USER }

session dump in logIn method :

    Symfony\Component\HttpFoundation\Session\Session {#377
  #storage: Symfony\Component\HttpFoundation\Session\Storage\MockFileSessionStorage {#376
    -savePath: "/var/www/integra/app/cache/test/sessions"
    #id: "e3723c3056f23247565a4abdafd2c3cc964cc6d425e6d51dfd70bc588b4ad78b"
    #name: "MOCKSESSID"
    #started: false
    #closed: false
    #data: array:3 [
      "_sf2_attributes" => &1 array:1 [
        "_security_secured_area" => "C:74:"Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken":227:{a:3:{i:0;N;i:1;s:12:"secured_area";i:2;s:178:"a:4:{i:0;s:5:"login";i:1;b:1;i:2;a:1:{i:0;O:41:"Symfony\Component\Security\Core\Role\Role":1:{s:47:"\x00Symfony\Component\Security\Core\Role\Role\x00role";s:9:"ROLE_USER";}}i:3;a:0:{}}";}}"
      ]
      "_sf2_flashes" => &2 []
      "_sf2_meta" => &3 array:3 [
        "u" => 1453705239
        "c" => 1453705239
        "l" => "0"
      ]
    ]
1

1 Answers