[英]ZF2 - \Zend\Db\Adapter\Platform::getQuoteIdentifierSymbol()
[英]zf2 mockery zend db adapter
使用以下作为指导,我试图摆脱嘲讽:
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转储,则除了
(的resultSet)
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通过。
请指教,
谢谢。
最后不需要模拟适配器。 以下作品:
public function testFetchAll() {
$resultSet = new ResultSet();
$commissionTableMock = $this->getMock('GWL\Model\CommissionTable', array('select'), array(), '', false);
$commissionTableMock->expects($this->once())
->method('select')
->with()
->will($this->returnValue($resultSet));
$this->assertSame($resultSet, $commissionTableMock->fetchAll());
}
public function testGetCommission() {
$commission = new Commission();
$commission->exchangeArray(array(
'id' => 1,
'description' => 'Default Commission',
'type' => '%',
'amount' => 45.000
));
$resultSet = new ResultSet();
$resultSet->setArrayObjectPrototype(new Commission());
$resultSet->initialize(array($commission));
$commissionTableMock = $this->getMock('GWL\Model\CommissionTable', array('select'), array(), '', false);
$commissionTableMock->expects($this->once())
->method('select')
->with(array('id' => 1))
->will($this->returnValue($resultSet));
$this->assertSame($commission, $commissionTableMock->getCommission(1));
}
public function testExceptionThrownForNonExistentCommission() {
$resultSet = new ResultSet();
$resultSet->setArrayObjectPrototype(new Commission());
$resultSet->initialize(array());
$commissionTableMock = $this->getMock('GWL\Model\CommissionTable', array('select'), array(), '', false);
$commissionTableMock->expects($this->once())
->method('select')
->with(array('id' => 1))
->will($this->returnValue($resultSet));
try {
$commissionTableMock->getCommission(1);
} catch (\Exception $ex) {
$this->assertSame('Could not find row 1', $ex->getMessage());
return;
}
$this->fail('Expected exception was not thrown');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.