I have an abstract base class with an abstract protected method
abstract class AbstractSample
{
abstract protected function getConnection();
}
and child class where protected method is redefined as public:
class ConcreteSample extends AbstractSample
{
public function getConnection()
{
return 'connection resource';
}
}
This is the class I want to test:
class Caller
{
protected $_sample;
public function __construct(ConcreteSample $sample)
{
$this->_sample = $sample;
}
public function method()
{
$conn = $this->_sample->getConnection();
return $conn;
}
}
and the test unit itself:
class Sample_Test extends \PHPUnit_Framework_TestCase
{
public function test_getConnection()
{
$RESOURCE = 'mocked resource';
$mSample = \Mockery::mock(ConcreteSample::class);
$mSample
->shouldReceive('getConnection')->once()
->andReturn($RESOURCE);
$obj = new Caller($mSample);
$res = $obj->method();
$this->assertEquals($RESOURCE, $res);
}
}
When I run the test I have an error:
InvalidArgumentException : getConnection() cannot be mocked as it a protected method and mocking protected methods is not allowed for this mock
/.../vendor/mockery/mockery/library/Mockery.php:670
/.../vendor/mockery/mockery/library/Mockery.php:678
/.../vendor/mockery/mockery/library/Mockery.php:629
/.../var/tmp/Sample_Test.php:13
How can I mock public method that redefines protected method in the base class? Mockery version is 0.9.4
abstract protected function
can be overwritten bypublic function
? I would decline this pull request... – iRaS