title: Doctrine and ZF2 - Unit Testing to ignore flush() and non-persisted mocked objects
body: <p>Attempting to unit test my Service models which are littered with flush commands to Doctrine for my processed entities. I'm getting errors that my new mocked objects aren't persisted and that a relationship doesn't have cascade options for the new entity.</p>
  
  <p>Upon seeing this I realise I also don't want my flush() operations to go to the DB as they're just unit tests on new entities.</p>
  
  <p>What can I do to get Doctrine to ignore mocked objects and ignore flush operations?</p>
  
  <p>Right now I'm getting this error: <code>Fatal error: Command failed: PHP Fatal error:  Call to a member function bindValue() on a non-object in /Users/person/Sites/project/vendor/doctrine/dbal/lib/Doctrine/DBAL/Connection.php on line 1311</code> with:</p>
  
  <pre><code>    // Nullify EM
      $sm-&gt;setAllowOverride(true);
      $sm-&gt;setFactory('Doctrine\ORM\EntityManager', function ($sm) {
  
          $eventManager = null;
          $metadataCache = new \Doctrine\Common\Cache\ArrayCache;
          $paths = $sm-&gt;get('Config')['doctrine']['driver']['application_entities']['paths'];
  
          $config = new \Doctrine\ORM\Configuration();
          $config-&gt;setMetadataCacheImpl($metadataCache);
          $config-&gt;setMetadataDriverImpl($config-&gt;newDefaultAnnotationDriver($paths, false));
          $config-&gt;setQueryCacheImpl(new \Doctrine\Common\Cache\ArrayCache);
          $config-&gt;setProxyDir(__DIR__ . '/Proxies');
          $config-&gt;setProxyNamespace('Doctrine\Tests\Proxies');
  
          $conn = array(
              'driverClass'  =&gt; 'Doctrine\Tests\Mocks\DriverMock',
              'wrapperClass' =&gt; 'Doctrine\Tests\Mocks\ConnectionMock',
              'user'         =&gt; 'john',
              'password'     =&gt; 'wayne'
          );
  
          $conn = \Doctrine\DBAL\DriverManager::getConnection($conn, $config, $eventManager);
  
          return \Doctrine\Tests\Mocks\EntityManagerMock::create($conn, $config, $eventManager);
  
  });
  </code></pre>
  
  <p>I can half fix the above by altering the Testing code of Doctrine, but that's not allowed.
  I've tried creating a Mock of the EntityManager to store within my ServiceManager with a mocked flush, but the EntityManager is a singleton, so returns a new instance of itself regardless. There's absolutely zero information regarding unit testing of services consuming Doctrine so at this point I'll just have to assume it's not actually possible.</p>