@@ -21,14 +21,14 @@ class TestUtil
21
21
* Gets a <b>real</b> database connection using the following parameters
22
22
* of the $GLOBALS array:
23
23
*
24
- * 'db_type' : The name of the Doctrine DBAL database driver to use.
25
- * 'db_username' : The username to use for connecting.
26
- * 'db_password' : The password to use for connecting.
27
- * 'db_host' : The hostname of the database to connect to.
28
- * 'db_server' : The server name of the database to connect to
29
- * (optional, some vendors allow multiple server instances with different names on the same host).
30
- * 'db_name' : The name of the database to connect to.
31
- * 'db_port' : The port of the database to connect to.
24
+ * 'db_driver': The name of the Doctrine DBAL database driver to use.
25
+ * 'db_user': The username to use for connecting.
26
+ * 'db_password': The password to use for connecting.
27
+ * 'db_host': The hostname of the database to connect to.
28
+ * 'db_server': The server name of the database to connect to
29
+ * (optional, some vendors allow multiple server instances with different names on the same host).
30
+ * 'db_dbname': The name of the database to connect to.
31
+ * 'db_port': The port of the database to connect to.
32
32
*
33
33
* Usually these variables of the $GLOBALS array are filled by PHPUnit based
34
34
* on an XML configuration file. If no such parameters exist, an SQLite
@@ -66,21 +66,7 @@ public static function getConnectionParams() : array
66
66
67
67
private static function hasRequiredConnectionParams () : bool
68
68
{
69
- return isset (
70
- $ GLOBALS ['db_type ' ],
71
- $ GLOBALS ['db_username ' ],
72
- $ GLOBALS ['db_password ' ],
73
- $ GLOBALS ['db_host ' ],
74
- $ GLOBALS ['db_name ' ],
75
- $ GLOBALS ['db_port ' ]
76
- )
77
- && isset (
78
- $ GLOBALS ['tmpdb_type ' ],
79
- $ GLOBALS ['tmpdb_username ' ],
80
- $ GLOBALS ['tmpdb_password ' ],
81
- $ GLOBALS ['tmpdb_host ' ],
82
- $ GLOBALS ['tmpdb_port ' ]
83
- );
69
+ return isset ($ GLOBALS ['db_driver ' ]);
84
70
}
85
71
86
72
private static function initializeDatabase () : void
@@ -154,53 +140,51 @@ private static function addDbEventSubscribers(Connection $conn) : void
154
140
*/
155
141
private static function getParamsForTemporaryConnection () : array
156
142
{
157
- $ connectionParams = [
158
- 'driver ' => $ GLOBALS ['tmpdb_type ' ],
159
- 'user ' => $ GLOBALS ['tmpdb_username ' ],
160
- 'password ' => $ GLOBALS ['tmpdb_password ' ],
161
- 'host ' => $ GLOBALS ['tmpdb_host ' ],
162
- 'dbname ' => null ,
163
- 'port ' => $ GLOBALS ['tmpdb_port ' ],
164
- ];
165
-
166
- if (isset ($ GLOBALS ['tmpdb_name ' ])) {
167
- $ connectionParams ['dbname ' ] = $ GLOBALS ['tmpdb_name ' ];
143
+ if (isset ($ GLOBALS ['tmpdb_driver ' ])) {
144
+ self ::mapConnectionParameters ($ GLOBALS , 'tmpdb_ ' );
168
145
}
169
146
170
- if (isset ($ GLOBALS ['tmpdb_server ' ])) {
171
- $ connectionParams ['server ' ] = $ GLOBALS ['tmpdb_server ' ];
172
- }
147
+ $ parameters = self ::mapConnectionParameters ($ GLOBALS , 'db_ ' );
148
+ unset($ parameters ['dbname ' ]);
173
149
174
- if (isset ($ GLOBALS ['tmpdb_unix_socket ' ])) {
175
- $ connectionParams ['unix_socket ' ] = $ GLOBALS ['tmpdb_unix_socket ' ];
176
- }
177
-
178
- return $ connectionParams ;
150
+ return $ parameters ;
179
151
}
180
152
181
153
/**
182
154
* @return mixed[]
183
155
*/
184
156
private static function getParamsForMainConnection () : array
185
157
{
186
- $ connectionParams = [
187
- 'driver ' => $ GLOBALS ['db_type ' ],
188
- 'user ' => $ GLOBALS ['db_username ' ],
189
- 'password ' => $ GLOBALS ['db_password ' ],
190
- 'host ' => $ GLOBALS ['db_host ' ],
191
- 'dbname ' => $ GLOBALS ['db_name ' ],
192
- 'port ' => $ GLOBALS ['db_port ' ],
193
- ];
158
+ return self ::mapConnectionParameters ($ GLOBALS , 'db_ ' );
159
+ }
194
160
195
- if (isset ($ GLOBALS ['db_server ' ])) {
196
- $ connectionParams ['server ' ] = $ GLOBALS ['db_server ' ];
197
- }
161
+ /**
162
+ * @param array<string,mixed> $configuration
163
+ *
164
+ * @return array<string,mixed>
165
+ */
166
+ private static function mapConnectionParameters (array $ configuration , string $ prefix ) : array
167
+ {
168
+ $ parameters = [];
169
+
170
+ foreach ([
171
+ 'driver ' ,
172
+ 'user ' ,
173
+ 'password ' ,
174
+ 'host ' ,
175
+ 'dbname ' ,
176
+ 'port ' ,
177
+ 'server ' ,
178
+ 'unix_socket ' ,
179
+ ] as $ parameter ) {
180
+ if (! isset ($ configuration [$ prefix . $ parameter ])) {
181
+ continue ;
182
+ }
198
183
199
- if (isset ($ GLOBALS ['db_unix_socket ' ])) {
200
- $ connectionParams ['unix_socket ' ] = $ GLOBALS ['db_unix_socket ' ];
184
+ $ parameters [$ parameter ] = $ configuration [$ prefix . $ parameter ];
201
185
}
202
186
203
- return $ connectionParams ;
187
+ return $ parameters ;
204
188
}
205
189
206
190
public static function getTempConnection () : Connection
0 commit comments