Code coverage when not testing protected/private methods with PHPUnit

后端 未结 1 633
挽巷
挽巷 2021-02-14 19:09

I know it\'s possible to test private/protected methods with PHPUnit using reflection or other workarounds.

But most sources tell me that it\'s not best

1条回答
  •  不思量自难忘°
    2021-02-14 19:58

    In Phpunit you can specify the Covered Methods with special annotation, as descrived in the doc.

    You can do something like this:

        class ApiControllerTest extends WebTestCase {
    
            /**
             * @covers ApiController::getCodesAction
             * @covers ApiController::_stringToArray
             */
            public function test_getCodesAction(){
                $client = static::createClient();
                $client->request('GET', '/api/1+2+3');
                $this->assertContains('{"type": "codes", "data": [1,2,3]}', $client->getResponse()->getContent());
            }
    
        }
    

    Hope this help

    0 讨论(0)
提交回复
热议问题