In a Zend Framework 2 application I have a Mapper
(ZfcBase\Mapper\AbstractDbMapper
) class, that looks like this:
<?php
namespace AddressBookAPI\V1\Rest\Address;
use ZfcBase\Mapper\AbstractDbMapper;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Db\Sql\Expression;
use Zend\Db\ResultSet\HydratingResultSet;
class AddressMapper extends AbstractDbMapper {
protected $tableName = 'addresses';
public function getTableName() {
return $this->tableName;
}
public function setTableName($tableName) {
$this->tableName = $tableName;
return $this;
}
public function findAll($params = array()) {
$select = $this->getSelect();
$select->where(
isset($params['user_id']) ? ['user_id' => $params['user_id']] : []
);
$paginatorAdapter = $this->createPaginationAdapter($select);
$collection = new AddressCollection($paginatorAdapter);
return $collection;
}
public function findById($id) {
$select = $this->getSelect();
$select->join('users', 'users.id = addresses.user_id', ['firstname', 'lastname']);
$select->where(array(
'addresses.id' => $id,
));
$entity = $this->select($select)->current();
return $entity;
}
protected function createPaginationAdapter(\Zend\Db\Sql\Select $select) {
return new DbSelect(
$select,
$this->getDbAdapter(),
$this->createHydratingResultSet()
);
}
protected function createHydratingResultSet() {
return new HydratingResultSet(
$this->getHydrator(),
$this->getEntityPrototype()
);
}
}
Now I'm writing unit tests for it:
<?php
use AddressBookAPI\V1\Rest\Address\AddressMapperFactory;
use AddressBookAPITest\Bootstrap;
use AddressBookAPI\V1\Rest\Address\AddressMapper;
use Zend\ServiceManager\ServiceManager;
use AddressBookAPI\V1\Rest\Address\AddressBookAPI\V1\Rest\Address;
class AddressMapperTest extends PHPUnit_Framework_TestCase {
private $addressMapper;
private $serviceManager;
private $tableName;
protected function setUp() {
parent::setUp();
$this->serviceManager = Bootstrap::getServiceManager();
$this->addressMapper = new AddressMapper();
$this->tableName = 'addresses';
}
protected function tearDown() {
$this->serviceManager = null;
$this->addressMapper = null;
$this->tableName = null;
parent::tearDown();
}
public function __construct() {
}
public function testGetTableName() {
$reflectionObject = new ReflectionObject($this->addressMapper);
$reflectionProperty = $reflectionObject->getProperty('tableName');
$reflectionProperty->setAccessible(true);
$reflectionProperty->setValue($this->addressMapper, $this->tableName);
$this->assertSame($this->tableName, $this->addressMapper->getTableName());
}
public function testSetTableName() {
$this->addressMapper->setTableName($this->tableName);
$reflectionObject = new ReflectionObject($this->addressMapper);
$reflectionProperty = $reflectionObject->getProperty('tableName');
$reflectionProperty->setAccessible(true);
$this->assertSame($this->tableName, $reflectionProperty->getValue($this->addressMapper));
}
public function testFindAll() {
$this->markTestIncomplete( "findAll test not implemented" );
}
public function testFindById() {
$this->markTestIncomplete( "findById test not implemented" );
}
}
The problem is the testing of findAll()
and findById()
. Both these methods get a Zend\Db\Sql\Select
over the ZfcBase\Mapper\AbstractDbMapper#getSelect(...)
.
Does it make sense to mock the Select
(mocking then the Mapper
with the getSelect()
of the parent parent class)? What and how should be mocked here?