Authentication in functional tests in Symfony 2.1

后端 未结 5 735
梦谈多话
梦谈多话 2020-12-13 05:15

I am currently in the process of migrating a 2.0.* project to the current 2.1 beta of Symfony.

In my functional tests i currently have this code to create a client w

5条回答
  •  甜味超标
    2020-12-13 05:34

    In my testsuite (working on 2.0 and now 2.1) I use a Base class WebTestCase that extends the Symfony2 WebTestCase class.

    I have these two functions that create an anonymous client or a logged one in my tests:

    static protected function createClient(array $options = array(), array $server = array())
    {
        $client = parent::createClient($options, $server);
    
        self::$container = self::$kernel->getContainer();
    
        return $client;
    }
    
    protected function createAuthClient($user, $pass)
    {
        return self::createClient(array(), array(
            'PHP_AUTH_USER' => $user,
            'PHP_AUTH_PW'   => $pass,
        ));
    }
    

    Then, in my Test classes, I use:

        $client = static::createClient();
    

    to create an anon client and

        $client = static::createAuthClient('user','pass');
    

    to create an authenticated one.

    This works like a charm on 2.1

提交回复
热议问题