Commit a27733a0 authored by jeroendedauw's avatar jeroendedauw

Split the methods in TestUtil more and improve naming

parent 8dbf562d
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
namespace Doctrine\Tests; namespace Doctrine\Tests;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DriverManager; use Doctrine\DBAL\DriverManager;
/** /**
...@@ -32,13 +33,47 @@ class TestUtil ...@@ -32,13 +33,47 @@ class TestUtil
* 1) Each invocation of this method returns a NEW database connection. * 1) Each invocation of this method returns a NEW database connection.
* 2) The database is dropped and recreated to ensure it's clean. * 2) The database is dropped and recreated to ensure it's clean.
* *
* @return \Doctrine\DBAL\Connection The database connection instance. * @return Connection The database connection instance.
*/ */
public static function getConnection() public static function getConnection()
{ {
$conn = DriverManager::getConnection(self::getConnectionParams());
self::addDbEventSubscribers($conn);
return $conn;
}
private static function getConnectionParams() {
if (self::hasRequiredConnectionParams()) { if (self::hasRequiredConnectionParams()) {
$realDbParams = self::getConnectionParams(); return self::getSpecifiedConnectionParams();
$tmpDbParams = self::getTmpConnectionParams(); }
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); $realConn = DriverManager::getConnection($realDbParams);
...@@ -65,19 +100,24 @@ class TestUtil ...@@ -65,19 +100,24 @@ class TestUtil
} }
} }
$conn = DriverManager::getConnection($realDbParams, null, null); return $realDbParams;
} else { }
private static function getFallbackConnectionParams() {
$params = array( $params = array(
'driver' => 'pdo_sqlite', 'driver' => 'pdo_sqlite',
'memory' => true 'memory' => true
); );
if (isset($GLOBALS['db_path'])) { if (isset($GLOBALS['db_path'])) {
$params['path'] = $GLOBALS['db_path']; $params['path'] = $GLOBALS['db_path'];
unlink($GLOBALS['db_path']); unlink($GLOBALS['db_path']);
} }
$conn = DriverManager::getConnection($params);
return $params;
} }
private static function addDbEventSubscribers(Connection $conn) {
if (isset($GLOBALS['db_event_subscribers'])) { if (isset($GLOBALS['db_event_subscribers'])) {
$evm = $conn->getEventManager(); $evm = $conn->getEventManager();
foreach (explode(",", $GLOBALS['db_event_subscribers']) as $subscriberClass) { foreach (explode(",", $GLOBALS['db_event_subscribers']) as $subscriberClass) {
...@@ -85,30 +125,9 @@ class TestUtil ...@@ -85,30 +125,9 @@ class TestUtil
$evm->addEventSubscriber($subscriberInstance); $evm->addEventSubscriber($subscriberInstance);
} }
} }
return $conn;
}
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 getTmpConnectionParams() private static function getParamsForTemporaryConnection()
{ {
$connectionParams = array( $connectionParams = array(
'driver' => $GLOBALS['tmpdb_type'], 'driver' => $GLOBALS['tmpdb_type'],
...@@ -134,7 +153,7 @@ class TestUtil ...@@ -134,7 +153,7 @@ class TestUtil
return $connectionParams; return $connectionParams;
} }
private static function getConnectionParams() private static function getParamsForMainConnection()
{ {
$connectionParams = array( $connectionParams = array(
'driver' => $GLOBALS['db_type'], 'driver' => $GLOBALS['db_type'],
...@@ -157,10 +176,10 @@ class TestUtil ...@@ -157,10 +176,10 @@ class TestUtil
} }
/** /**
* @return \Doctrine\DBAL\Connection * @return Connection
*/ */
public static function getTempConnection() public static function getTempConnection()
{ {
return DriverManager::getConnection(self::getTmpConnectionParams()); return DriverManager::getConnection(self::getParamsForTemporaryConnection());
} }
} }
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