Last active
December 12, 2015 03:48
-
-
Save tystr/4709093 to your computer and use it in GitHub Desktop.
Simple trait for bootstrapping the Doctrine ODM in unit tests.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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