From c84f722f13cbfa41ef0e21a217d0194542b7b601 Mon Sep 17 00:00:00 2001 From: Chad Horohoe Date: Tue, 24 May 2011 21:22:36 +0000 Subject: [PATCH] Fixup phpunit tests so we don't drop/create tables on EVERY SINGLE TEST. Now just create them on the first test that needsDB() and skip creation on the rest. Leaves you with a bunch of useless tables at the end of a run, but oh well at least its faster :) --- tests/phpunit/MediaWikiTestCase.php | 61 ++++++++++++++++------------- 1 file changed, 34 insertions(+), 27 deletions(-) diff --git a/tests/phpunit/MediaWikiTestCase.php b/tests/phpunit/MediaWikiTestCase.php index 19facd9962..3363e3f349 100644 --- a/tests/phpunit/MediaWikiTestCase.php +++ b/tests/phpunit/MediaWikiTestCase.php @@ -4,11 +4,14 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase { public $suite; public $regex = ''; public $runDisabled = false; - + + /** + * @var DatabaseBase + */ protected $db; - protected $dbClone; protected $oldTablePrefix; protected $useTemporaryTables = true; + private static $dbSetup = false; /** * Table name prefixes. Oracle likes it shorter. @@ -45,16 +48,17 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase { $this->checkDbIsSupported(); $this->oldTablePrefix = $wgDBprefix; - - $this->destroyDB(); - - $this->initDB(); + + if( !self::$dbSetup ) { + $this->initDB(); + self::$dbSetup = true; + } + + $this->resetDB(); $this->addCoreDBData(); $this->addDBData(); parent::run( $result ); - - $this->destroyDB(); } else { parent::run( $result ); } @@ -109,31 +113,34 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase { private function initDB() { global $wgDBprefix; - - $dbType = $this->db->getType(); - if ( $wgDBprefix === $this->dbPrefix() ) { throw new MWException( 'Cannot run unit tests, the database prefix is already "unittest_"' ); } - $tables = $this->listTables(); - - $this->dbClone = new CloneDatabase( $this->db, $tables, $this->dbPrefix() ); - $this->dbClone->useTemporaryTables( $this->useTemporaryTables ); - $this->dbClone->cloneTableStructure(); - - if ( $dbType == 'oracle' ) - $this->db->query( 'BEGIN FILL_WIKI_INFO; END;' ); + $dbClone = new CloneDatabase( $this->db, $this->listTables(), $this->dbPrefix() ); + $dbClone->useTemporaryTables( $this->useTemporaryTables ); + $dbClone->cloneTableStructure(); + } - if ( $dbType == 'oracle' ) { - # Insert 0 user to prevent FK violations - - # Anonymous user - $this->db->insert( 'user', array( - 'user_id' => 0, - 'user_name' => 'Anonymous' ) ); + /** + * Empty all tables so they can be repopulated for tests + */ + private function resetDB() { + if( $this->db ) { + foreach( $this->listTables() as $tbl ) { + $this->db->delete( $tbl, '*', __METHOD__ ); + } + + if ( $this->db->getType() == 'oracle' ) { + $this->db->query( 'BEGIN FILL_WIKI_INFO; END;' ); + + # Insert 0 user to prevent FK violations + # Anonymous user + $this->db->insert( 'user', array( + 'user_id' => 0, + 'user_name' => 'Anonymous' ) ); + } } - } protected function destroyDB() { -- 2.20.1