Both Zend\ServiceManager and Zend\EventManager heavy use of closure. You cannot serialize the entire instance of the application and expect it to work, as this basically means that you are trying to serialize service factories and event listeners defined as closures.
The solution could be to use the Bootstrap.php test as one of the DoctrineORMModule that does not store the application instance in memory. Here's a simplified example:
require_once __DIR__ . '/../vendor/autoload.php'; $appConfig = require __DIR__ . '/TestConfiguration.php'; \YourModuleTest\Util\ServiceManagerFactory::setConfig($appConfig); unset($appConfig);
( TestConfiguration should look like the standard mvc application configuration )
You will also need a ServiceManagerFactory . An example implementation can be found here and here .
namespace YourModuleTest\Util; class ServiceManagerFactory { protected static $config; public static function setConfig(array $config) { static::$config = $config; } public static function getServiceManager() { $serviceManager = new ServiceManager(new ServiceManagerConfig( isset(static::$config['service_manager']) ? static::$config['service_manager'] : array() )); $serviceManager->setService('ApplicationConfig', static::$config); $serviceManager->setFactory( 'ServiceListener', 'Zend\Mvc\Service\ServiceListenerFactory' ); $moduleManager = $serviceManager->get('ModuleManager'); $moduleManager->loadModules(); return $serviceManager; } }
Now, wherever you want in your tests, you can:
$serviceManager = \YourModuleTest\Util\ServiceManagerFactory::getServiceManager(); $application = $serviceManager->get('Application'); $application->bootstrap();
With this setting, you can run tests in isolation.
On the other hand, you first need to focus on real unit tests, since ZF2 really makes it easy to create complex objects. You must also properly configure coverage filters so that code unrelated to it is not processed (which can be time consuming).
In addition, reusing an instance of the mvc application is incorrect, since the helpers are not stateless, which makes it difficult to reuse them.