3 namespace Doctrine\Tests;
5 use Doctrine\Common\Cache\ArrayCache;
8 * Base testcase class for all ORM testcases.
10 abstract class OrmTestCase extends DoctrineTestCase
12 /** The metadata cache that is shared between all ORM tests (except functional tests). */
13 private static $_metadataCacheImpl = null;
15 /** The query cache that is shared between all ORM tests (except functional tests). */
16 private static $_queryCacheImpl = null;
20 * @return \Doctrine\ORM\Mapping\Driver\AnnotationDriver
22 protected function createAnnotationDriver($paths = array(), $alias = null)
24 if (version_compare(\Doctrine\Common\Version::VERSION, '3.0.0', '>=')) {
25 $reader = new \Doctrine\Common\Annotations\CachedReader(
26 new \Doctrine\Common\Annotations\AnnotationReader(), new ArrayCache()
29 else if (version_compare(\Doctrine\Common\Version::VERSION, '2.2.0-DEV', '>=')) {
30 // Register the ORM Annotations in the AnnotationRegistry
31 $reader = new \Doctrine\Common\Annotations\SimpleAnnotationReader();
32 $reader->addNamespace('Doctrine\ORM\Mapping');
33 $reader = new \Doctrine\Common\Annotations\CachedReader($reader, new ArrayCache());
35 else if (version_compare(\Doctrine\Common\Version::VERSION, '2.1.0-BETA3-DEV', '>=')) {
36 $reader = new \Doctrine\Common\Annotations\AnnotationReader();
37 $reader->setIgnoreNotImportedAnnotations(true);
38 $reader->setEnableParsePhpImports(false);
40 $reader->setAnnotationNamespaceAlias('Doctrine\ORM\Mapping\\', $alias);
42 $reader->setDefaultAnnotationNamespace('Doctrine\ORM\Mapping\\');
44 $reader = new \Doctrine\Common\Annotations\CachedReader(
45 new \Doctrine\Common\Annotations\IndexedReader($reader), new ArrayCache()
48 $reader = new \Doctrine\Common\Annotations\AnnotationReader();
50 $reader->setAnnotationNamespaceAlias('Doctrine\ORM\Mapping\\', $alias);
52 $reader->setDefaultAnnotationNamespace('Doctrine\ORM\Mapping\\');
55 \Doctrine\Common\Annotations\AnnotationRegistry::registerFile(
56 __DIR__ . "/../../../lib/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php");
57 return new \Doctrine\ORM\Mapping\Driver\AnnotationDriver($reader, (array)$paths);
61 * Creates an EntityManager for testing purposes.
63 * NOTE: The created EntityManager will have its dependant DBAL parts completely
64 * mocked out using a DriverMock, ConnectionMock, etc. These mocks can then
65 * be configured in the tests to simulate the DBAL behavior that is desired
66 * for a particular test,
68 * @return Doctrine\ORM\EntityManager
70 protected function _getTestEntityManager($conn = null, $conf = null, $eventManager = null, $withSharedMetadata = true)
72 $metadataCache = $withSharedMetadata
73 ? self::getSharedMetadataCacheImpl()
74 : new \Doctrine\Common\Cache\ArrayCache;
76 $config = new \Doctrine\ORM\Configuration();
78 $config->setMetadataCacheImpl($metadataCache);
79 $config->setMetadataDriverImpl($config->newDefaultAnnotationDriver(array(), true));
80 $config->setQueryCacheImpl(self::getSharedQueryCacheImpl());
81 $config->setProxyDir(__DIR__ . '/Proxies');
82 $config->setProxyNamespace('Doctrine\Tests\Proxies');
86 'driverClass' => 'Doctrine\Tests\Mocks\DriverMock',
87 'wrapperClass' => 'Doctrine\Tests\Mocks\ConnectionMock',
93 if (is_array($conn)) {
94 $conn = \Doctrine\DBAL\DriverManager::getConnection($conn, $config, $eventManager);
97 return \Doctrine\Tests\Mocks\EntityManagerMock::create($conn, $config, $eventManager);
100 private static function getSharedMetadataCacheImpl()
102 if (self::$_metadataCacheImpl === null) {
103 self::$_metadataCacheImpl = new \Doctrine\Common\Cache\ArrayCache;
106 return self::$_metadataCacheImpl;
109 private static function getSharedQueryCacheImpl()
111 if (self::$_queryCacheImpl === null) {
112 self::$_queryCacheImpl = new \Doctrine\Common\Cache\ArrayCache;
115 return self::$_queryCacheImpl;