Added DatabaseAdmin::clearAllData() to empty out a database.

Improved performance of testrunner so that it doesn't create a new database for each test, instead only once per test run.

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@60597 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Sam Minnee 2008-08-13 02:47:14 +00:00
parent 9dcb89bf51
commit 12868bab9f
3 changed files with 55 additions and 19 deletions

View File

@ -204,6 +204,16 @@ class DatabaseAdmin extends Controller {
} }
} }
/**
* Clear all data out of the database
*/
function clearAllData() {
$tables = DB::query("SHOW TABLES")->column();
foreach($tables as $table) {
DB::query("TRUNCATE `$table`");
}
}
/** /**
* Method used to check mod_rewrite is working correctly in the installer. * Method used to check mod_rewrite is working correctly in the installer.

View File

@ -34,22 +34,19 @@ class SapphireTest extends PHPUnit_Framework_TestCase {
// Set up fixture // Set up fixture
if($fixtureFile) { if($fixtureFile) {
// Create a temporary database if(substr(DB::getConn()->currentDatabase(),0,5) != 'tmpdb') {
$dbConn = DB::getConn(); echo "Re-creating temp database... ";
$dbname = 'tmpdb' . rand(1000000,9999999); self::create_temp_db();
while(!$dbname || $dbConn->databaseExists($dbname)) { echo "done.\n";
$dbname = 'tmpdb' . rand(1000000,9999999);
} }
$dbConn->selectDatabase($dbname);
// This code is a bit misplaced; we want some way of the whole session being reinitialised... // This code is a bit misplaced; we want some way of the whole session being reinitialised...
Versioned::reading_stage(null); Versioned::reading_stage(null);
$dbConn->createDatabase();
singleton('DataObject')->flushCache(); singleton('DataObject')->flushCache();
$dbadmin = new DatabaseAdmin(); $dbadmin = new DatabaseAdmin();
$dbadmin->doBuild(true, false, true); $dbadmin->clearAllData();
$this->fixture = new YamlFixture($fixtureFile); $this->fixture = new YamlFixture($fixtureFile);
$this->fixture->saveIntoDatabase(); $this->fixture->saveIntoDatabase();
@ -107,16 +104,6 @@ class SapphireTest extends PHPUnit_Framework_TestCase {
} }
function tearDown() { function tearDown() {
// Delete our temporary database
$dbConn = DB::getConn();
if($dbConn && substr($dbConn->currentDatabase(),0,5) == 'tmpdb') {
$dbName = $dbConn->currentDatabase();
if($dbName && DB::query("SHOW DATABASES LIKE '$dbName'")->value()) {
// echo "Deleted temp database " . $dbConn->currentDatabase() . "\n";
$dbConn->dropDatabase();
}
}
// Restore email configuration // Restore email configuration
Email::set_mailer($this->originalMailer); Email::set_mailer($this->originalMailer);
$this->originalMailer = null; $this->originalMailer = null;
@ -169,6 +156,33 @@ class SapphireTest extends PHPUnit_Framework_TestCase {
); );
} }
} }
static function kill_temp_db() {
// Delete our temporary database
$dbConn = DB::getConn();
if($dbConn && substr($dbConn->currentDatabase(),0,5) == 'tmpdb') {
$dbName = $dbConn->currentDatabase();
if($dbName && DB::query("SHOW DATABASES LIKE '$dbName'")->value()) {
// echo "Deleted temp database " . $dbConn->currentDatabase() . "\n";
$dbConn->dropDatabase();
}
}
}
static function create_temp_db() {
// Create a temporary database
$dbConn = DB::getConn();
$dbname = 'tmpdb' . rand(1000000,9999999);
while(!$dbname || $dbConn->databaseExists($dbname)) {
$dbname = 'tmpdb' . rand(1000000,9999999);
}
$dbConn->selectDatabase($dbname);
$dbConn->createDatabase();
$dbadmin = new DatabaseAdmin();
$dbadmin->doBuild(true, false, true);
}
} }
?> ?>

View File

@ -114,6 +114,8 @@ class TestRunner extends Controller {
} }
function runTests($classList, $coverage = false) { function runTests($classList, $coverage = false) {
$this->setUp();
// run tests before outputting anything to the client // run tests before outputting anything to the client
$suite = new PHPUnit_Framework_TestSuite(); $suite = new PHPUnit_Framework_TestSuite();
foreach($classList as $className) { foreach($classList as $className) {
@ -158,9 +160,19 @@ class TestRunner extends Controller {
if(!Director::is_cli()) self::$default_reporter->writeFooter(); if(!Director::is_cli()) self::$default_reporter->writeFooter();
$this->tearDown();
// Todo: we should figure out how to pass this data back through Director more cleanly // Todo: we should figure out how to pass this data back through Director more cleanly
if(Director::is_cli() && ($results->failureCount() + $results->errorCount()) > 0) exit(2); if(Director::is_cli() && ($results->failureCount() + $results->errorCount()) > 0) exit(2);
} }
function setUp() {
SapphireTest::create_temp_db();
}
function tearDown() {
SapphireTest::kill_temp_db();
}
} }
// This class is here to help with documentation. // This class is here to help with documentation.