Fixup phpunit tests so we don't drop/create tables on EVERY SINGLE TEST.
authorChad Horohoe <demon@users.mediawiki.org>
Tue, 24 May 2011 21:22:36 +0000 (21:22 +0000)
committerChad Horohoe <demon@users.mediawiki.org>
Tue, 24 May 2011 21:22:36 +0000 (21:22 +0000)
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

index 19facd9..3363e3f 100644 (file)
@@ -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() {