-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathTestUtil.php
270 lines (227 loc) · 8.17 KB
/
TestUtil.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
<?php
namespace Doctrine\DBAL\Tests;
use Doctrine\Common\EventSubscriber;
use Doctrine\DBAL\Configuration;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Exception\DatabaseObjectNotFoundException;
use Doctrine\DBAL\Logging;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\OraclePlatform;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use Monolog\Processor\PsrLogMessageProcessor;
use PHPUnit\Framework\Assert;
use function array_keys;
use function array_map;
use function array_values;
use function explode;
use function extension_loaded;
use function file_exists;
use function implode;
use function in_array;
use function is_string;
use function strlen;
use function strpos;
use function substr;
use function unlink;
/**
* TestUtil is a class with static utility methods used during tests.
*/
class TestUtil
{
/** @var bool Whether the database schema is initialized. */
private static $initialized = false;
/**
* Creates a new <b>test</b> database connection using the following parameters
* of the $GLOBALS array:
*
* 'db_driver': The name of the Doctrine DBAL database driver to use.
* 'db_user': The username to use for connecting.
* 'db_password': The password to use for connecting.
* 'db_host': The hostname of the database to connect to.
* 'db_server': The server name of the database to connect to
* (optional, some vendors allow multiple server instances with different names on the same host).
* 'db_dbname': The name of the database to connect to.
* 'db_port': The port of the database to connect to.
*
* Usually these variables of the $GLOBALS array are filled by PHPUnit based
* on an XML configuration file. If no such parameters exist, an SQLite
* in-memory database is used.
*
* @return Connection The database connection instance.
*/
public static function getConnection(): Connection
{
if (self::hasRequiredConnectionParams() && ! self::$initialized) {
self::initializeDatabase();
self::$initialized = true;
}
$logger = new Logger('PHPUnit');
$logger->pushHandler(new StreamHandler('/tmp/dbal.log', Logger::DEBUG));
$logger->pushProcessor(new PsrLogMessageProcessor(null, true));
$configuration = (new Configuration())->setMiddlewares([new Logging\Middleware($logger)]);
$conn = DriverManager::getConnection(self::getConnectionParams(), $configuration);
self::addDbEventSubscribers($conn);
return $conn;
}
/**
* @return mixed[]
*/
public static function getConnectionParams(): array
{
if (self::hasRequiredConnectionParams()) {
return self::getTestConnectionParameters();
}
return self::getFallbackConnectionParams();
}
private static function hasRequiredConnectionParams(): bool
{
return isset($GLOBALS['db_driver']);
}
private static function initializeDatabase(): void
{
$testConnParams = self::getTestConnectionParameters();
$privConnParams = self::getPrivilegedConnectionParameters();
// Connect as a privileged user to create and drop the test database.
$privConn = DriverManager::getConnection($privConnParams);
$platform = $privConn->getDatabasePlatform();
if ($platform->supportsCreateDropDatabase()) {
if (! $platform instanceof OraclePlatform) {
$dbname = $testConnParams['dbname'];
} else {
$dbname = $testConnParams['user'];
}
$sm = $privConn->getSchemaManager();
try {
$sm->dropDatabase($dbname);
} catch (DatabaseObjectNotFoundException $e) {
}
$sm->createDatabase($dbname);
} elseif (isset($testConnParams['path'])) {
if (file_exists($testConnParams['path'])) {
unlink($testConnParams['path']);
}
} else {
$testConn = DriverManager::getConnection($testConnParams);
$sm = $testConn->getSchemaManager();
$schema = $sm->createSchema();
$stmts = $schema->toDropSql($testConn->getDatabasePlatform());
foreach ($stmts as $stmt) {
$testConn->executeStatement($stmt);
}
$testConn->close();
}
$privConn->close();
}
/**
* @return mixed[]
*/
private static function getFallbackConnectionParams(): array
{
if (! extension_loaded('pdo_sqlite')) {
Assert::markTestSkipped('PDO SQLite extension is not loaded');
}
return [
'driver' => 'pdo_sqlite',
'memory' => true,
];
}
private static function addDbEventSubscribers(Connection $conn): void
{
if (! isset($GLOBALS['db_event_subscribers'])) {
return;
}
$evm = $conn->getEventManager();
/** @psalm-var class-string<EventSubscriber> $subscriberClass */
foreach (explode(',', $GLOBALS['db_event_subscribers']) as $subscriberClass) {
$subscriberInstance = new $subscriberClass();
$evm->addEventSubscriber($subscriberInstance);
}
}
/**
* @return mixed[]
*/
private static function getPrivilegedConnectionParameters(): array
{
if (isset($GLOBALS['tmpdb_driver'])) {
return self::mapConnectionParameters($GLOBALS, 'tmpdb_');
}
$parameters = self::mapConnectionParameters($GLOBALS, 'db_');
unset($parameters['dbname']);
return $parameters;
}
/**
* @return mixed[]
*/
private static function getTestConnectionParameters(): array
{
return self::mapConnectionParameters($GLOBALS, 'db_');
}
/**
* @param array<string,mixed> $configuration
*
* @return array<string,mixed>
*/
private static function mapConnectionParameters(array $configuration, string $prefix): array
{
$parameters = [];
foreach (
[
'driver',
'user',
'password',
'host',
'dbname',
'port',
'server',
'ssl_key',
'ssl_cert',
'ssl_ca',
'ssl_capath',
'ssl_cipher',
'unix_socket',
'path',
'charset',
] as $parameter
) {
if (! isset($configuration[$prefix . $parameter])) {
continue;
}
$parameters[$parameter] = $configuration[$prefix . $parameter];
}
foreach ($configuration as $param => $value) {
if (strpos($param, $prefix . 'driver_option_') !== 0) {
continue;
}
$parameters['driverOptions'][substr($param, strlen($prefix . 'driver_option_'))] = $value;
}
return $parameters;
}
public static function getPrivilegedConnection(): Connection
{
return DriverManager::getConnection(self::getPrivilegedConnectionParameters());
}
public static function isDriverOneOf(string ...$names): bool
{
return in_array(self::getConnectionParams()['driver'], $names, true);
}
/**
* Generates a query that will return the given rows without the need to create a temporary table.
*
* @param array<int,array<string,mixed>> $rows
*/
public static function generateResultSetQuery(array $rows, AbstractPlatform $platform): string
{
return implode(' UNION ALL ', array_map(static function (array $row) use ($platform): string {
return $platform->getDummySelectSQL(
implode(', ', array_map(static function (string $column, $value) use ($platform): string {
if (is_string($value)) {
$value = $platform->quoteStringLiteral($value);
}
return $value . ' ' . $platform->quoteIdentifier($column);
}, array_keys($row), array_values($row)))
);
}, $rows));
}
}