Revert "Clean up applyState"

This reverts commit 5c5438abee.

Breaks core builds.
This commit is contained in:
Ingo Schommer 2014-03-17 22:59:05 +13:00
parent fe980540b9
commit 975e6bed56

View File

@ -157,58 +157,106 @@ class TestSessionEnvironment extends Object {
* @throws InvalidArgumentException * @throws InvalidArgumentException
*/ */
public function applyState($state) { public function applyState($state) {
$this->extend('onBeforeApplyState', $state);
$database = (isset($state->database)) ? $state->database : null;
// back up source
global $databaseConfig; global $databaseConfig;
$this->oldDatabaseName = $databaseConfig['database'];
$this->extend('onBeforeApplyState', $state);
// Load existing state from $this->state into $state, if there is any // Load existing state from $this->state into $state, if there is any
$oldState = $this->getState(); $oldState = $this->getState();
if($oldState) { if($oldState) {
foreach($oldState as $k => $v) { foreach($oldState as $k => $v) {
if(!isset($state->$k)) { if(!isset($state->$k)) $state->$k = $v; // Don't overwrite stuff in $state, as that's the new state
$state->$k = $v; // Don't overwrite stuff in $state, as that's the new state }
}
if(isset($state->database) && $state->database) {
if(!DB::getConn()) {
// 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::query("SELECT DATABASE()")->value();
if(isset($state->database) && $db != $state->database) {
$this->oldDatabaseName = $databaseConfig['database'];
$databaseConfig['database'] = $state->database;
DB::connect($databaseConfig);
} }
} }
} }
// ensure we have a connection to the database // Database
if($database) { if(!$this->isRunningTests() && (@$state->createDatabase || @$state->database)) {
$conn = DB::getConn(); $dbName = (isset($state->database)) ? $state->database : null;
if(!$conn) { if($dbName) {
DB::connect($databaseConfig); $dbExists = (bool)DB::query(
$conn = DB::getConn(); sprintf("SHOW DATABASES LIKE '%s'", Convert::raw2sql($dbName))
)->value();
} else {
$dbExists = false;
} }
$conn->selectDatabase($database); if(!$dbExists) {
} else { // Create a new one with a randomized name
$state->database = SapphireTest::create_temp_db(); $dbName = SapphireTest::create_temp_db();
}
// Database $state->database = $dbName; // In case it's changed by the call to SapphireTest::create_temp_db();
if(isset($state->createDatabase)) {
$this->createDatabaseForState($state);
unset($state->createDatabase); // Set existing one, assumes it already has been created
$prefix = defined('SS_DATABASE_PREFIX') ? SS_DATABASE_PREFIX : 'ss_';
$pattern = strtolower(sprintf('#^%stmpdb\d{7}#', $prefix));
if(!preg_match($pattern, $dbName)) {
throw new InvalidArgumentException("Invalid database name format");
}
$this->oldDatabaseName = $databaseConfig['database'];
$databaseConfig['database'] = $dbName; // Instead of calling DB::set_alternative_db_name();
// Connect to the new database, overwriting the old DB connection (if any)
DB::connect($databaseConfig);
}
// Import database template if required
if(isset($state->createDatabaseTemplate) && $state->createDatabaseTemplate) {
$sql = file_get_contents($state->createDatabaseTemplate);
// Split into individual query commands, removing comments
$sqlCmds = array_filter(
preg_split('/\s*;\s*/',
preg_replace(array('/^$\n/m', '/^(\/|#).*$\n/m'), '', $sql)
)
);
// Execute each query
foreach($sqlCmds as $sqlCmd) {
DB::query($sqlCmd);
}
// In case the dump involved CREATE TABLE commands, we need to ensure
// the schema is still up to date
$dbAdmin = new DatabaseAdmin();
$populate = (isset($state->requireDefaultRecords) && $state->requireDefaultRecords);
Versioned::set_reading_mode('');
$dbAdmin->doBuild(true /*quiet*/, $populate);
}
if(isset($state->createDatabase)) unset($state->createDatabase);
} }
// Fixtures // Fixtures
$fixtureFile = (isset($state->fixture)) ? $state->fixture : null; $fixtureFile = (isset($state->fixture)) ? $state->fixture : null;
if($fixtureFile) { if($fixtureFile) {
$this->loadFixtureIntoDb($fixtureFile); $this->loadFixtureIntoDb($fixtureFile);
unset($state->fixture); // Only insert the fixture(s) once, not every time we call this method
unset($state->fixture);
} }
// Mailer // Mailer
$mailer = (isset($state->mailer)) ? $state->mailer : null; $mailer = (isset($state->mailer)) ? $state->mailer : null;
if($mailer) { if($mailer) {
if(!class_exists($mailer) || !is_subclass_of($mailer, 'Mailer')) { if(!class_exists($mailer) || !is_subclass_of($mailer, 'Mailer')) {
throw new InvalidArgumentException(sprintf( throw new InvalidArgumentException(sprintf(
@ -230,55 +278,12 @@ class TestSessionEnvironment extends Object {
} }
} }
$this->saveState($state); file_put_contents(
$this->extend('onAfterApplyState');
}
/**
* Import the database
*
* @return void
*/
public function createDatabaseForState($state) {
if(isset($state->createDatabaseTemplate) && $state->createDatabaseTemplate) {
$sql = file_get_contents($state->createDatabaseTemplate);
// Split into individual query commands, removing comments
$sqlCmds = array_filter(
preg_split('/\s*;\s*/',
preg_replace(array('/^$\n/m', '/^(\/|#).*$\n/m'), '', $sql)
)
);
// Execute each query
foreach($sqlCmds as $sqlCmd) {
DB::query($sqlCmd);
}
// In case the dump involved CREATE TABLE commands, we need to ensure
// the schema is still up to date
$dbAdmin = new DatabaseAdmin();
$populate = (isset($state->requireDefaultRecords) && $state->requireDefaultRecords);
Versioned::set_reading_mode('');
$dbAdmin->doBuild(true, $populate);
unset($state->createDatabaseTemplate);
}
return $state;
}
/**
* Sliented as if the file already exists by another process, we don't want
* to modify.
*/
public function saveState($state) {
@file_put_contents(
$this->getFilePath(), $this->getFilePath(),
json_encode($state, JSON_PRETTY_PRINT), json_encode($state, JSON_PRETTY_PRINT)
LOCK_EX
); );
$this->extend('onAfterApplyState');
} }
public function loadFromFile() { public function loadFromFile() {
@ -298,7 +303,6 @@ class TestSessionEnvironment extends Object {
private function removeStateFile() { private function removeStateFile() {
$file = $this->getFilePath(); $file = $this->getFilePath();
if(file_exists($file)) { if(file_exists($file)) {
if(!unlink($file)) { if(!unlink($file)) {
throw new \Exception('Unable to remove the testsession state file, please remove it manually. File ' throw new \Exception('Unable to remove the testsession state file, please remove it manually. File '
@ -323,8 +327,6 @@ class TestSessionEnvironment extends Object {
if(SapphireTest::using_temp_db()) { if(SapphireTest::using_temp_db()) {
$this->resetDatabaseName(); $this->resetDatabaseName();
SapphireTest::set_is_running_test(false);
} }
$this->removeStateFile(); $this->removeStateFile();
@ -367,17 +369,11 @@ class TestSessionEnvironment extends Object {
* Reset the database connection to use the original database. Called by {@link self::endTestSession()}. * Reset the database connection to use the original database. Called by {@link self::endTestSession()}.
*/ */
public function resetDatabaseName() { public function resetDatabaseName() {
if($this->oldDatabaseName) { global $databaseConfig;
global $databaseConfig;
$databaseConfig['database'] = $this->oldDatabaseName; $databaseConfig['database'] = $this->oldDatabaseName;
$conn = DB::getConn(); DB::connect($databaseConfig);
if($conn) {
$conn->selectDatabase($this->oldDatabaseName);
}
}
} }
/** /**