1
votes

I followed the instructions on this page but couldn't get my unit test working.

http://framework.zend.com/manual/2.2/en/tutorials/unittesting.html

My initial code was like this:

<?php

namespace ApplicationTest\Controller;

use Zend\Http\Request;
use Zend\Http\Response;
use Zend\Test\PHPUnit\Controller\AbstractHttpControllerTestCase;

class IndexControllerTest extends AbstractHttpControllerTestCase {

    protected $controller;
    protected $request;
    protected $response;
    protected $routeMatch;
    protected $event;
    protected $traceError = true;

    public function setUp() {

        $this->setApplicationConfig(
            include '../../../config/application.config.php'
        );
        parent::setUp();
    }

    public function testIndexActionCanBeAccessed() {

        $this->dispatch('/');
        $this->assertResponseStatusCode(200);

    }
}

And when I ran phpunit, I got the following error message:

PHPUnit 3.7.21 by Sebastian Bergmann.

Configuration read from /usr/share/php/tool/module/Application/test/phpunit.xml

onDispatch called. E

Time: 1 second, Memory: 14.50Mb

There was 1 error:

1) ApplicationTest\Controller\IndexControllerTest::testIndexActionCanBeAccessed Zend\ServiceManager\Exception\ServiceNotFoundException: Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for Zend\Db\Adapter\Adapter

Then I followed the second set of instructions to configure the service manager.

public function testIndexActionCanBeAccessed() {

    $albumTableMock = $this->getMockBuilder('User\Model\UserData')
        ->disableOriginalConstructor()
        ->getMock();

    $albumTableMock->expects($this->once())
        ->method('getUserSessionArray')
        ->will($this->returnValue(array()));

    $serviceManager = $this->getApplicationServiceLocator();
    $serviceManager->setAllowOverride(true);
    $serviceManager->setService('User\Model\UserData', $albumTableMock);

    $this->dispatch('/');
    $this->assertResponseStatusCode(200);

}

And this time, I got the following error:

PHPUnit 3.7.21 by Sebastian Bergmann.

Configuration read from /usr/share/php/tool/module/Application/test/phpunit.xml

onDispatch called. PHP Fatal error: Call to undefined method Mock_UserData_ae821217::getUserSessionArray() in /usr/share/php/tool/module/User/Module.php on line 95 PHP Stack trace: PHP 1. {main}() /usr/local/pear/bin/phpunit:0 …

Could someone help me on this please?

We are using Zend Framework 2.2.0.

Thank you so much.

EC

1

1 Answers

3
votes

Your mock isn't quite setup right. You don't set any methods for the mock to have and so your expects isn't really being set. You need to create your mock like this:

$albumTableMock = $this->getMockBuilder('User\Model\UserData')
    ->disableOriginalConstructor()
    ->setMethods(array('getUserSessionArray'))  //ADD this line
    ->getMock();

Your User\Model\UserData class doesn't exist and so PHPUnit did not create the method to get mocked. And when you ran your tests the function was not defined.