How to test complex Query in ZF2 using phpunit ?

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

How to test complex Query in ZF2 using phpunit ?

samsonasik
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());
    }
}
Reply | Threaded
Open this post in threaded view
|

Re: How to test complex Query in ZF2 using phpunit ?

bakura
I'm not using Zend\Db but is this really something you'd like to test ? After all, this method only uses the table gateway which should have been tested.

I think the best way to test that kind of thing would be to use something similar as fixtures, with real input data.

I'm looking forward the answer though !
Reply | Threaded
Open this post in threaded view
|

Re: How to test complex Query in ZF2 using phpunit ?

samsonasik
Yes, I expand the case from this http://zf2.readthedocs.org/en/latest/tutorials/unittesting.html and I want to test the join/group/other filter instead of select() functionality. real input data is simply to test input/delete/update from my last read from http://zf2.readthedocs.org/en/latest/tutorials/unittesting.html , but I don't know how to get all data with complex filter(join/group/columns/other filter)
Reply | Threaded
Open this post in threaded view
|

Re: How to test complex Query in ZF2 using phpunit ?

robertbasic
In reply to this post by samsonasik
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 <[hidden email]> 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: [hidden email]
> Info: http://framework.zend.com/archives
> Unsubscribe: [hidden email]
>
>



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

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


Reply | Threaded
Open this post in threaded view
|

Re: How to test complex Query in ZF2 using phpunit ?

samsonasik
Robert Basic, Thanks, it's work
I modified the code to utilize join test here  :
https://gist.github.com/samsonasik/5048353/e48f931059790fd5c099fe972ffc76e65b7c1e6f