Skip to content

Instantly share code, notes, and snippets.

@tystr
Last active December 12, 2015 03:48
Show Gist options
  • Select an option

  • Save tystr/4709093 to your computer and use it in GitHub Desktop.

Select an option

Save tystr/4709093 to your computer and use it in GitHub Desktop.
Simple trait for bootstrapping the Doctrine ODM in unit tests.
<?php
use Doctrine\ODM\MongoDB\Configuration;
use Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\AnnotationRegistry;
use Doctrine\MongoDB\Connection;
/**
* Bootstraps the Doctrine MongoDB ODM
*
* The ODM is configured for annotation mapping, and the document manager exposed via a protected property
*
* @author Tyler Stroud <tyler@tylerstroud.com>
*/
trait DoctrineMongoDBODMBootstrapTrait
{
protected $dm;
public function bootstrapODM()
{
if (!is_dir(__DIR__.'/cache')) {
mkdir(__DIR__.'/cache/');
}
$config = new Configuration();
$config->setDefaultDB('my_test_database');
$config->setProxyDir(__DIR__.'/cache');
$config->setProxyNamespace('Proxies');
$config->setHydratorDir(__DIR__.'/cache');
$config->setHydratorNamespace('Hydrators');
$reader = new AnnotationReader();
AnnotationRegistry::registerAutoloadNamespace(
'Doctrine\ODM\MongoDB\Mapping\Annotations',
__DIR__.'/../../../vendor/doctrine/mongodb-odm/lib'
);
$config->setMetadataDriverImpl(new AnnotationDriver($reader));
$this->dm = DocumentManager::create(new Connection(), $config);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment