creamcheese
creamcheese

Reputation: 2544

Doctrine and ZF2 - Unit Testing to ignore flush() and non-persisted mocked objects

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.

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.

What can I do to get Doctrine to ignore mocked objects and ignore flush operations?

Right now I'm getting this error: 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 with:

    // Nullify EM
    $sm->setAllowOverride(true);
    $sm->setFactory('Doctrine\ORM\EntityManager', function ($sm) {

        $eventManager = null;
        $metadataCache = new \Doctrine\Common\Cache\ArrayCache;
        $paths = $sm->get('Config')['doctrine']['driver']['application_entities']['paths'];

        $config = new \Doctrine\ORM\Configuration();
        $config->setMetadataCacheImpl($metadataCache);
        $config->setMetadataDriverImpl($config->newDefaultAnnotationDriver($paths, false));
        $config->setQueryCacheImpl(new \Doctrine\Common\Cache\ArrayCache);
        $config->setProxyDir(__DIR__ . '/Proxies');
        $config->setProxyNamespace('Doctrine\Tests\Proxies');

        $conn = array(
            'driverClass'  => 'Doctrine\Tests\Mocks\DriverMock',
            'wrapperClass' => 'Doctrine\Tests\Mocks\ConnectionMock',
            'user'         => 'john',
            'password'     => 'wayne'
        );

        $conn = \Doctrine\DBAL\DriverManager::getConnection($conn, $config, $eventManager);

        return \Doctrine\Tests\Mocks\EntityManagerMock::create($conn, $config, $eventManager);

});

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.

Upvotes: 0

Views: 1284

Answers (1)

creamcheese
creamcheese

Reputation: 2544

OK, so after googling for days. I finally received an answer from Ocramius on how to actually test Doctrine consuming services: https://gist.github.com/Ocramius/3994325

Upvotes: 0

Related Questions