PHPUnit test coverage API with mock

I, I would like to cover a method that get an authentication form an API.

Here is my class:

class MyService
{
    private $api_token;
    private $uri_api;

    public function __construct(string $api_token, string $uri_api){
        $this->api_token = $api_token;
        $this->uri_api = $uri_api;
    }
    public function api_authentication(){
        $client = HttpClient::create();
        return $client->request('GET', $this->getUri_api().'salespoints/', [
            'auth_bearer' => $this->getApi_token()
        ]);
    }
}

And here is my test class:

class MyServiceTest extends WebTestCase
{
    public function test_api_authentication(){
        $client = new HttpClient();

        $stub = $this->getMockBuilder(myService::class)
                    ->disableOriginalConstructor()
                    ->disableOriginalClone()
                    ->disableArgumentCloning()
                    ->disallowMockingUnknownTypes()
                    ->getMock();
        $stub->method('api_authentication')->willReturn($client);
        $this->assertSame($client, $stub->api_authentication());
    }
}

I am using the doc of PHPUnit here (example 8.4).

The problem is that my test pass when I run phpunit but sonarQube tell me that it is not test coverage. I am new using phpunit so if you have any suggestions.

Thank you for your help!

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *