Php zf2模拟zend db适配器

Php zf2模拟zend db适配器,php,zend-framework2,mockery,Php,Zend Framework2,Mockery,以下面的内容为指导,我试图掌握嘲弄的窍门: 及 (my CommissionTable扩展了AbstractTableGateway,并将其构造函数传递给Db适配器) 我有以下(适配器模拟链接): 实际测试是: public function testFetchAll() { $commission = new Commission(); $resultSet = new ResultSet(); $resultSet->setArrayObjectPrototy

以下面的内容为指导,我试图掌握嘲弄的窍门:

(my CommissionTable扩展了AbstractTableGateway,并将其构造函数传递给Db适配器)

我有以下(适配器模拟链接):

实际测试是:

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调试转储,它们除了

(结果集)

($commissionTable->fetchAll())

我还没有弄清楚如何正确初始化$commissionTable的数据源,以便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');
}
protected $dataSource =>
 class ArrayIterator#2573 (1) {
 private $storage =>
 array(1) {
  [0] =>
  class GWL\Model\Commission#2571 (5) {
    ...
  }
 }
}
  protected $dataSource =>
  class ArrayIterator#2666 (1) {
   private $storage =>
    array(0) {
   }
  }
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');
}