使用功能测试symfony时如何获取访问令牌?

时间:2017-11-15 10:07:46

标签: php symfony phpunit

我想在功能测试中添加一个访问令牌

public function testShowEmployees(){

        $accessToken = "";
        $this->client->request('GET', 'api/v1/patients/108', array(),
            array(),
            array('CONTENT_TYPE' => 'application/json',
                'HTTP_AUTHORIZATION' => "Bearer {$accessToken}",);

        $this->assertEquals(
            Response::HTTP_OK,
            $this->client->getResponse()->getStatusCode()
        );

        $this->logIn();

        $this->client->request('GET', 'api/v1/patients');
        $client = static::createClient();
}

我尝试在谷歌搜索,但我找不到获取访问令牌的具体示例。我有这个link,但我不知道如何获得令牌。但如果我尝试这个

$client->getContainer()->get('security.context')->getToken();

我收到错误

You have requested a non-existent service "security.context"

更新

在我的控制器测试中我有这些代码

public function setUp()
    {
        $this->client = static::createClient( array(),
            array(
                'PHP_AUTH_USER' => 'sample',
                'PHP_AUTH_PW' => 'sample'
            ));

    }

    public function testShowEmployees(){

        $accessToken = "";
        $this->client->request('GET', 'api/v1/patients/108', array(),
            array(),
            array('CONTENT_TYPE' => 'application/json',
                'HTTP_AUTHORIZATION' => "Bearer {$accessToken}"));

         echo $this->client->getContainer()->get('security.token_storage')->getToken();
        die;
}

当我运行phpunit命令时。我从令牌I echo

得到空结果

0 个答案:

没有答案