PHPUnit Strict Mode - setUp() - Coverage
Since this question started to gain some momentum: Here is my kind-of-solution for the problem.
My unit-test (FooTest
) of Foo
will always use Foo
, therefore I add @uses Foo
to the class.
This is also important if protected
/private
functions are used by public
functions, because else you have to add each and every protected
/private
function to a test, if the class uses the function internally. I even think it is wrong if you are doing unit-tests, because a unit-test must not care about how the class does "stuff", it should only assert that a specific input results in a specific output.
(Additionally: The constructor should only do assignments, nothing else.)
After adding @uses
the error will disapear.
(You can add @covers Foo::_construct
to the class to have code-coverage of your constructor.)
/**
* @uses Foo
* (optional)@covers Foo::__construct
*/
class FooTest extends \PHPUnit_Framework_TestCase
{
protected $_foo;
protected function setUp()
{
$this->_foo=new Foo(10);
}
public function testGetBar()
{
$this->assertSame(10, $this->_foo->getBar());
}
/**
* @covers Foo::getBar2
*/
public function testGetBar2()
{
$this->assertSame(10, $this->_foo->getBar2());
}
}
You have specified strict coverage with checkForUnintentionallyCoveredCode="true"
. And since PHPUnit 4.0 PHPUnit has the following behaviour:
Dealing with unintentionally covered code
PHPUnit 4.0 can optionally be strict about unintentionally covered code (strict > coverage mode). When enabled, PHPUnit will fail a test that uses the @covers annotation and executes code that is not specified using a @covers annotation.