Hi @gocanto I don't think that 100% code coverage is that good. Its not that has a hidden return it is that throw Exception will stop everything unless you have a try catch, what you could do are several tests that will trigger all the other flows that isn't the exception, but like in this case you already done it.
Oct 1, 2019
8
Level 54
Test coverage
Hi Guys, I have a question regarding the phpunit tests coverage tool.
Say, I have this code:
function foo(): void
{
if ($foo === true) {
return;
}
throw new Exception('some error');
}
Even though I have a full tests suit that covers the whole function, phpunit marks this file as not completed coverage due to the exception at the end of the function.
I have been told this is due to a hidden return after the exception, but I am not quite sure how to deal with it.
Now, if I move that exception within a else statement, the test coverage is back to normal.
Another work around I found was putting this after the exception call:
//@codeCoverageIgnoreStart
return;
//@codeCoverageIgnoreEnd
Any hint I can follow?
Thanks,
Level 54
I finally found how to deal with it.
- Do not throw an exception within an unhandled structure.
- Write a happy path to coverage the code after a given exception is thrown.
Thanks, everybody!
Please or to participate in this conversation.