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
<?php
namespace Doctrine\Tests;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DriverManager;
use PHPUnit\Framework\Assert;
use function explode;
use function extension_loaded;
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;
/**
* Gets a <b>real</b> database connection using the following parameters
* of the $GLOBALS array:
*
* 'db_type' : The name of the Doctrine DBAL database driver to use.
* 'db_username' : 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_name' : 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.
*
* IMPORTANT: Each invocation of this method returns a NEW database connection.
*
* @return Connection The database connection instance.
*/
public static function getConnection()
{
$conn = DriverManager::getConnection(self::getConnectionParams());
self::addDbEventSubscribers($conn);
return $conn;
}
private static function getConnectionParams()
{
if (self::hasRequiredConnectionParams()) {
return self::getSpecifiedConnectionParams();
}
return self::getFallbackConnectionParams();
}
private static function hasRequiredConnectionParams()
{
return isset(
$GLOBALS['db_type'],
$GLOBALS['db_username'],
$GLOBALS['db_password'],
$GLOBALS['db_host'],
$GLOBALS['db_name'],
$GLOBALS['db_port']
)
&& isset(
$GLOBALS['tmpdb_type'],
$GLOBALS['tmpdb_username'],
$GLOBALS['tmpdb_password'],
$GLOBALS['tmpdb_host'],
$GLOBALS['tmpdb_port']
);
}
private static function getSpecifiedConnectionParams()
{
$realDbParams = self::getParamsForMainConnection();
$tmpDbParams = self::getParamsForTemporaryConnection();
$realConn = DriverManager::getConnection($realDbParams);
// Connect to tmpdb in order to drop and create the real test db.
$tmpConn = DriverManager::getConnection($tmpDbParams);
$platform = $tmpConn->getDatabasePlatform();
if (! self::$initialized) {
if ($platform->supportsCreateDropDatabase()) {
$dbname = $realConn->getDatabase();
$realConn->close();
$tmpConn->getSchemaManager()->dropAndCreateDatabase($dbname);
$tmpConn->close();
} else {
$sm = $realConn->getSchemaManager();
$schema = $sm->createSchema();
$stmts = $schema->toDropSql($realConn->getDatabasePlatform());
foreach ($stmts as $stmt) {
$realConn->exec($stmt);
}
}
self::$initialized = true;
}
return $realDbParams;
}
private static function getFallbackConnectionParams()
{
if (! extension_loaded('pdo_sqlite')) {
Assert::markTestSkipped('PDO SQLite extension is not loaded');
}
$params = [
'driver' => 'pdo_sqlite',
'memory' => true,
];
if (isset($GLOBALS['db_path'])) {
$params['path'] = $GLOBALS['db_path'];
unlink($GLOBALS['db_path']);
}
return $params;
}
private static function addDbEventSubscribers(Connection $conn)
{
if (! isset($GLOBALS['db_event_subscribers'])) {
return;
}
$evm = $conn->getEventManager();
foreach (explode(',', $GLOBALS['db_event_subscribers']) as $subscriberClass) {
$subscriberInstance = new $subscriberClass();
$evm->addEventSubscriber($subscriberInstance);
}
}
private static function getParamsForTemporaryConnection()
{
$connectionParams = [
'driver' => $GLOBALS['tmpdb_type'],
'user' => $GLOBALS['tmpdb_username'],
'password' => $GLOBALS['tmpdb_password'],
'host' => $GLOBALS['tmpdb_host'],
'dbname' => null,
'port' => $GLOBALS['tmpdb_port'],
];
if (isset($GLOBALS['tmpdb_name'])) {
$connectionParams['dbname'] = $GLOBALS['tmpdb_name'];
}
if (isset($GLOBALS['tmpdb_server'])) {
$connectionParams['server'] = $GLOBALS['tmpdb_server'];
}
if (isset($GLOBALS['tmpdb_unix_socket'])) {
$connectionParams['unix_socket'] = $GLOBALS['tmpdb_unix_socket'];
}
return $connectionParams;
}
private static function getParamsForMainConnection()
{
$connectionParams = [
'driver' => $GLOBALS['db_type'],
'user' => $GLOBALS['db_username'],
'password' => $GLOBALS['db_password'],
'host' => $GLOBALS['db_host'],
'dbname' => $GLOBALS['db_name'],
'port' => $GLOBALS['db_port'],
];
if (isset($GLOBALS['db_server'])) {
$connectionParams['server'] = $GLOBALS['db_server'];
}
if (isset($GLOBALS['db_unix_socket'])) {
$connectionParams['unix_socket'] = $GLOBALS['db_unix_socket'];
}
return $connectionParams;
}
/**
* @return Connection
*/
public static function getTempConnection()
{
return DriverManager::getConnection(self::getParamsForTemporaryConnection());
}
}