Skip to content

Instantly share code, notes, and snippets.

@samsonasik
Forked from robertbasic/gist:5047567
Last active December 14, 2015 07:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samsonasik/5048353 to your computer and use it in GitHub Desktop.
Save samsonasik/5048353 to your computer and use it in GitHub Desktop.
Unit Test select Join Zend\Db
assume, i have a method in AlbumTable class like the following :
<?php
/////////
public function fetchAll()
{
$sqlSelect = $this->tableGateway->getSql()
->select()->columns(array('artist', 'title', 'id'))
->join('track', 'track.album_id = album.id', array(), 'left');
return $this->tableGateway->select($sqlSelect);
}
/////////
?>
<?php
public function testFetchAllReturnsAllAlbums()
{
$resultSet = new ResultSet();
$mockSelect = $this->getMock('Zend\Db\Sql\Select', array('columns', 'join'), array(), '', false);
$mockSelect->expects($this->once())
->method('columns')
->with(array('artist', 'title', 'id'))
->will($this->returnValue($mockSelect));
$mockSelect->expects($this->once())
->method('join')
->with('track', 'track.album_id = album.id', array(), 'left')
->will($this->returnValue($mockSelect));
$mockSql = $this->getMock('Zend\Db\Sql\Sql', array('select'), array(), '', false);
$mockSql->expects($this->once())
->method('select')
->with()
->will($this->returnValue($mockSelect));
$mockTableGateway = $this->getMock('Zend\Db\TableGateway\TableGateway',
array('getSql', 'select'), array(), '', false);
$mockTableGateway->expects($this->once())
->method('getSql')
->with()
->will($this->returnValue($mockSql));
$mockTableGateway->expects($this->once())
->method('select')
->with($mockSelect)
->will($this->returnValue($resultSet));
$albumTable = new AlbumTable($mockTableGateway);
$this->assertSame($resultSet, $albumTable->fetchAll());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment