Magda K.
Magda K.

Reputation: 145

Zend Framework 2 database testing - canCallMagicCall() on a non object

I've been trying to test my model tables using this tutorial: http://framework.zend.com/manual/2.1/en/tutorials/unittesting.html

I try to apply them to my own application which in fact is very much alike the album one in the tutorial with some little modifications.

When I try to run the fetchAll() test I get the following error:

PHP Fatal error:  Call to a member function canCallMagicCall() on a non-object in C:\Program Files (x86)\Zend\Apache2\htdocs\ukazka2\vendor\zendframework\zendframework\library\Zend\Db\TableGateway\AbstractTableGateway.php on line 470

Stacktrace:

[09-Apr-2013 21:23:19 UTC] PHP Stack trace:
[09-Apr-2013 21:23:19 UTC] PHP   1. {main}() C:\Program Files (x86)\Zend\ZendServer\bin\phpunit:0
[09-Apr-2013 21:23:19 UTC] PHP   2. PHPUnit_TextUI_Command::main() C:\Program Files (x86)\Zend\ZendServer\bin\phpunit:46
[09-Apr-2013 21:23:19 UTC] PHP   3. PHPUnit_TextUI_Command->run() C:\Program Files (x86)\Zend\ZendServer\bin\PEAR\PHPUnit\TextUI\Command.php:129
[09-Apr-2013 21:23:19 UTC] PHP   4. PHPUnit_TextUI_TestRunner->doRun() C:\Program Files (x86)\Zend\ZendServer\bin\PEAR\PHPUnit\TextUI\Command.php:176
[09-Apr-2013 21:23:19 UTC] PHP   5. PHPUnit_Framework_TestSuite->run() C:\Program Files (x86)\Zend\ZendServer\bin\PEAR\PHPUnit\TextUI\TestRunner.php:349
[09-Apr-2013 21:23:19 UTC] PHP   6. PHPUnit_Framework_TestSuite->run() C:\Program Files (x86)\Zend\ZendServer\bin\PEAR\PHPUnit\Framework\TestSuite.php:705
[09-Apr-2013 21:23:19 UTC] PHP   7. PHPUnit_Framework_TestSuite->runTest() C:\Program Files (x86)\Zend\ZendServer\bin\PEAR\PHPUnit\Framework\TestSuite.php:745
[09-Apr-2013 21:23:19 UTC] PHP   8. PHPUnit_Framework_TestCase->run() C:\Program Files (x86)\Zend\ZendServer\bin\PEAR\PHPUnit\Framework\TestSuite.php:775
[09-Apr-2013 21:23:19 UTC] PHP   9. PHPUnit_Framework_TestResult->run() C:\Program Files (x86)\Zend\ZendServer\bin\PEAR\PHPUnit\Framework\TestCase.php:776
[09-Apr-2013 21:23:19 UTC] PHP  10. PHPUnit_Framework_TestCase->runBare() C:\Program Files (x86)\Zend\ZendServer\bin\PEAR\PHPUnit\Framework\TestResult.php:648
[09-Apr-2013 21:23:19 UTC] PHP  11. PHPUnit_Framework_TestCase->runTest() C:\Program Files (x86)\Zend\ZendServer\bin\PEAR\PHPUnit\Framework\TestCase.php:831
[09-Apr-2013 21:23:19 UTC] PHP  12. ReflectionMethod->invokeArgs() C:\Program Files (x86)\Zend\ZendServer\bin\PEAR\PHPUnit\Framework\TestCase.php:976
[09-Apr-2013 21:23:19 UTC] PHP  13. BookTest\Model\BookTableTest->testFetchAllReturnsAllBooks() C:\Program Files (x86)\Zend\ZendServer\bin\PEAR\PHPUnit\Framework\TestCase.php:976
[09-Apr-2013 21:23:19 UTC] PHP  14. Mock_TableGateway_26882600->expect() C:\Program Files (x86)\Zend\Apache2\htdocs\ukazka2\module\Book\test\BookTest\Model\BookTableTest.php:15
[09-Apr-2013 21:23:19 UTC] PHP  15. Zend\Db\TableGateway\AbstractTableGateway->__call() C:\Program Files (x86)\Zend\Apache2\htdocs\ukazka2\module\Book\test\BookTest\Model\BookTableTest.php:15

The code of my test is as follows:

public function testFetchAllReturnsAllBooks(){
    $resultSet = new ResultSet();
    $mockTableGateway = $this->getMock('Zend\Db\TableGateway\TableGateway',
            array('select'), array(), '', false);
    $mockTableGateway->expect($this->once())
                     ->method('select')
                     ->with()
                     ->will($this->returnValue($resultSet));

    $bookTable = new BookTable($mockTableGateway);

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

This is my fetchAll() function:

public function fetchAll(){
    if(($resultSet = $this->cache->getItem('books')) == FALSE){
        $resultSet = $this->tableGateway->select();
        $resultSet = $resultSet->toArray();
        $this->cache->setItem('books', $resultSet);         
    }
    $books = array();
    $hydrator = new Hydrator\ArraySerializable();
    foreach($resultSet as $result){
        $books[] = $hydrator->hydrate($result, new Book());
    }
    return $books;
}

Do you have any hints where the bug could dwell?

Upvotes: 2

Views: 2795

Answers (1)

Emma Gospodinova
Emma Gospodinova

Reputation: 148

Are you sure that the method you're calling is correct on the line:

$mockTableGateway->expect($this->once())

From what I can see in the Zend documentation, it should be expects(). Please try and let me know if this is the case.

Upvotes: 2

Related Questions