8000 Split the methods in TestUtil more and improve naming by JeroenDeDauw · Pull Request #643 · doctrine/dbal · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Split the methods in TestUtil more and improve naming #643

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 27, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 70 additions & 51 deletions tests/Doctrine/Tests/TestUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Doctrine\Tests;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DriverManager;

/**
Expand Down Expand Up @@ -32,61 +33,23 @@ class TestUtil
* 1) Each invocation of this method returns a NEW database connection.
* 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()
{
if (self::hasRequiredConnectionParams()) {
$realDbParams = self::getConnectionParams();
$tmpDbParams = self::getTmpConnectionParams();

$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 ($platform->supportsCreateDropDatabase()) {
$dbname = $realConn->getDatabase();
$realConn->close();

$tmpConn->getSchemaManager()->dropAndCreateDatabase($dbname);

$tmpConn->close();
} else {
$sm = $realConn->getSchemaManager();
$conn = DriverManager::getConnection(self::getConnectionParams());

$schema = $sm->createSchema();
$stmts = $schema->toDropSql($realConn->getDatabasePlatform());
self::addDbEventSubscribers($conn);

foreach ($stmts as $stmt) {
$realConn->exec($stmt);
}
}

$conn = DriverManager::getConnection($realDbParams, null, null);
} else {
$params = array(
'driver' => 'pdo_sqlite',
'memory' => true
);
if (isset($GLOBALS['db_path'])) {
$params['path'] = $GLOBALS['db_path'];
unlink($GLOBALS['db_path']);
}
$conn = DriverManager::getConnection($params);
}
return $conn;
}

if (isset($GLOBALS['db_event_subscribers'])) {
$evm = $conn->getEventManager();
foreach (explode(",", $GLOBALS['db_event_subscribers']) as $subscriberClass) {
$subscriberInstance = new $subscriberClass();
$evm->addEventSubscriber($subscriberInstance);
}
private static function getConnectionParams() {
if (self::hasRequiredConnectionParams()) {
return self::getSpecifiedConnectionParams();
}

return $conn;
return self::getFallbackConnectionParams();
}

private static function hasRequiredConnectionParams()
Expand All @@ -108,7 +71,63 @@ private static function hasRequiredConnectionParams()
);
}

private static function getTmpConnectionParams()
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 ($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);
}
}

return $realDbParams;
}

private static function getFallbackConnectionParams() {
$params = array(
'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'])) {
$evm = $conn->getEventManager();
foreach (explode(",", $GLOBALS['db_event_subscribers']) as $subscriberClass) {
$subscriberInstance = new $subscriberClass();
$evm->addEventSubscriber($subscriberInstance);
}
}
}

private static function getParamsForTemporaryConnection()
{
$connectionParams = array(
'driver' => $GLOBALS['tmpdb_type'],
Expand All @@ -134,7 +153,7 @@ private static function getTmpConnectionParams()
return $connectionParams;
}

private static function getConnectionParams()
private static function getParamsForMainConnection()
{
$connectionParams = array(
'driver' => $GLOBALS['db_type'],
Expand All @@ -157,10 +176,10 @@ private static function getConnectionParams()
}

/**
* @return \Doctrine\DBAL\Connection
* @return Connection
*/
public static function getTempConnection()
{
return DriverManager::getConnection(self::getTmpConnectionParams());
return DriverManager::getConnection(self::getParamsForTemporaryConnection());
}
}
0