title: Zend Framework 2 Doctrine 2 Module - No Service Locator in Controller Unit Tests
body: <p>Here is my situation. I am developing a Zend Framework 2 application. I am using Doctrine module to communicate with a MySQL database. It is working fine in the application, I can load the entity manager from the service locator inside my controllers.</p>
  
  <p>But inside my controller unit tests, the service locator doesn't exists and therefor all tests dealing with the database are failing with error message like this:</p>
  
  <blockquote>
    <p>Fatal error: Call to a member function <code>get()</code> on a non-object in /Users/richardknop/Projects/myproject/module/Api/src/Api/Controller/UserController.php on line 19</p>
  </blockquote>
  
  <p>I have narrowed the problem down to this method:</p>
  
  <pre><code>$this-&gt;getServiceLocator()
  </code></pre>
  
  <p>Which works in my controllers but returns NULL in unit tests.</p>
  
  <p>This is my application.config.php:</p>
  
  <pre><code>&lt;?php
  
  return array(
      'modules' =&gt; array(
          'DoctrineModule',
          'DoctrineORMModule',
          'Api',
      ),
      'module_listener_options' =&gt; array(
          'config_glob_paths' =&gt; array(
              'config/autoload/{,*.}{global,local}.php',
          ),
          'module_paths' =&gt; array(
              './module',
              './vendor',
          ),
      ),
  );
  </code></pre>
  
  <p>My local.php file with database connection details:</p>
  
  <pre><code>&lt;?php
  
  return array(
      'doctrine' =&gt; array(
          'connection' =&gt; array(
              'orm_default' =&gt; array(
                  'driverClass' =&gt; 'Doctrine\DBAL\Driver\PDOMySql\Driver',
                  'params' =&gt; array(
                      'host'     =&gt; 'localhost',
                      'port'     =&gt; '3306',
                      'user'     =&gt; 'root',
                      'password' =&gt; 'root',
                      'dbname'   =&gt; 'mydb',
                  ),
              ),
          ),
      ),
  );
  </code></pre>
  
  <p>In my module.config.php I have:</p>
  
  <pre><code>'doctrine' =&gt; array(
      'driver' =&gt; array(
          __NAMESPACE__ . '_driver' =&gt; array(
              'class' =&gt; 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
              'cache' =&gt; 'array',
              'paths' =&gt; array(__DIR__ . '/../src/' . __NAMESPACE__ . '/Entity')
          ),
          'orm_default' =&gt; array(
              'drivers' =&gt; array(
                  __NAMESPACE__ . '\Entity' =&gt; __NAMESPACE__ . '_driver'
              ),
          ),
      ),
  ),
  </code></pre>
  
  <p>And in my controllers I have this method to get the entity manager:</p>
  
  <pre><code>private $_em;
  
  private function _getEntityManager()
  {
      if (null === $this-&gt;_em) {
          $this-&gt;_em = $this-&gt;getServiceLocator()-&gt;get('doctrine.entitymanager.orm_default');
      }
      return $this-&gt;_em;
  }
  </code></pre>
  
  <p>Now my unit tests. Here is the bootstrap file:</p>
  
  <pre><code>&lt;?php
  
  chdir(dirname(__DIR__));
  include __DIR__ . '/../init_autoloader.php';
  return Zend\Mvc\Application::init(include 'config/application.config.php');
  </code></pre>
  
  <p>And setUp method of my controller tests looks like:</p>
  
  <pre><code>public function setUp()
  {
      $this-&gt;_controller = new UserTokenController;
      $this-&gt;_request = new Request;
      $this-&gt;_routeMatch = new RouteMatch(array('controller' =&gt; 'user'));
      $this-&gt;_event = new MvcEvent();
      $this-&gt;_event-&gt;setRouteMatch($this-&gt;_routeMatch);
      $this-&gt;_controller-&gt;setEvent($this-&gt;_event);
  }
  </code></pre>
  
  <p>An example controller unit test then looks like:</p>
  
  <pre><code>public function testGetListHttpStatusCode()
  {
      $response = $this-&gt;_controller-&gt;dispatch($this-&gt;_request);
      $this-&gt;assertEquals(405, $response-&gt;getStatusCode());
  }
  </code></pre>
  
  <p>All tests that are not connecting to the database are passing. Tests where I am using entity manager are failing though with this error:</p>
  
  <blockquote>
    <p>Fatal error: Call to a member function <code>get()</code> on a non-object in /Users/richardknop/Projects/myproject/module/Api/src/Api/Controller/UserController.php on line 19</p>
  </blockquote>
  
  <p>This is line 19:</p>
  
  <pre><code>$this-&gt;_em = $this-&gt;getServiceLocator()-&gt;get('doctrine.entitymanager.orm_default');
  </code></pre>
  
  <p>And the reason it's failing is that <code>$this-&gt;getServiceLocator()</code> returns <strong><code>NULL</code></strong>.</p>
  
  <p>Any ideas how to solve this issue?</p>