使用以下作为指南,我试图掌握嘲弄的窍门:
Zend Db Adapter 适配器模拟与模拟和 数据库和模型测试
(我的 CommissionTable 扩展了 AbstractTableGateway 并且它的构造函数通过了 Db 适配器)
我有以下(适配器模拟链接):
protected function getAdapterMock() {
$driver = m::mock('Zend\Db\Adapter\Driver\DriverInterface');
$adapter = m::mock('Zend\Db\Adapter\Adapter');
$platform = m::mock('Zend\Db\Adapter\Platform\Mysql[getName]');
$stmt = m::mock('Zend\Db\Adapter\Driver\Pdo\Statement');
$paramContainer = m::mock('Zend\Db\Adapter\ParameterContainer');
$platform->shouldReceive('getName')
->once()
->andReturn('MySQL');
$stmt->shouldReceive('getParameterContainer')
->once()
->andReturn($paramContainer);
$stmt->shouldReceive('setSql')
->once()
->andReturn($stmt);
$stmt->shouldReceive('execute')
->once()
->andReturn(array());
$adapter->shouldReceive('getPlatform')
->once()
->andReturn($platform);
$driver->shouldReceive('createStatement')
->once()
->andReturn($stmt);
$adapter->shouldReceive('getDriver')
->once()
->andReturn($driver);
return $adapter;
}
实际测试是:
public function testFetchAll() {
$commission = new Commission();
$resultSet = new ResultSet();
$resultSet->setArrayObjectPrototype(new Commission());
$resultSet->initialize(array($commission));
$adapter = $this->getAdapterMock();
$adapter->shouldReceive('fetchAll')
->once()
->andReturn($this->returnValue($resultSet));
$commissionTable = new CommissionTable($adapter);
$this->assertSame($resultSet, $commissionTable->fetchAll());
}
PHPUnit 返回:
1) GWLTest\Model\CommissionTableTest::testFetchAll
Failed asserting that two variables reference the same object.
如果我对 ($resultSet) 和 $commissionTable->fetchAll() 进行 Zend Debug 转储,它们是相同的,除了
(结果集)
protected $dataSource =>
class ArrayIterator#2573 (1) {
private $storage =>
array(1) {
[0] =>
class GWL\Model\Commission#2571 (5) {
...
}
}
}
($commissionTable->fetchAll())
protected $dataSource =>
class ArrayIterator#2666 (1) {
private $storage =>
array(0) {
}
}
而且我还没有弄清楚如何正确初始化 $commissionTable 的 dataSource 以便 assertSame 通过。
请指教,
谢谢你。