If checkForUnintentionallyCoveredCode is set to true in the PHPUnit configuration file (which is the case in typo3/buildessentials's UnitTests.xml), unit test like the following one are [1] marked as “risky” by PHPUnit and [2] therefore will not increase code coverage:
/**
* @test
* @covers \Foo\Bar\Domain\Model\Baz::getBam
* @return void
*/
public function canReturnBam() {
$baz = new \Foo\Bar\Domain\Model\Baz();
$this->assertEquals('', $baz->getBam());
}
If I omit the @covers annotation the tests are not marked as “risky” anymore and will increase code coverage.
PHPUnit 4.5.0 by Sebastian Bergmann and contributors.
Configuration read from Build/buildessentials/PhpUnit/UnitTests.xml
.
Time: 1.09 seconds, Memory: 15.25Mb
OK (1 test, 1 assertion)
Generating code coverage report in HTML format ... done
I guess the following was the problem: my example was oversimplyfied (but not tested by itself). Sorry for that!
As far as I can recall a call of a __construct() method was most probably involved which seems to lead to “unintentionally covered code”. I added a @codeCoverageIgnore annotation to the __construct(). The test is not marked risky any longer.
I am not sure if using a @codeCoverageIgnore is a good practice here. I already tried various things using the @uses and @coversannotations but couldn’t come with a better solution.