From 12868bab9f82d4e2e3eeed3e7bb5ece48df63caf Mon Sep 17 00:00:00 2001 From: Sam Minnee Date: Wed, 13 Aug 2008 02:47:14 +0000 Subject: [PATCH] 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 --- core/model/DatabaseAdmin.php | 10 +++++++ dev/SapphireTest.php | 52 +++++++++++++++++++++++------------- dev/TestRunner.php | 12 +++++++++ 3 files changed, 55 insertions(+), 19 deletions(-) diff --git a/core/model/DatabaseAdmin.php b/core/model/DatabaseAdmin.php index ccb68bf76..a65894259 100644 --- a/core/model/DatabaseAdmin.php +++ b/core/model/DatabaseAdmin.php @@ -203,6 +203,16 @@ class DatabaseAdmin extends Controller { echo "OK"; } } + + /** + * Clear all data out of the database + */ + function clearAllData() { + $tables = DB::query("SHOW TABLES")->column(); + foreach($tables as $table) { + DB::query("TRUNCATE `$table`"); + } + } /** diff --git a/dev/SapphireTest.php b/dev/SapphireTest.php index bdbb45115..34488d0d7 100644 --- a/dev/SapphireTest.php +++ b/dev/SapphireTest.php @@ -34,22 +34,19 @@ class SapphireTest extends PHPUnit_Framework_TestCase { // Set up fixture if($fixtureFile) { - // Create a temporary database - $dbConn = DB::getConn(); - $dbname = 'tmpdb' . rand(1000000,9999999); - while(!$dbname || $dbConn->databaseExists($dbname)) { - $dbname = 'tmpdb' . rand(1000000,9999999); + if(substr(DB::getConn()->currentDatabase(),0,5) != 'tmpdb') { + echo "Re-creating temp database... "; + self::create_temp_db(); + echo "done.\n"; } - $dbConn->selectDatabase($dbname); - + // This code is a bit misplaced; we want some way of the whole session being reinitialised... Versioned::reading_stage(null); - $dbConn->createDatabase(); singleton('DataObject')->flushCache(); $dbadmin = new DatabaseAdmin(); - $dbadmin->doBuild(true, false, true); + $dbadmin->clearAllData(); $this->fixture = new YamlFixture($fixtureFile); $this->fixture->saveIntoDatabase(); @@ -107,16 +104,6 @@ class SapphireTest extends PHPUnit_Framework_TestCase { } 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 Email::set_mailer($this->originalMailer); $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); + } } ?> diff --git a/dev/TestRunner.php b/dev/TestRunner.php index 7519445a2..e7a6fe104 100644 --- a/dev/TestRunner.php +++ b/dev/TestRunner.php @@ -114,6 +114,8 @@ class TestRunner extends Controller { } function runTests($classList, $coverage = false) { + $this->setUp(); + // run tests before outputting anything to the client $suite = new PHPUnit_Framework_TestSuite(); foreach($classList as $className) { @@ -158,9 +160,19 @@ class TestRunner extends Controller { 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 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.