-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
OrmTestCase.php
193 lines (157 loc) · 6.25 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\DBAL\Connection;
use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Driver\Result;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Schema\AbstractSchemaManager;
use Doctrine\DBAL\Schema\SchemaConfig;
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\AttributeDriver;
use Doctrine\Tests\Mocks\EntityManagerMock;
use Psr\Cache\CacheItemPoolInterface;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use function method_exists;
use function realpath;
use function sprintf;
/**
* 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).
*/
private static CacheItemPoolInterface|null $metadataCache = null;
/**
* The query cache that is shared between all ORM tests (except functional tests).
*/
private static CacheItemPoolInterface|null $queryCache = null;
/** @var bool */
protected $isSecondLevelCacheEnabled = false;
/** @var bool */
protected $isSecondLevelCacheLogEnabled = false;
/** @var CacheFactory */
protected $secondLevelCacheFactory;
/** @var StatisticsCacheLogger */
protected $secondLevelCacheLogger;
private CacheItemPoolInterface|null $secondLevelCache = null;
protected function createAttributeDriver(array $paths = []): AttributeDriver
{
return new AttributeDriver($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,
*/
protected function getTestEntityManager(): EntityManagerMock
{
return $this->buildTestEntityManagerWithPlatform(
$this->createConnectionMock($this->createPlatformMock()),
);
}
protected function createTestEntityManagerWithConnection(Connection $connection): EntityManagerMock
{
return $this->buildTestEntityManagerWithPlatform($connection);
}
protected function createTestEntityManagerWithPlatform(AbstractPlatform $platform): EntityManagerMock
{
return $this->buildTestEntityManagerWithPlatform(
$this->createConnectionMock($platform),
);
}
private function buildTestEntityManagerWithPlatform(Connection $connection): EntityManagerMock
{
$metadataCache = self::getSharedMetadataCacheImpl();
$config = new Configuration();
TestUtil::configureProxies($config);
$config->setMetadataCache($metadataCache);
$config->setQueryCache(self::getSharedQueryCache());
$config->setMetadataDriverImpl(new AttributeDriver([
realpath(__DIR__ . '/Models/Cache'),
], true));
if ($this->isSecondLevelCacheEnabled) {
$cacheConfig = new CacheConfiguration();
$factory = new DefaultCacheFactory(
$cacheConfig->getRegionsConfiguration(),
$this->getSharedSecondLevelCache(),
);
$this->secondLevelCacheFactory = $factory;
$cacheConfig->setCacheFactory($factory);
$config->setSecondLevelCacheEnabled();
$config->setSecondLevelCacheConfiguration($cacheConfig);
}
return new EntityManagerMock($connection, $config);
}
protected function enableSecondLevelCache(bool $log = true): void
{
$this->isSecondLevelCacheEnabled = true;
$this->isSecondLevelCacheLogEnabled = $log;
}
private static function getSharedMetadataCacheImpl(): CacheItemPoolInterface
{
return self::$metadataCache
?? self::$metadataCache = new ArrayAdapter();
}
private static function getSharedQueryCache(): CacheItemPoolInterface
{
return self::$queryCache
?? self::$queryCache = new ArrayAdapter();
}
protected function getSharedSecondLevelCache(): CacheItemPoolInterface
{
return $this->secondLevelCache
?? $this->secondLevelCache = new ArrayAdapter();
}
private function createConnectionMock(AbstractPlatform $platform): Connection
{
$connection = $this->getMockBuilder(Connection::class)
->setConstructorArgs([[], $this->createDriverMock($platform)])
->onlyMethods(['quote'])
->getMockForAbstractClass();
$connection->method('quote')->willReturnCallback(static fn (string $input) => sprintf("'%s'", $input));
return $connection;
}
private function createPlatformMock(): AbstractPlatform
{
$schemaManager = $this->createMock(AbstractSchemaManager::class);
$schemaManager->method('createSchemaConfig')
->willReturn(new SchemaConfig());
$platform = $this->getMockBuilder(AbstractPlatform::class)
->onlyMethods(['supportsIdentityColumns', 'createSchemaManager'])
->getMockForAbstractClass();
$platform->method('supportsIdentityColumns')
->willReturn(true);
$platform->method('createSchemaManager')
->willReturn($schemaManager);
return $platform;
}
private function createDriverMock(AbstractPlatform $platform): Driver
{
$result = $this->createMock(Result::class);
$result->method('fetchAssociative')
->willReturn(false);
$connection = $this->createMock(Driver\Connection::class);
$connection->method('query')
->willReturn($result);
$driver = $this->createMock(Driver::class);
$driver->method('connect')
->willReturn($connection);
$driver->method('getDatabasePlatform')
->willReturn($platform);
if (method_exists(Driver::class, 'getSchemaManager')) {
$driver->method('getSchemaManager')
->willReturnCallback([$platform, 'createSchemaManager']);
}
return $driver;
}
}