【问题标题】:zf2 mockery zend db adapterzf2 mockery zend db 适配器
【发布时间】:2013-10-28 07:52:05
【问题描述】:

使用以下内容作为指南,我试图掌握嘲弄的窍门:

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

之外是相同的

(结果集)

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 的数据源,以便 assertSame 通过。

请指教,

谢谢。

【问题讨论】:

    标签: php zend-framework2 mockery


    【解决方案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');
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-20
      • 1970-01-01
      • 2023-04-01
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多