Question

I have a mapper that uses a \Zend\Db\TableGateway\TableGateway via DI. I have mocked it for the unit test.Here is the test:

class EMSMapperTest extends PHPUnit_Framework_TestCase
{

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

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

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

        $mockTableGateway->expects($this->once())
                         ->method('getTable')
                         ->with()
                         ->will($this->returnValue('table'));

        $emsMapper = new EMSMapper($mockTableGateway);

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

and the mapper being tested:

class EMSMapper extends BaseMapper
{    
    public function fetchAll( $building = null, 
            $room = null, DateRange $range = null )
    {
        $select = new Select;
        $table = $this->tableGateway->getTable();

        $select->from($table);

        if(!empty($building))
        {
            $select->where(array('buildingCode'=>$building));
        }

        if(!empty($room))
        {
            $select->where(array("room"=>$room));
        }

        if(is_array($range))
        {
            if(!empty($range['start']))
            {
                $select->where("start >= '{$range['start']}'");
            }

            if(!empty($range['stop']))
            {
                $select->where("stop <= '{$range['stop']}'");
            }
        }

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

        $results = array();

        foreach($resultSet as $r)
        {
            $results[] = $r;
        }

        return $results;
    }
}

After returning a string from the TableGateway's getTable() method the unit test says:

There was 1 error:

1) EMSTest\Model\EMSMapperTest::testFetchAllReturnsAllScheduledBlocks
Zend\Db\TableGateway\Exception\RuntimeException: 
This table does not have an Adapter setup

If would seem that the Select requires the table string supplied to the from() method have an adapter associated with it. How do I supply a mock of the required adapter?

Thanks for the help!

Was it helpful?

Solution

Your code is using the actual code for selectWith. This calls an initialize method that throws your error.

Change you mock code to:

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

This should properly configure your mock.

http://phpunit.de/manual/current/en/test-doubles.html

From the manual:

When the second (optional) parameter is provided, only the methods whose names are in the array are replaced with a configurable test double. The behavior of the other methods is not changed. Providing NULL as the parameter means that no methods will be replaced.

So you were setting the expects on the correct method, but were replacing the wrong one with your mock and so the real code was being executed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top