Reputation: 11
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!
Upvotes: 0
Views: 322
Reputation: 11
Well after research, I think the method that I want to test is not possible to be covered. This is an API call and the coverage need to be done by the developper that written this code. Thanks for your help.
Upvotes: 1
Reputation: 1191
Have you select a Preferred Coverage engine (like xdebug or phpdbg) ?
If you're using phpstorm you can right click on test directory and then go to Edit 'tests (PHPUnit)
.
Then select a code coverrage engine
Then click Apply
.
Now run your test with code coverrage
Upvotes: 0