如何使用Symfony 2在phpunit功能测试中获得用户的关联?

时间:2014-02-04 08:23:07

标签: symfony authentication testing phpunit

我目前正在测试我正在使用http_basic身份验证,如Symfony 2的食谱中所述。

但是,我现在收到此错误:

Fatal error: Call to a member function getClient() on a non-object

代码:

protected function getClient()
{
    /** @var User $user */
    $user = $this->getUser();
    /** @var UserClient $userClient */
    $userClient = $user->getUserClients()->last();
    return $userClient->getClient();
}

由于我是测试新手,并且由于这是一项功能测试,因此传递此错误的最佳做法是什么?

1 个答案:

答案 0 :(得分:0)

您尝试调用getClient()方法,但$userClient不是对象,因此您需要首先检查$userClient对象

protected function getClient()
{
    /** @var User $user */
    $user = $this->getUser();
    /** @var UserClient $userClient */
    $userClient = $user->getUserClients()->last();
    if ($userClient) {
        return $userClient->getClient();
    } 
    return NULL;
}