In order to access the entity manager in your unit tests you will have to make your PHPUnit bootstrap file a little bit more complex. You will need to access the entity manager if you are using Doctrine module together with Zend Framework 2.
Here is what I have done:
<?php use Zend\ServiceManager\ServiceManager, Zend\Mvc\Service\ServiceManagerConfig; class Bootstrap { static public $config; static public $sm; static public $em; static public function go() { chdir(dirname(__DIR__)); include __DIR__ . '/../init_autoloader.php'; self::$config = include 'config/application.config.php'; Zend\Mvc\Application::init(self::$config); self::$sm = self::getServiceManager(self::$config); self::$em = self::getEntityManager(self::$sm); } static public function getServiceManager($config) { $serviceManager = new ServiceManager(new ServiceManagerConfig); $serviceManager->setService('ApplicationConfig', $config); $serviceManager->get('ModuleManager')->loadModules(); return $serviceManager; } static public function getEntityManager($serviceManager) { return $serviceManager->get('doctrine.entitymanager.orm_default'); } } Bootstrap::go();
And then in your unit tests you can access the entity manager simply by:
Bootstrap:$em
That was quite easy actually.