【发布时间】:2014-05-02 16:04:48
【问题描述】:
我有一个通过 DI 使用 \Zend\Db\TableGateway\TableGateway 的映射器。我已经为单元测试模拟了它。这是测试:
class EMSMapperTest extends PHPUnit_Framework_TestCase
{
public function testFetchAllReturnsAllScheduledBlocks()
{
$resultSet = new ResultSet();
$mockTableGateway = $this->getMock(
'Zend\Db\TableGateway\TableGateway',
array('select','getTable'),
array(),
'',
false
);
$mockTableGateway->expects($this->once())
->method('selectWith')
->with()
->will($this->returnValue($resultSet));
$mockTableGateway->expects($this->once())
->method('getTable')
->with()
->will($this->returnValue('table'));
$emsMapper = new EMSMapper($mockTableGateway);
$this->assertSame($resultSet, $emsMapper->fetchAll());
}
}
和正在测试的映射器:
class EMSMapper extends BaseMapper
{
public function fetchAll( $building = null,
$room = null, DateRange $range = null )
{
$select = new Select;
$table = $this->tableGateway->getTable();
$select->from($table);
if(!empty($building))
{
$select->where(array('buildingCode'=>$building));
}
if(!empty($room))
{
$select->where(array("room"=>$room));
}
if(is_array($range))
{
if(!empty($range['start']))
{
$select->where("start >= '{$range['start']}'");
}
if(!empty($range['stop']))
{
$select->where("stop <= '{$range['stop']}'");
}
}
$resultSet = $this->tableGateway->selectWith($select);
$results = array();
foreach($resultSet as $r)
{
$results[] = $r;
}
return $results;
}
}
在从TableGateway 的getTable() 方法返回一个字符串后,单元测试会显示:
There was 1 error:
1) EMSTest\Model\EMSMapperTest::testFetchAllReturnsAllScheduledBlocks
Zend\Db\TableGateway\Exception\RuntimeException:
This table does not have an Adapter setup
如果Select 似乎要求提供给from() 方法的表字符串具有与之关联的适配器。如何提供所需适配器的模拟?
感谢您的帮助!
【问题讨论】: