From ec2ce32245b638cee39cba0cd7e005cd98319f01 Mon Sep 17 00:00:00 2001 From: Sean Harvey Date: Sat, 11 Dec 2010 05:27:34 +0000 Subject: [PATCH] ENHANCEMENT #5442 SapphireTest now creates a "tmpdb" database name with a prefix, changed by defining SS_DATABASE_PREFIX. Defaults to "SS_" to namespace SilverStripe temporary database names git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@114819 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- dev/SapphireTest.php | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/dev/SapphireTest.php b/dev/SapphireTest.php index 1ddc217ba..974d5d298 100755 --- a/dev/SapphireTest.php +++ b/dev/SapphireTest.php @@ -145,10 +145,11 @@ class SapphireTest extends PHPUnit_Framework_TestCase { $className = get_class($this); $fixtureFile = eval("return {$className}::\$fixture_file;"); - + $prefix = defined('SS_DATABASE_PREFIX') ? SS_DATABASE_PREFIX : 'SS_'; + // Set up fixture if($fixtureFile || $this->usesDatabase || !self::using_temp_db()) { - if(substr(DB::getConn()->currentDatabase(),0,5) != 'tmpdb') { + if(substr(DB::getConn()->currentDatabase(), 0, strlen($prefix) + 5) != sprintf('%stmpdb', $prefix)) { //echo "Re-creating temp database... "; self::create_temp_db(); //echo "done.\n"; @@ -608,7 +609,8 @@ class SapphireTest extends PHPUnit_Framework_TestCase { */ static function using_temp_db() { $dbConn = DB::getConn(); - return $dbConn && (substr($dbConn->currentDatabase(),0,5) == 'tmpdb'); + $prefix = defined('SS_DATABASE_PREFIX') ? SS_DATABASE_PREFIX : 'SS_'; + return $dbConn && (substr($dbConn->currentDatabase(), 0, strlen($prefix) + 5) == sprintf('%stmpdb', $prefix)); } /** @@ -659,9 +661,10 @@ class SapphireTest extends PHPUnit_Framework_TestCase { // Create a temporary database $dbConn = DB::getConn(); - $dbname = 'tmpdb' . rand(1000000,9999999); + $prefix = defined('SS_DATABASE_PREFIX') ? SS_DATABASE_PREFIX : 'SS_'; + $dbname = sprintf('%stmpdb', $prefix) . rand(1000000,9999999); while(!$dbname || $dbConn->databaseExists($dbname)) { - $dbname = 'tmpdb' . rand(1000000,9999999); + $dbname = sprintf('%stmpdb', $prefix) . rand(1000000,9999999); } $dbConn->selectDatabase($dbname); @@ -677,8 +680,9 @@ class SapphireTest extends PHPUnit_Framework_TestCase { } static function delete_all_temp_dbs() { + $prefix = defined('SS_DATABASE_PREFIX') ? SS_DATABASE_PREFIX : 'SS_'; foreach(DB::getConn()->allDatabaseNames() as $dbName) { - if(preg_match('/^tmpdb[0-9]+$/', $dbName)) { + if(preg_match(sprintf('/^%stmpdb[0-9]+$/', $prefix), $dbName)) { DB::getConn()->dropDatabaseByName($dbName); if(Director::is_cli()) { echo "Dropped database \"$dbName\"" . PHP_EOL;