I writing test for PHP Trait class. When i run test with coverage on PhpStorm IDE it show some red line on editor line number and give me the result 76% on coverage test lines like this
My question is how to test on that lines so it will give me the result 100% lines
Here is my test code :
Response.php
<?php
trait Response {
/**
* Failed to login response.
*
* @return array
*/
public function failedLogin(): array
{
return [
'code' => 401,
'message' => 'Failed to login',
];
}
}
ResponseTest.php
<?php
use PHPUnitFrameworkMockObjectMockObject;
use PHPUnitFrameworkTestCase;
class ResponseTest extends TestCase
{
/**
* @var MockObject|Response|null
*/
private ?MockObject $mock;
protected function setUp(): void
{
$this->mock = $this->getMockForTrait(Response::class);
}
/**
* @test
*/
public function it_correct_output_failed_login()
{
$response = $this->mock->failedLogin();
$this->assertResponse($response, 401, 'Failed to login');
}
/**
* Assert response.
*
* @param array $response
* @param int $code
* @param null $message
*/
private function assertResponse(array $response, $code = 200, $message = null)
{
$this->assertIsArray($response);
$this->assertArrayHasKey('code', $response);
$this->assertArrayHasKey('message', $response);
$this->assertContains($code, $response);
$this->assertContains($message, $response);
}
protected function tearDown(): void
{
$this->mock = null;
}
}
question from:
https://stackoverflow.com/questions/65883771/test-red-line-coverage-phpunit 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…