ZF2 - Mocking service requested in Module.php ZF2 - Mocking service requested in Module.php php php

ZF2 - Mocking service requested in Module.php


There is not enough data here to help you directly. It would be more useful to have the function ValidateCustomerRegistrationTest->getApplicationServiceLocator() for starters.

I hope I can help you indirectly.

Refactoring could be helpful

When I am writing a unit test I start with a few personal rules.

Only test the code being testing. Mock EVERYTHING else. No need to test something that should have it's own tests already.

How a function works should not be important. Only the input/output. This keeps your test viable even when the core of a function changes drastically.

/** * @param MvcEventInterface $event * @param Service\From\Other\ModuleInterface $service * * @return boolean */public function onBootstrap(MvcEventInterface $event, Service\From\Other\ModuleInterface $service){  return true;}

Then in the test class:

public function testOnBootstrap(){   $eventMock = $this->getMock(MvcEventInterface::class);   $serviceMock = $this->getMock(ModuleInterface::class);   $module = new Module();   $result = $module->onBootstrap($eventMock, $serviceMock);   $this->assertTrue($result);}

* I clearly do not know what you are trying to test

When refactoring is not an option

There are two mock types that can help out, that occur to me right off the bat, mock, and mockBuilder. Take a look at the PHPUnit documentation for Test Doubles.

$serviceMock = $this->getMockBuilder(ServiceManager::class)    ->disableOriginalConstructor()    ->setMethods([        '__construct',        'get'    ])    ->getMock();$serviceMock    ->expects($this->any())    ->method('get')    ->will($this->returnValue(        $this->getMock(ModuleManagerInterface::class)    ));

Good luck, I hope you can let us know if we can help you more specifically. I would also recommend looking into SOLID Principles of Object Oriented Programing. One of many programming principles that should make your code clean, easy to extend, and easy to test.


One way to accomplish what you're trying to do may be to force an overwrite of the service in the Service Manager before dispatching the request to your test controller. Here's an example of how to do that (NOTE: since what you're doing is during the module's bootstrap process, the example may not translate 100% to your situation).

Like other people mentioned: you may still want to double-check whether you can refactor to a cleaner approach, but that's another story.


In order to mock the service manager and the calls made with it you can use mockery https://github.com/mockery/mockery. This library is framework agnostic so even if you use PHPUnit or an other tool it should work.

The bad way to use it but it should solve your problem fast is to overload the class using mockery('overload:myclass') where myclass is the instance of the service manager.

The best way to use the library is to use the depedency injection pattern to inject a mock of the service locator when your app is boostraping for the tests.

An easy way to go with it is to create a factory for your controller, inject the service locator inside. Then in your test you init the controller with the mock of the service locator as a parameter.