-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
OrmTestCase.php
193 lines (160 loc) · 5.93 KB
/
OrmTestCase.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php
declare(strict_types=1);
namespace Doctrine\Tests;
use Doctrine\Common\Annotations;
use Doctrine\Common\Cache\ArrayCache;
use Doctrine\Common\Cache\Cache;
use Doctrine\Common\EventManager;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DriverManager;
use Doctrine\ORM\Cache\CacheConfiguration;
use Doctrine\ORM\Cache\CacheFactory;
use Doctrine\ORM\Cache\DefaultCacheFactory;
use Doctrine\ORM\Cache\Logging\StatisticsCacheLogger;
use Doctrine\ORM\Configuration;
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
use Doctrine\ORM\Proxy\Factory\ProxyFactory;
use function is_array;
use function realpath;
use ReflectionClass;
/**
* Base testcase class for all ORM testcases.
*/
abstract class OrmTestCase extends DoctrineTestCase
{
/**
* The metadata cache that is shared between all ORM tests (except functional tests).
*
* @var Cache|null
*/
private static $metadataCacheImpl = null;
/**
* The query cache that is shared between all ORM tests (except functional tests).
*
* @var Cache|null
*/
private static $queryCacheImpl = null;
/** @var bool */
protected $isSecondLevelCacheEnabled = false;
/** @var bool */
protected $isSecondLevelCacheLogEnabled = false;
/** @var CacheFactory */
protected $secondLevelCacheFactory;
/** @var StatisticsCacheLogger */
protected $secondLevelCacheLogger;
/** @var Cache|null */
protected $secondLevelCacheDriverImpl;
/**
* @param array $paths
*
* @return AnnotationDriver
*/
protected function createAnnotationDriver($paths = [])
{
$reader = new Annotations\CachedReader(new Annotations\AnnotationReader(), new ArrayCache());
Annotations\AnnotationRegistry::registerFile(__DIR__ . '/../../../lib/Doctrine/ORM/Annotation/DoctrineAnnotations.php');
return new AnnotationDriver($reader, (array) $paths);
}
/**
* 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 Connection|array $conn
* @param mixed $conf
* @param EventManager|null $eventManager
* @param bool $withSharedMetadata
*/
protected function getTestEntityManager(
$conn = null,
$conf = null,
$eventManager = null,
$withSharedMetadata = true
) : Mocks\EntityManagerMock {
$metadataCache = $withSharedMetadata
? self::getSharedMetadataCacheImpl()
: new ArrayCache();
$config = new Configuration();
$config->setMetadataCacheImpl($metadataCache);
$config->setMetadataDriverImpl($config->newDefaultAnnotationDriver([]));
$config->setQueryCacheImpl(self::getSharedQueryCacheImpl());
$config->setProxyNamespace('Doctrine\Tests\Proxies');
$config->setAutoGenerateProxyClasses(ProxyFactory::AUTOGENERATE_EVAL);
$config->setMetadataDriverImpl(
$config->newDefaultAnnotationDriver([realpath(__DIR__ . '/Models/Cache')])
);
if ($this->isSecondLevelCacheEnabled) {
$cacheConfig = new 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 = [
'driverClass' => Mocks\DriverMock::class,
'wrapperClass' => Mocks\ConnectionMock::class,
'user' => 'john',
'password' => 'wayne',
];
}
if (is_array($conn)) {
$conn = DriverManager::getConnection($conn, $config, $eventManager);
}
return Mocks\EntityManagerMock::create($conn, $config, $eventManager);
}
protected function enableSecondLevelCache($log = true)
{
$this->isSecondLevelCacheEnabled = true;
$this->isSecondLevelCacheLogEnabled = $log;
}
/**
* @return Cache
*/
private static function getSharedMetadataCacheImpl()
{
if (self::$metadataCacheImpl === null) {
self::$metadataCacheImpl = new ArrayCache();
}
return self::$metadataCacheImpl;
}
/**
* @return Cache
*/
private static function getSharedQueryCacheImpl()
{
if (self::$queryCacheImpl === null) {
self::$queryCacheImpl = new ArrayCache();
}
return self::$queryCacheImpl;
}
/**
* @return Cache
*/
protected function getSharedSecondLevelCacheDriverImpl()
{
if ($this->secondLevelCacheDriverImpl === null) {
$this->secondLevelCacheDriverImpl = new ArrayCache();
}
return $this->secondLevelCacheDriverImpl;
}
public static function assertAttributeEmpty(string $haystackAttributeName, $haystackClassOrObject, string $message = ''): void
{
$class = new ReflectionClass($haystackClassOrObject);
$property = $class->getProperty($haystackAttributeName);
$property->setAccessible(true);
self::assertEmpty($property->getValue($haystackClassOrObject));
}
public static function assertAttributeSame($expected, string $actualAttributeName, $actualClassOrObject, string $message = ''): void
{
$class = new ReflectionClass($actualClassOrObject);
$property = $class->getProperty($actualAttributeName);
$property->setAccessible(true);
self::assertSame($expected, $property->getValue($actualClassOrObject));
}
}