zf2 mockery zend db adapter

时间:2013-10-19 14:27:41

标签: php zend-framework2 mockery

使用以下作为指导我试图摆脱嘲弄:

Zend Db Adapter Adapter Mocking with mockeryDatabase and Models Testing

(我的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通过。

请指教,

谢谢。

1 个答案:

答案 0 :(得分:1)

最后不需要模拟适配器。以下作品:

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');
}