Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zend Framework 2 phpunit testing a table with a join

I have used Album example from Zend Framework 2 Documentation and created a application.

Now while unittesting it using phpunit I am having an issue while testing a table which is having a join with say table Account_Type.

Here is the code for it.

fetchAll function is

function fetachAll()
{
    $sql = new Sql($this->tableGateway->getAdapter());

    $select = $sql->select();

    $select->from('Album')
           ->columns(array('id', 'name', 'account_type_id', 'managing_account_id'))
       ->join(array('AT' => 'account_type'), 'album.account_type_id = AT.account_type_id');

    $resultSet = $this->tableGateway->selectWith($select);

    return $resultSet; 
}

Unit test code for above table is.

public function testFetchAllReturnsAllAlbums()
{
    $resultSet= new ResultSet();

    $mockTableGateway = $this->getMock(
        'Zend\Db\TableGateway\TableGateway', 
        array('select'), 
        array(), 
        '', 
        false
    );    

    $mockTableGateway->expects($this->once())
                     ->method('select')
                     ->with()
                     ->will($this->returnValue($resultSet));

    $albumTable = new AlbumTable($mockTableGateway);

    $this->assertSame($resultSet, $albumTable->fetchAll());
}

I am getting error this error

Argument 1 passed to Zend\Db\Sql\Sql::__construct() must be an instance of
Zend\Db\Adapter\Adapter, null given,

for this line $this->assertSame($resultSet, $albumTable->fetchAll()); in testFetchAllReturnsAllAlbums method.

If any one have done phpunit testing for join, please provide an example for the same.

like image 856
user1970996 Avatar asked Feb 02 '13 20:02

user1970996


1 Answers

You might want to mock the getAdapter method of your Zend\Db\TableGateway\TableGateway object. This method is called and its returned value passed to the Zend\Db\Sql\Sql constructor.

like image 185
Thierry Marianne Avatar answered Nov 14 '22 21:11

Thierry Marianne