Amelie
Amelie

Reputation: 516

How to mock ServiceLocator ZF2?

Here is a Factory :

namespace Maintenance\Factory\View\Helper;

use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Maintenance\View\Helper\SousMenuContrat;

class SousMenuContratFactory implements FactoryInterface
{
    public function createService(ServiceLocatorInterface $serviceLocator)
        {
            $realServiceLocator = $serviceLocator->getServiceLocator();

            $maiContratService = $realServiceLocator->get(
                'Maintenance\Service\Model\FMaiContratService'
            );   

            return new SousMenuContrat(
                $maiContratService
            );
        }
   } 

I have to write some PHPUnit tests, I began to do so :

public function testCreateService()
    {
        $this->mockDriver = $this->getMock('Zend\Db\Adapter\Driver\DriverInterface');
        $this->mockConnection = $this->getMock('Zend\Db\Adapter\Driver\ConnectionInterface');
        $this->mockDriver->expects($this->any())->method('checkEnvironment')->will($this->returnValue(true));
        $this->mockDriver->expects($this->any())->method('getConnection')->will($this->returnValue($this->mockConnection));
        $this->mockPlatform = $this->getMock('Zend\Db\Adapter\Platform\PlatformInterface');
        $this->mockStatement = $this->getMock('Zend\Db\Adapter\Driver\StatementInterface');
        $this->mockDriver->expects($this->any())->method('createStatement')->will($this->returnValue($this->mockStatement));
        $this->adapter = new Adapter($this->mockDriver, $this->mockPlatform);
        $this->sql = new Sql($this->adapter);


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

        $maiContratTable = $this->getMockBuilder('Maintenance\Model\BDD\FMaiContratTable')
            ->setMethods(array())
            ->setConstructorArgs(array($mockTableGateway, $this->adapter, $this->sql))
            ->getMock();


        $smMock = $this->getMockBuilder('Zend\ServiceManager\ServiceManager')
                       ->setMethods(array('get'))
                       ->getMock();

        $smMock->expects($this->at(0))
            ->method('get')
            ->with('Maintenance\Service\Model\FMaiContratService')
            ->will($this->returnValue(new FMaiContratService($maiContratTable)));

        $factory = new SousMenuContratFactory();
        $runner = $factory->createService($smMock);
    }

But I got some problems. This tells me :

Call to undefined method Mock_ServiceManager_3ed93deb::getServiceLocator()

What have I misunderstood ?

Thanks !

Upvotes: 1

Views: 680

Answers (1)

StoryTeller
StoryTeller

Reputation: 1748

In your factory you call:

$realServiceLocator = $serviceLocator->getServiceLocator();

But you defined:

$smMock->expects($this->at(0))
        ->method('get')

The ServiceLocator passed to your factory usually does not have the method getServiceLocator because it already is the service locator. (Edit: Scratch that, PluginManagers do!) Instead use:

public function createService(ServiceLocatorInterface $serviceLocator)
{
    $maiContratService = $serviceLocator->get(
        'Maintenance\Service\Model\FMaiContratService'
    );   

    return new SousMenuContrat(
        $maiContratService
    );
}

Edit: Plugin factories are another thing, here's the test code:

public function testCreateService()
{
    $maiContractServiceMock = $this->getMockBuilder('Maintenance\Service\Model\FMaiContratService')
        ->disableOriginalConstructor()
        ->getMock();
    // if you do something with FMaiContratService in the constructor of SousMenuContrat,
    // mock more methods here

    $smMock = $this->getMockBuilder('Zend\ServiceManager\ServiceManager')
        ->setMethods(array('get'))
        ->getMock();
    $smMock->expects($this->at(0))
        ->method('get')
        ->with('Maintenance\Service\Model\FMaiContratService')
        ->will($this->returnValue($maiContractServiceMock));

    $hpmMock = $this->getMockBuilder('Zend\View\HelperPluginManager')
        ->setMethods(array('getServiceLocator'))
        ->getMock();
    $hpmMock->expects($this->any())
        ->method('getServiceLocator')
        ->will($this->returnValue($smMock));

    $factory = new SousMenuContratFactory();
    $runner = $factory->createService($hpmMock);
}

In this case, you need a mock of the plugin manager, returning another service locator if getServiceLocator is called. Désolé!

Upvotes: 2

Related Questions