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