Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / orm / tests / Doctrine / Tests / OrmTestCase.php
1 <?php
2
3 namespace Doctrine\Tests;
4
5 use Doctrine\Common\Cache\ArrayCache;
6
7 /**
8  * Base testcase class for all ORM testcases.
9  */
10 abstract class OrmTestCase extends DoctrineTestCase
11 {
12     /** The metadata cache that is shared between all ORM tests (except functional tests). */
13     private static $_metadataCacheImpl = null;
14
15     /** The query cache that is shared between all ORM tests (except functional tests). */
16     private static $_queryCacheImpl = null;
17
18     /**
19      * @param array $paths
20      * @return \Doctrine\ORM\Mapping\Driver\AnnotationDriver
21      */
22     protected function createAnnotationDriver($paths = array(), $alias = null)
23     {
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()
27             );
28         }
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());
34         }
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);
39             if ($alias) {
40                 $reader->setAnnotationNamespaceAlias('Doctrine\ORM\Mapping\\', $alias);
41             } else {
42                 $reader->setDefaultAnnotationNamespace('Doctrine\ORM\Mapping\\');
43             }
44             $reader = new \Doctrine\Common\Annotations\CachedReader(
45                 new \Doctrine\Common\Annotations\IndexedReader($reader), new ArrayCache()
46             );
47         } else {
48             $reader = new \Doctrine\Common\Annotations\AnnotationReader();
49             if ($alias) {
50                 $reader->setAnnotationNamespaceAlias('Doctrine\ORM\Mapping\\', $alias);
51             } else {
52                 $reader->setDefaultAnnotationNamespace('Doctrine\ORM\Mapping\\');
53             }
54         }
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);
58     }
59
60     /**
61      * Creates an EntityManager for testing purposes.
62      *
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,
67      *
68      * @return Doctrine\ORM\EntityManager
69      */
70     protected function _getTestEntityManager($conn = null, $conf = null, $eventManager = null, $withSharedMetadata = true)
71     {
72         $metadataCache = $withSharedMetadata
73             ? self::getSharedMetadataCacheImpl()
74             : new \Doctrine\Common\Cache\ArrayCache;
75
76         $config = new \Doctrine\ORM\Configuration();
77
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');
83
84         if ($conn === null) {
85             $conn = array(
86                 'driverClass'  => 'Doctrine\Tests\Mocks\DriverMock',
87                 'wrapperClass' => 'Doctrine\Tests\Mocks\ConnectionMock',
88                 'user'         => 'john',
89                 'password'     => 'wayne'
90             );
91         }
92
93         if (is_array($conn)) {
94             $conn = \Doctrine\DBAL\DriverManager::getConnection($conn, $config, $eventManager);
95         }
96
97         return \Doctrine\Tests\Mocks\EntityManagerMock::create($conn, $config, $eventManager);
98     }
99
100     private static function getSharedMetadataCacheImpl()
101     {
102         if (self::$_metadataCacheImpl === null) {
103             self::$_metadataCacheImpl = new \Doctrine\Common\Cache\ArrayCache;
104         }
105
106         return self::$_metadataCacheImpl;
107     }
108
109     private static function getSharedQueryCacheImpl()
110     {
111         if (self::$_queryCacheImpl === null) {
112             self::$_queryCacheImpl = new \Doctrine\Common\Cache\ArrayCache;
113         }
114
115         return self::$_queryCacheImpl;
116     }
117 }