Hiya!

It is actually quite easy, you just need to break down the entire
thing into different mock objects. For that, you need to know what
method returns what.

getSql() returns an Zend\Db\Sql\Sql object, select() on that returns
an Zend\Db\Sql\Select object and columns() returns the same object.

The second select() returns a ResultSet as before. So, the mocks are
something like this:

        $mockSelect = $this->getMock('Zend\Db\Sql\Select',
array('columns'), array(), '', false);
        $mockSelect->expects($this->once())
                    ->method('columns')
                    ->with(array('artist', 'title', 'id'))
                    ->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));

here's the entire method in a gist https://gist.github.com/robertbasic/5047567

Add the mock for join yourself, it's actually similar to the columns
part, and see the phpunit docs on test doubles for more help
http://www.phpunit.de/manual/current/en/test-doubles.html

On 22 February 2013 15:04, samsonasik <[email protected]> wrote:
> assume, i have a method in AlbumTable class like the following :
> /////////
>      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);
>     }
> /////////
> The question is how to test that method ? I have created test case like the
> following :
>
> namespace AlbumTest\Model;
>
> use Album\Model\AlbumTable;
> use Album\Model\Album;
> use Zend\Db\ResultSet\ResultSet;
> use Zend\Db\Sql\Sql;
> use PHPUnit_Framework_TestCase;
>
> class AlbumTableTest extends PHPUnit_Framework_TestCase
> {
>     public function testFetchAllReturnsAllAlbums()
>     {
>         $resultSet = new ResultSet();
>         $mockTableGateway =
> $this->getMock('Zend\Db\TableGateway\TableGateway',
>                                            array('select'), array(), '',
> false);
>         $mockTableGateway->expects($this->once())
>                          ->method('select')
>                          ->will($this->returnValue($resultSet));
>
>
>         $albumTable = new AlbumTable($mockTableGateway);
>         $albumTable->fetchAll();
>
>         $this->assertSame($resultSet, $albumTable->fetchAll());
>     }
> }
>
>
>
> --
> View this message in context: 
> http://zend-framework-community.634137.n4.nabble.com/How-to-test-complex-Query-in-ZF2-using-phpunit-tp4659268.html
> Sent from the Zend Framework mailing list archive at Nabble.com.
>
> --
> List: [email protected]
> Info: http://framework.zend.com/archives
> Unsubscribe: [email protected]
>
>



-- 
~Robert Basic;
http://robertbasic.com/

-- 
List: [email protected]
Info: http://framework.zend.com/archives
Unsubscribe: [email protected]


Reply via email to