Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
610 views
in Technique[技术] by (71.8m points)

mocking - Test Red Line Coverage PHPUnit

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

enter image description here

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...