$loader = (require $loaderPath); // Set up class loading. $loader->add('Entities', __DIR__); $loader->add('Proxies', __DIR__); $debug = true; $config = new \Doctrine\ORM\Configuration(); // Set up Metadata Drivers $driverImpl = $config->newDefaultAnnotationDriver(array(__DIR__ . "/Entities")); $config->setMetadataDriverImpl($driverImpl); // Set up caches, depending on $debug variable. // You can use another variable to define which one of the cache systems you gonna use. $cache = $debug ? new Cache\ArrayCache() : new Cache\ApcCache(); $config->setMetadataCacheImpl($cache); $config->setQueryCacheImpl($cache); // Proxy configuration $config->setProxyDir(__DIR__ . '/Proxies'); $config->setProxyNamespace('Proxies'); // Database connection information $connectionOptions = array('driver' => 'pdo_sqlite', 'path' => 'database.sqlite'); // Enable second-level cache $cacheConfig = new \Doctrine\ORM\Cache\CacheConfiguration(); $cacheDriver = $debug ? new Cache\ArrayCache() : new Cache\ApcCache(); $cacheLogger = new \Doctrine\ORM\Cache\Logging\StatisticsCacheLogger(); $factory = new \Doctrine\ORM\Cache\DefaultCacheFactory($cacheConfig->getRegionsConfiguration(), $cacheDriver); if ($debug) { $cacheConfig->setCacheLogger($cacheLogger); } $cacheConfig->setCacheFactory($factory); $config->setSecondLevelCacheEnabled(true); $config->setSecondLevelCacheConfiguration($cacheConfig); return EntityManager::create($connectionOptions, $config);
/** * Creates an EntityManager for testing purposes. * * NOTE: The created EntityManager will have its dependant DBAL parts completely * mocked out using a DriverMock, ConnectionMock, etc. These mocks can then * be configured in the tests to simulate the DBAL behavior that is desired * for a particular test, * * @param \Doctrine\DBAL\Connection|array $conn * @param mixed $conf * @param \Doctrine\Common\EventManager|null $eventManager * @param bool $withSharedMetadata * * @return \Doctrine\ORM\EntityManager */ protected function _getTestEntityManager($conn = null, $conf = null, $eventManager = null, $withSharedMetadata = true) { $metadataCache = $withSharedMetadata ? self::getSharedMetadataCacheImpl() : new \Doctrine\Common\Cache\ArrayCache(); $config = new \Doctrine\ORM\Configuration(); $config->setMetadataCacheImpl($metadataCache); $config->setMetadataDriverImpl($config->newDefaultAnnotationDriver(array(), true)); $config->setQueryCacheImpl(self::getSharedQueryCacheImpl()); $config->setProxyDir(__DIR__ . '/Proxies'); $config->setProxyNamespace('Doctrine\\Tests\\Proxies'); $config->setMetadataDriverImpl($config->newDefaultAnnotationDriver(array(realpath(__DIR__ . '/Models/Cache')), true)); if ($this->isSecondLevelCacheEnabled) { $cacheConfig = new \Doctrine\ORM\Cache\CacheConfiguration(); $cache = $this->getSharedSecondLevelCacheDriverImpl(); $factory = new DefaultCacheFactory($cacheConfig->getRegionsConfiguration(), $cache); $this->secondLevelCacheFactory = $factory; $cacheConfig->setCacheFactory($factory); $config->setSecondLevelCacheEnabled(true); $config->setSecondLevelCacheConfiguration($cacheConfig); } if ($conn === null) { $conn = array('driverClass' => 'Doctrine\\Tests\\Mocks\\DriverMock', 'wrapperClass' => 'Doctrine\\Tests\\Mocks\\ConnectionMock', 'user' => 'john', 'password' => 'wayne'); } if (is_array($conn)) { $conn = \Doctrine\DBAL\DriverManager::getConnection($conn, $config, $eventManager); } return \Doctrine\Tests\Mocks\EntityManagerMock::create($conn, $config, $eventManager); }
/** * Gets the 'doctrine.orm.default_entity_manager' service. * * This service is shared. * This method always returns the same instance of the service. * * @return \Doctrine\ORM\EntityManager A Doctrine\ORM\EntityManager instance. */ protected function getDoctrine_Orm_DefaultEntityManagerService() { $a = new \Doctrine\ORM\Cache\Logging\CacheLoggerChain(); $a->setLogger('statistics', new \Doctrine\ORM\Cache\Logging\StatisticsCacheLogger()); $b = new \Doctrine\ORM\Cache\CacheConfiguration(); $b->setCacheLogger($a); $b->setCacheFactory(new \Doctrine\ORM\Cache\DefaultCacheFactory($this->get('doctrine.orm.default_second_level_cache.regions_configuration'), $this->get('doctrine_cache.providers.doctrine.orm.default_second_level_cache.region_cache_driver'))); $b->setRegionsConfiguration(new \Doctrine\ORM\Cache\RegionsConfiguration()); $c = new \Doctrine\Common\Persistence\Mapping\Driver\MappingDriverChain(); $c->addDriver(new \Doctrine\ORM\Mapping\Driver\AnnotationDriver($this->get('annotation_reader'), array(0 => $this->targetDirs[3] . '\\src\\Laiso\\ArmBundle\\Entity')), 'Laiso\\ArmBundle\\Entity'); $c->addDriver(new \Doctrine\ORM\Mapping\Driver\XmlDriver(new \Doctrine\Common\Persistence\Mapping\Driver\SymfonyFileLocator(array($this->targetDirs[3] . '\\vendor\\friendsofsymfony\\user-bundle\\Resources\\config\\doctrine-mapping' => 'FOS\\UserBundle\\Model'), '.orm.xml')), 'FOS\\UserBundle\\Model'); $d = new \Doctrine\ORM\Configuration(); $d->setEntityNamespaces(array('LaisoArmBundle' => 'Laiso\\ArmBundle\\Entity')); $d->setSecondLevelCacheEnabled(true); $d->setSecondLevelCacheConfiguration($b); $d->setMetadataCacheImpl($this->get('doctrine_cache.providers.doctrine.orm.default_metadata_cache')); $d->setQueryCacheImpl($this->get('doctrine_cache.providers.doctrine.orm.default_query_cache')); $d->setResultCacheImpl($this->get('doctrine_cache.providers.doctrine.orm.default_result_cache')); $d->setMetadataDriverImpl($c); $d->setProxyDir(__DIR__ . '/doctrine/orm/Proxies'); $d->setProxyNamespace('Proxies'); $d->setAutoGenerateProxyClasses(true); $d->setClassMetadataFactoryName('Doctrine\\ORM\\Mapping\\ClassMetadataFactory'); $d->setDefaultRepositoryClassName('Doctrine\\ORM\\EntityRepository'); $d->setNamingStrategy(new \Doctrine\ORM\Mapping\UnderscoreNamingStrategy()); $d->setQuoteStrategy(new \Doctrine\ORM\Mapping\DefaultQuoteStrategy()); $d->setEntityListenerResolver($this->get('doctrine.orm.default_entity_listener_resolver')); $this->services['doctrine.orm.default_entity_manager'] = $instance = \Doctrine\ORM\EntityManager::create($this->get('doctrine.dbal.default_connection'), $d); $this->get('doctrine.orm.default_manager_configurator')->configure($instance); return $instance; }
/** * Gets an EntityManager for testing purposes. * * @param \Doctrine\ORM\Configuration $config The Configuration to pass to the EntityManager. * @param \Doctrine\Common\EventManager $eventManager The EventManager to pass to the EntityManager. * * @return \Doctrine\ORM\EntityManager */ protected function _getEntityManager($config = null, $eventManager = null) { // NOTE: Functional tests use their own shared metadata cache, because // the actual database platform used during execution has effect on some // metadata mapping behaviors (like the choice of the ID generation). if (is_null(self::$_metadataCacheImpl)) { if (isset($GLOBALS['DOCTRINE_CACHE_IMPL'])) { self::$_metadataCacheImpl = new $GLOBALS['DOCTRINE_CACHE_IMPL'](); } else { self::$_metadataCacheImpl = new \Doctrine\Common\Cache\ArrayCache(); } } if (is_null(self::$_queryCacheImpl)) { self::$_queryCacheImpl = new \Doctrine\Common\Cache\ArrayCache(); } $this->_sqlLoggerStack = new \Doctrine\DBAL\Logging\DebugStack(); $this->_sqlLoggerStack->enabled = false; //FIXME: two different configs! $conn and the created entity manager have // different configs. $config = new \Doctrine\ORM\Configuration(); $config->setMetadataCacheImpl(self::$_metadataCacheImpl); $config->setQueryCacheImpl(self::$_queryCacheImpl); $config->setProxyDir(__DIR__ . '/Proxies'); $config->setProxyNamespace('Doctrine\\Tests\\Proxies'); $enableSecondLevelCache = getenv('ENABLE_SECOND_LEVEL_CACHE'); if ($this->isSecondLevelCacheEnabled || $enableSecondLevelCache) { $cacheConfig = new \Doctrine\ORM\Cache\CacheConfiguration(); $cache = $this->getSharedSecondLevelCacheDriverImpl(); $factory = new DefaultCacheFactory($cacheConfig->getRegionsConfiguration(), $cache); $this->secondLevelCacheFactory = $factory; if ($this->isSecondLevelCacheLogEnabled) { $this->secondLevelCacheLogger = new StatisticsCacheLogger(); $cacheConfig->setCacheLogger($this->secondLevelCacheLogger); } $cacheConfig->setCacheFactory($factory); $config->setSecondLevelCacheEnabled(true); $config->setSecondLevelCacheConfiguration($cacheConfig); $this->isSecondLevelCacheEnabled = true; } $config->setMetadataDriverImpl($config->newDefaultAnnotationDriver(array(realpath(__DIR__ . '/Models/Cache'), realpath(__DIR__ . '/Models/GeoNames')), true)); $conn = static::$_sharedConn; $conn->getConfiguration()->setSQLLogger($this->_sqlLoggerStack); // get rid of more global state $evm = $conn->getEventManager(); foreach ($evm->getListeners() as $event => $listeners) { foreach ($listeners as $listener) { $evm->removeEventListener(array($event), $listener); } } if ($enableSecondLevelCache) { $evm->addEventListener('loadClassMetadata', new CacheMetadataListener()); } if (isset($GLOBALS['db_event_subscribers'])) { foreach (explode(",", $GLOBALS['db_event_subscribers']) as $subscriberClass) { $subscriberInstance = new $subscriberClass(); $evm->addEventSubscriber($subscriberInstance); } } if (isset($GLOBALS['debug_uow_listener'])) { $evm->addEventListener(array('onFlush'), new \Doctrine\ORM\Tools\DebugUnitOfWorkListener()); } return \Doctrine\ORM\EntityManager::create($conn, $config); }