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

@ -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`");
}
}
/**

View File

@ -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);
}
}
?>

View File

@ -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.