Test db cleanup
authorChad Horohoe <demon@users.mediawiki.org>
Wed, 1 Jun 2011 22:43:19 +0000 (22:43 +0000)
committerChad Horohoe <demon@users.mediawiki.org>
Wed, 1 Jun 2011 22:43:19 +0000 (22:43 +0000)
* 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

tests/phpunit/MediaWikiTestCase.php
tests/phpunit/includes/api/ApiBlockTest.php

index 1051844..f421fea 100644 (file)
@@ -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 );
 
index 16ae636..a7dbcf8 100644 (file)
@@ -28,14 +28,6 @@ class ApiBlockTest extends ApiTestSetup {
                }
        }
 
-       function removeDBData() {
-               $block = Block::newFromTarget('UTBlockee');
-               if( $block ) {
-                       $block->delete();
-               }
-       }       
-
-       
        function testMakeNormalBlock() {
                
                $data = $this->getTokens();