Loading modules not working

I’ve been struggling with this issue for 2 days now, so any help would be really appreciated. We are trying to migrate our laminas-mvc project to 3.x. There is this piece of code which loads the modules for php-unit tests which I have not been able to get it to work.

Previous (worked fine):

 public static function getServiceManager(array $config = null)
    {
        $config = $config ?: static::getApplicationConfig();
        $serviceManager = new ServiceManager(
            new ServiceManagerConfig(
                isset($config['service_manager']) ? $config['service_manager'] : []
            )
        );
        $serviceManager->setService('ApplicationConfig', $config);

        /* @var $moduleManager \Laminas\ModuleManager\ModuleManagerInterface */
        $moduleManager = $serviceManager->get('ModuleManager');

        $moduleManager->loadModules();

        return $serviceManager;
    }

And this is the adapted piece of code which throws Call to a member function attach() on null:

public static function getServiceManager(array $config = null)
    {
        $config = $config ?: static::getApplicationConfig();
        $serviceManager = new ServiceManager(isset($config['service_manager']) ? $config['service_manager'] : []);
        $serviceManager->setService('ApplicationConfig', $config);
        $listenerOptions = new ListenerOptions(isset($config['module_listener_options']) ? $config['module_listener_options'] : []);
        $defaultListeners = new DefaultListenerAggregate($listenerOptions);

        $moduleManager = new ModuleManager(isset($config['modules']) ? $config['modules'] : []);
        $defaultListeners->attach($moduleManager->getEventManager());

        $moduleManager->loadModules();

        return $serviceManager;
    }

Any hint provided would be much appreciated, thank you in advance.

Hello and welcome to our forums! :smiley:

Do you use laminas-test? If so, then none of this is needed.