5

I'm trying to Unit Test a ZF2 Module I've written, specifically, a service object.

But I'm getting stuck on how to get the service manager (which calls my factory object) into the test class properly. My factory object injects my modules entity object, the Doctrine entity manager, and my module's entity repository.

How do I ensure that the the factory is properly called during the Unit Test?

1

1 Answer 1

4

This is what I do in my bootstrap.php:

public static function init()
{
    if (is_readable(__DIR__ . '/TestConfig.php')) {
        $testConfig = include __DIR__ . '/TestConfig.php';
    } else {
        $testConfig = include __DIR__ . '/TestConfig.php.dist';
    }

    $zf2ModulePaths = array();
    if (isset($testConfig['module_listener_options']['module_paths'])) {
        $modulePaths = $testConfig['module_listener_options']['module_paths'];
        foreach ($modulePaths as $modulePath) {
            if (($path = static::findParentPath($modulePath)) ) {
                $zf2ModulePaths[] = $path;
            }
        }
    }
    $zf2ModulePaths  = implode(PATH_SEPARATOR, $zf2ModulePaths) . PATH_SEPARATOR;
    $zf2ModulePaths .= getenv('ZF2_MODULES_TEST_PATHS') ?: (defined('ZF2_MODULES_TEST_PATHS') ? ZF2_MODULES_TEST_PATHS : '');

    $serviceManager = new ServiceManager(new ServiceManagerConfig());
    $serviceManager->setService(
        'ApplicationConfig',
        $testConfig
    );
    $serviceManager->get('ModuleManager')->loadModules();
    $serviceManager->setAllowOverride(true);
    static::$serviceManager = $serviceManager;
}

public static function getServiceManager()
{
    return static::$serviceManager;
}

And in your test class you can jus call Bootstrap::getServiceManager().

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.