问题
The goal: Unit test an InputFilter in Zend Framework 2.
The problem: A mocked DbAdapter is needed.
As I am relatively new to Unit Testing I just got started with mocking classes. After doing a lot of research I'm still unable to find a proper solution for my problem so here we go with my filter to start things off:
class ExampleFilter extends Inputfilter
{
protected $dbAdapter;
public function __construct(AdapterInterface $dbAdapter)
{
$this->dbAdapter = $dbAdapter;
}
public function init()
{
$this->add(
[
'name' => 'example_field',
'required' => true,
'filters' => [
['name' => 'StringTrim'],
['name' => 'StripTags'],
],
'validators' => [
[
'name' => 'Db\NoRecordExists',
'options' => [
'adapter' => $this->dbAdapter,
'table' => 'example_table',
'field' => 'example_field',
],
],
],
]
);
}
}
Without the need of an adapter, testing this filter would be rather easy. My problem is to create the Filter in my TestClass as seen here:
class ExampleFilterTest extends \PHPUnit_Framework_TestCase
{
protected $exampleFilter;
protected $mockDbAdapter;
public function setUp()
{
$this->mockDbAdapter = $this->getMockBuilder('Zend\Db\Adapter')
->disableOriginalConstructor()
->getMock();
$this->exampleFilter = new ExampleFilter($this->mockDbAdapter);
}
}
When creating the filter like this the ExampleFilter class will end up saying that I did provide a wrong class to its constructor. It's receiving a mock object when expecting one of type Zend\Db\Adapter\Adapter.
I could create a real adapter of course but I want to avoid performing actual queries to the database as it is a unit test and this would go far beyond the border of my unit to test.
Can anyone tell me how I can achieve my goal of testing the filter with a mocked DbAdapter?
回答1:
Well... As I was commenting on gontrollez hint I already smelled my mistake. I had to create a mock of 'Zend/Db/Adapter/AdapterInterface' instead of just '/Zend/Db/Adapter'.
Thanks for bringing me on the right path anyway gontrollez :)
来源:https://stackoverflow.com/questions/34225052/zf2-phpunit-mock-a-zend-db-adapter-adapter-for-further-usage