Commit aa56a440 authored by Marco Pivetta's avatar Marco Pivetta

Merge pull request #597 from JeroenDeDauw/cleantestsetup

Some much needed cleanup in the TestUtil class
parents cf35f193 b71e44a9
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
namespace Doctrine\Tests; namespace Doctrine\Tests;
use Doctrine\DBAL\DriverManager;
/** /**
* TestUtil is a class with static utility methods used during tests. * TestUtil is a class with static utility methods used during tests.
* *
...@@ -34,51 +36,14 @@ class TestUtil ...@@ -34,51 +36,14 @@ class TestUtil
*/ */
public static function getConnection() public static function getConnection()
{ {
if (isset($GLOBALS['db_type'], $GLOBALS['db_username'], $GLOBALS['db_password'], if (self::hasRequiredConnectionParams()) {
$GLOBALS['db_host'], $GLOBALS['db_name'], $GLOBALS['db_port']) && $realDbParams = self::getConnectionParams();
isset($GLOBALS['tmpdb_type'], $GLOBALS['tmpdb_username'], $GLOBALS['tmpdb_password'], $tmpDbParams = self::getTmpConnectionParams();
$GLOBALS['tmpdb_host'], $GLOBALS['tmpdb_port'])) {
$realDbParams = array(
'driver' => $GLOBALS['db_type'],
'user' => $GLOBALS['db_username'],
'password' => $GLOBALS['db_password'],
'host' => $GLOBALS['db_host'],
'dbname' => $GLOBALS['db_name'],
'port' => $GLOBALS['db_port']
);
$tmpDbParams = array(
'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['db_server'])) { $realConn = DriverManager::getConnection($realDbParams);
$realDbParams['server'] = $GLOBALS['db_server'];
}
if (isset($GLOBALS['db_unix_socket'])) {
$realDbParams['unix_socket'] = $GLOBALS['db_unix_socket'];
}
if (isset($GLOBALS['tmpdb_name'])) {
$tmpDbParams['dbname'] = $GLOBALS['tmpdb_name'];
}
if (isset($GLOBALS['tmpdb_server'])) {
$tmpDbParams['server'] = $GLOBALS['tmpdb_server'];
}
if (isset($GLOBALS['tmpdb_unix_socket'])) {
$tmpDbParams['unix_socket'] = $GLOBALS['tmpdb_unix_socket'];
}
$realConn = \Doctrine\DBAL\DriverManager::getConnection($realDbParams);
// Connect to tmpdb in order to drop and create the real test db. // Connect to tmpdb in order to drop and create the real test db.
$tmpConn = \Doctrine\DBAL\DriverManager::getConnection($tmpDbParams); $tmpConn = DriverManager::getConnection($tmpDbParams);
$platform = $tmpConn->getDatabasePlatform(); $platform = $tmpConn->getDatabasePlatform();
...@@ -92,7 +57,6 @@ class TestUtil ...@@ -92,7 +57,6 @@ class TestUtil
} else { } else {
$sm = $realConn->getSchemaManager(); $sm = $realConn->getSchemaManager();
/* @var $schema Schema */
$schema = $sm->createSchema(); $schema = $sm->createSchema();
$stmts = $schema->toDropSql($realConn->getDatabasePlatform()); $stmts = $schema->toDropSql($realConn->getDatabasePlatform());
...@@ -101,7 +65,7 @@ class TestUtil ...@@ -101,7 +65,7 @@ class TestUtil
} }
} }
$conn = \Doctrine\DBAL\DriverManager::getConnection($realDbParams, null, null); $conn = DriverManager::getConnection($realDbParams, null, null);
} else { } else {
$params = array( $params = array(
'driver' => 'pdo_sqlite', 'driver' => 'pdo_sqlite',
...@@ -111,7 +75,7 @@ class TestUtil ...@@ -111,7 +75,7 @@ class TestUtil
$params['path'] = $GLOBALS['db_path']; $params['path'] = $GLOBALS['db_path'];
unlink($GLOBALS['db_path']); unlink($GLOBALS['db_path']);
} }
$conn = \Doctrine\DBAL\DriverManager::getConnection($params); $conn = DriverManager::getConnection($params);
} }
if (isset($GLOBALS['db_event_subscribers'])) { if (isset($GLOBALS['db_event_subscribers'])) {
...@@ -125,25 +89,78 @@ class TestUtil ...@@ -125,25 +89,78 @@ class TestUtil
return $conn; return $conn;
} }
/** private static function hasRequiredConnectionParams()
* @return \Doctrine\DBAL\Connection {
*/ return isset(
public static function getTempConnection() $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 getTmpConnectionParams()
{ {
$tmpDbParams = array( $connectionParams = array(
'driver' => $GLOBALS['tmpdb_type'], 'driver' => $GLOBALS['tmpdb_type'],
'user' => $GLOBALS['tmpdb_username'], 'user' => $GLOBALS['tmpdb_username'],
'password' => $GLOBALS['tmpdb_password'], 'password' => $GLOBALS['tmpdb_password'],
'host' => $GLOBALS['tmpdb_host'], 'host' => $GLOBALS['tmpdb_host'],
'dbname' => $GLOBALS['tmpdb_name'], 'dbname' => null,
'port' => $GLOBALS['tmpdb_port'] 'port' => $GLOBALS['tmpdb_port']
); );
if (isset($GLOBALS['tmpdb_name'])) {
$connectionParams['dbname'] = $GLOBALS['tmpdb_name'];
}
if (isset($GLOBALS['tmpdb_server'])) { if (isset($GLOBALS['tmpdb_server'])) {
$tmpDbParams['server'] = $GLOBALS['tmpdb_server']; $connectionParams['server'] = $GLOBALS['tmpdb_server'];
} }
// Connect to tmpdb in order to drop and create the real test db. if (isset($GLOBALS['tmpdb_unix_socket'])) {
return \Doctrine\DBAL\DriverManager::getConnection($tmpDbParams); $connectionParams['unix_socket'] = $GLOBALS['tmpdb_unix_socket'];
}
return $connectionParams;
}
private static function getConnectionParams()
{
$connectionParams = array(
'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 \Doctrine\DBAL\Connection
*/
public static function getTempConnection()
{
return DriverManager::getConnection(self::getTmpConnectionParams());
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment