0

I would like to add a access token in my functional test

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();
}

I try to search in google but I failed to find concrete example for getting the access token. I have this link but there is no clue there how i can get the token. but if i try this one

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

I get an error

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

UPdate

in my controller test I have these codes

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;
}

when I run the phpunit command. I get empty result from token I echo

user3818576
  • 2,979
  • 8
  • 37
  • 62

0 Answers0