duanchongzi9997 2015-05-10 21:44 采纳率: 0%
浏览 37

如何使用ZF2中的PHPUnit使用Zend \ Db \ Sql \ Select单元测试方法?

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?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
    • ¥15 有了解d3和topogram.js库的吗?有偿请教
    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
    • ¥15 关于#Java#的问题,如何解决?
    • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
    • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
    • ¥15 cmd cl 0x000007b
    • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line