简体   繁体   中英

zf2 mockery zend db adapter

Using the below as guides I am trying to get the hang of mockery:

Zend Db Adapter Adapter Mocking with mockery and Database and Models Testing

(my CommissionTable extends AbstractTableGateway and its constructor is passed the Db Adapter)

I have the following (Adapter Mocking link):

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

and the actual test is:

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 returns:

1) GWLTest\Model\CommissionTableTest::testFetchAll
Failed asserting that two variables reference the same object.

If I do Zend Debug dump of the ($resultSet) and $commissionTable->fetchAll() they are identical apart from

(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) {
   }
  }

and I haven't figured out how to correctly initialize $commissionTable's dataSource so the assertSame passes.

Please advise,

Thank you.

Didn't need the mock adapter in the end. The following works:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM