Skip to content

zend framework 2.4 "Zend\ServiceManager\Exception\ServiceNotFoundException: Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for ApplicationConfig"

We had some controller tests in our test cases and the following error was thrown, after we updated to zend framework 2.4.


Zend\ServiceManager\Exception\ServiceNotFoundException: Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for ApplicationConfig

/vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php:555 /vendor/zendframework/zendframework/library/Zend/Mvc/Service/ModuleManagerFactory.php:41 /vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php:939 /vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php:1097 /vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php:638 /vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php:598 /vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php:530 /vendor/zendframework/zendframework/library/Zend/Mvc/Application.php:253 /vendor/zendframework/zendframework/library/Zend/Test/PHPUnit/Controller/AbstractControllerTestCase.php:164 /vendor/zendframework/zendframework/library/Zend/Test/PHPUnit/Controller/AbstractControllerTestCase.php:73 //MyControllerTest.php:124

"MyControllerTest" extends "Zend\Test\PHPUnit\Controller\AbstractHttpControllerTestCase". What we did to solve the problem was adding the following lines of code in our "setUp" method.

$applicationConfigurationPath = DIR . strrepeat('/..', ) . '/config/application.config.php';
if (!isfile($applicationConfigurationPath)) {
    $message = 'application configuration needed and not found in path "' . $applicationConfigurationPath . '"';

$this->fail($message);

} $applicationConfiguration = require $applicationConfigurationPath; unset($applicationConfiguration['modulelisteneroptions']['configglobpaths']); $applicationConfiguration['modules'] = array();

$this->setApplicationConfig($applicationConfiguration);


Thats it, hope it helps.

Translate to de es fr it pt ja

HowTo - PHPUnit MockObject and the problem with "get_class()"

I know it's not the right way but if you are dealing with legacy code or find good reasons (like not storing the class name a database), if is needed that you have to use the php core function "get_class()".

While you are implementing a unittest, you want to mock unneeded objects to focus on the area you need to test. Thats why you create a mockobject like:

$myMock->getMockBuilder('\My\Class')->getMock();
Problem with that, the return value of "getclass()" is something like "MockClassb9e94bdd". Pretty frustrating if you ask me. After a long time of thinking and talking with other developers, we established the following solution.
abstract class AbstractClass { public function getClassName() { return getclass($this); } }

Now you only have to rewrite the code that is using "getclass()" by "$myClass->getClassName()". Du to the fact that $this represents the object, you don't have to trouble with late static bindings. Everything is working out of the box and you have touched a well known area of your code. I have added a <a href="https://github.com/stevleibelt/PHPScript/blob/master/class/getClass.php" target="BLANK" title="github.com stevleibelt php script getClassName example replacing "getclass()">example on one of my github repositories.

By the way, don't get miss leaded by the method "setMockClassName()" provided by the "MockBuilder". If you use this method, your mock object has no mockmethods (like "expects()") anymore.

Translate to de es fr it pt ja