Skip to content

Commit

Permalink
Merge pull request #61 from webbuilders-group/db-reconnect-fix
Browse files Browse the repository at this point in the history
BUGFIX: Fixed issue where the incorrect database connection could be made when using a stubfile
  • Loading branch information
dnsl48 authored Feb 1, 2019
2 parents 8827e97 + e957d1e commit 61d12ec
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 24 deletions.
54 changes: 34 additions & 20 deletions src/TestSessionEnvironment.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,26 +284,7 @@ public function applyState($state)
}

// ensure we have a connection to the database
if (isset($state->database) && $state->database) {
if (!DB::get_conn()) {
// No connection, so try and connect to tmpdb if it exists
if (isset($state->database)) {
$this->oldDatabaseName = $databaseConfig['database'];
$databaseConfig['database'] = $state->database;
}

// Connect to database
DB::connect($databaseConfig);
} else {
// We've already connected to the database, do a fast check to see what database we're currently using
$db = DB::get_conn()->getSelectedDatabase();
if (isset($state->database) && $db != $state->database) {
$this->oldDatabaseName = $databaseConfig['database'];
$databaseConfig['database'] = $state->database;
DB::connect($databaseConfig);
}
}
}
$this->connectToDatabase($state);

// Database
if (!$this->isRunningTests()) {
Expand Down Expand Up @@ -562,6 +543,39 @@ protected function getAssetsBackupfolder()
return PUBLIC_PATH . DIRECTORY_SEPARATOR . 'assets_backup';
}

/**
* Ensure that there is a connection to the database
*
* @param mixed $state
*/
public function connectToDatabase($state = null) {
if ($state == null) {
$state = $this->getState();
}

$databaseConfig = DB::getConfig();

if (isset($state->database) && $state->database) {
if (!DB::get_conn()) {
// No connection, so try and connect to tmpdb if it exists
if (isset($state->database)) {
$this->oldDatabaseName = $databaseConfig['database'];
$databaseConfig['database'] = $state->database;
}

// Connect to database
DB::connect($databaseConfig);
} else {
// We've already connected to the database, do a fast check to see what database we're currently using
$db = DB::get_conn()->getSelectedDatabase();
if (isset($state->database) && $db != $state->database) {
$this->oldDatabaseName = $databaseConfig['database'];
$databaseConfig['database'] = $state->database;
DB::connect($databaseConfig);
}
}
}
}

/**
* Wait for pending requests
Expand Down
6 changes: 2 additions & 4 deletions src/TestSessionHTTPMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,8 @@ protected function loadTestState(HTTPRequest $request)
$file = $testState->stubfile;
if (!Director::isLive() && $file && file_exists($file)) {
// Connect to the database so the included code can interact with it
$databaseConfig = DB::getConfig();
if ($databaseConfig) {
DB::connect($databaseConfig);
}
$this->testSessionEnvironment->connectToDatabase();

include_once($file);
}
}
Expand Down

0 comments on commit 61d12ec

Please sign in to comment.