From: Chad Horohoe Date: Wed, 1 Jun 2011 22:43:19 +0000 (+0000) Subject: Test db cleanup X-Git-Tag: 1.31.0-rc.0~29789 X-Git-Url: https://git.cyclocoop.org/%7B%24admin_url%7Dmembres/supprimer.php?a=commitdiff_plain;h=f639fcdc4ff4031d9c789452f9a356b99a642c31;p=lhc%2Fweb%2Fwiklou.git Test db cleanup * Fix Oracle stuff per CR on r88755 * Don't expect tests to clean up after themselves, do it in MediaWikiTestCase::run() (skipping user and interwiki, also per CR) ** Also don't do it from the destructor, phpunit calls this more often than you'd think * Need to find: way to detect "is this the last test?" so we can drop our tables on completion --- diff --git a/tests/phpunit/MediaWikiTestCase.php b/tests/phpunit/MediaWikiTestCase.php index 10518445d1..f421fea5c7 100644 --- a/tests/phpunit/MediaWikiTestCase.php +++ b/tests/phpunit/MediaWikiTestCase.php @@ -46,7 +46,7 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase { $this->db = wfGetDB( DB_MASTER ); $this->checkDbIsSupported(); - + $this->oldTablePrefix = $wgDBprefix; if( !self::$dbSetup ) { @@ -54,24 +54,16 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase { self::$dbSetup = true; } - $this->resetDB(); $this->addCoreDBData(); $this->addDBData(); parent::run( $result ); - $this->removeDBData(); - + $this->resetDB(); } else { parent::run( $result ); } } - - function __destruct() { - if( $this->needsDB() ) { - $this->destroyDB(); - } - } function dbPrefix() { return $this->db->getType() == 'oracle' ? self::ORA_DB_PREFIX : self::DB_PREFIX; @@ -87,12 +79,6 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase { * implement this method and do so */ function addDBData() {} - - /** - * Stub. If a test needs to remove data from the database. Called after - * test run. - */ - function removeDBData() {} private function addCoreDBData() { @@ -129,6 +115,16 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase { $dbClone = new CloneDatabase( $this->db, $this->listTables(), $this->dbPrefix() ); $dbClone->useTemporaryTables( $this->useTemporaryTables ); $dbClone->cloneTableStructure(); + + 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' ) ); + } } /** @@ -137,49 +133,34 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase { private function resetDB() { if( $this->db ) { foreach( $this->listTables() as $tbl ) { + if( $tbl == 'interwiki' || $tbl == 'user' ) continue; $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() { - - if ( $this->useTemporaryTables ) { + if ( $this->useTemporaryTables || is_null( $this->db ) ) { # Don't need to do anything - //return; - //Temporary tables seem to be broken ATM, delete anyway - } - - if( is_null( $this->db ) ) { return; } - + $tables = $this->db->listTables( $this->dbPrefix(), __METHOD__ ); - + foreach ( $tables as $table ) { try { $sql = $this->db->getType() == 'oracle' ? "DROP TABLE $table CASCADE CONSTRAINTS PURGE" : "DROP TABLE `$table`"; $this->db->query( $sql, __METHOD__ ); - } catch( Exception $e ) { - } + } catch( MWException $mwe ) {} } - + if ( $this->db->getType() == 'oracle' ) $this->db->query( 'BEGIN FILL_WIKI_INFO; END;', __METHOD__ ); - + CloneDatabase::changePrefix( $this->oldTablePrefix ); } + function __call( $func, $args ) { static $compatibility = array( 'assertInternalType' => 'assertType', @@ -209,7 +190,7 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase { protected function listTables() { global $wgDBprefix; - + $tables = $this->db->listTables( $wgDBprefix, __METHOD__ ); $tables = array_map( array( __CLASS__, 'unprefixTable' ), $tables ); diff --git a/tests/phpunit/includes/api/ApiBlockTest.php b/tests/phpunit/includes/api/ApiBlockTest.php index 16ae636140..a7dbcf8384 100644 --- a/tests/phpunit/includes/api/ApiBlockTest.php +++ b/tests/phpunit/includes/api/ApiBlockTest.php @@ -28,14 +28,6 @@ class ApiBlockTest extends ApiTestSetup { } } - function removeDBData() { - $block = Block::newFromTarget('UTBlockee'); - if( $block ) { - $block->delete(); - } - } - - function testMakeNormalBlock() { $data = $this->getTokens();