public $regex = '';
public $runDisabled = false;
- protected static $databaseSetupDone = false;
protected $db;
protected $dbClone;
protected $oldTablePrefix;
protected $useTemporaryTables = true;
+ /**
+ * Table name prefixes. Oracle likes it shorter.
+ */
+ const DB_PREFIX = 'unittest_';
+ const ORA_DB_PREFIX = 'ut_';
+
+ protected $supportedDBs = array(
+ 'mysql',
+ 'sqlite',
+ 'oracle'
+ );
+
function __construct( $name = null, array $data = array(), $dataName = '' ) {
if ($name !== null) {
$this->setName($name);
$this->data = $data;
$this->dataName = $dataName;
+
+ $this->backupGlobals = false;
+ $this->backupStaticAttributes = false;
}
function run( PHPUnit_Framework_TestResult $result = NULL ) {
if( $this->needsDB() ) {
+
+ global $wgDBprefix;
- $this->destroyDBCheck();
+ $this->db = wfGetDB( DB_MASTER );
+
+ $this->checkDbIsSupported();
+
+ $this->oldTablePrefix = $wgDBprefix;
+
+ $this->destroyDB();
$this->initDB();
$this->addCoreDBData();
$this->addDBData();
+
+ parent::run( $result );
+
+ $this->destroyDB();
+ }
+ else {
+ parent::run( $result );
+
}
- parent::run( $result );
}
function __destruct() {
- $this->destroyDBCheck();
- }
-
- function destroyDBCheck() {
- if( is_object( $this->dbClone ) && $this->dbClone instanceof CloneDatabase ) {
+ if( $this->needsDB() ) {
$this->destroyDB();
}
}
$rc = new ReflectionClass( $this );
return strpos( $rc->getDocComment(), '@group Database' ) !== false;
}
-
+
+ /**
+ * Stub. If a test needs to add additional data to the database, it should
+ * implement this method and do so
+ */
function addDBData() {}
private function addCoreDBData() {
-
+
+ User::resetIdByNameCache();
+
//Make sysop user
$user = User::newFromName( 'UTSysop' );
private function initDB() {
global $wgDBprefix;
- if ( self::$databaseSetupDone ) {
- return;
- }
-
- $this->db = wfGetDB( DB_MASTER );
$dbType = $this->db->getType();
- if ( $wgDBprefix === 'unittest_' || ( $dbType == 'oracle' && $wgDBprefix === 'ut_' ) ) {
+ if ( $wgDBprefix === self::DB_PREFIX || ( $dbType == 'oracle' && $wgDBprefix === self::ORA_DB_PREFIX ) ) {
throw new MWException( 'Cannot run unit tests, the database prefix is already "unittest_"' );
}
- self::$databaseSetupDone = true;
- $this->oldTablePrefix = $wgDBprefix;
-
- # SqlBagOStuff broke when using temporary tables on r40209 (bug 15892).
- # It seems to have been fixed since (r55079?).
- # If it fails, $wgCaches[CACHE_DB] = new HashBagOStuff(); should work around it.
-
- # CREATE TEMPORARY TABLE breaks if there is more than one server
- if ( wfGetLB()->getServerCount() != 1 ) {
- $this->useTemporaryTables = false;
- }
-
- $temporary = $this->useTemporaryTables || $dbType == 'postgres';
-
$tables = $this->listTables();
- $prefix = $dbType != 'oracle' ? 'unittest_' : 'ut_';
-
+ $prefix = $dbType != 'oracle' ? self::DB_PREFIX : self::ORA_DB_PREFIX;
+
$this->dbClone = new CloneDatabase( $this->db, $tables, $prefix );
- $this->dbClone->useTemporaryTables( $temporary );
+ $this->dbClone->useTemporaryTables( false ); //reported problems with temp tables, disabling until fixed
$this->dbClone->cloneTableStructure();
-
+
if ( $dbType == 'oracle' )
$this->db->query( 'BEGIN FILL_WIKI_INFO; END;' );
}
protected function destroyDB() {
- if ( !self::$databaseSetupDone ) {
- return;
- }
- $this->dbClone->destroy();
- self::$databaseSetupDone = false;
-
if ( $this->useTemporaryTables ) {
# Don't need to do anything
//return;
//Temporary tables seem to be broken ATM, delete anyway
}
-
+
+ if( is_null( $this->db ) ) {
+ return;
+ }
+
if( $this->db->getType() == 'oracle' ) {
- $tables = $this->db->listTables( 'ut_', __METHOD__ );
+ $tables = $this->db->listTables( self::ORA_DB_PREFIX, __METHOD__ );
}
else {
- $tables = $this->db->listTables( 'unittest_', __METHOD__ );
+ $tables = $this->db->listTables( self::DB_PREFIX, __METHOD__ );
}
foreach ( $tables as $table ) {
- $sql = $this->db->getType() == 'oracle' ? "DROP TABLE $table DROP CONSTRAINTS" : "DROP TABLE `$table`";
- $this->db->query( $sql );
+ try {
+ $sql = $this->db->getType() == 'oracle' ? "DROP TABLE $table CASCADE CONSTRAINTS PURGE" : "DROP TABLE `$table`";
+ $this->db->query( $sql, __METHOD__ );
+ } catch( Exception $e ) {
+ }
}
-
- if ( $this->db->getType() == 'oracle' )
- $this->db->query( 'BEGIN FILL_WIKI_INFO; END;' );
+ if ( $this->db->getType() == 'oracle' )
+ $this->db->query( 'BEGIN FILL_WIKI_INFO; END;', __METHOD__ );
+ CloneDatabase::changePrefix( $this->oldTablePrefix );
}
function __call( $func, $args ) {
return $tables;
}
+
+ protected function checkDbIsSupported() {
+ if( !in_array( $this->db->getType(), $this->supportedDBs ) ) {
+ throw new MWException( $this->db->getType() . " is not currently supported for unit testing." );
+ }
+ }
+
+ public function getCliArg( $offset ) {
+
+ if( isset( MediaWikiPHPUnitCommand::$additionalOptions[$offset] ) ) {
+ return MediaWikiPHPUnitCommand::$additionalOptions[$offset];
+ }
+
+ }
+
+ public function setCliArg( $offset, $value ) {
+
+ MediaWikiPHPUnitCommand::$additionalOptions[$offset] = $value;
+
+ }
}