3 abstract class MediaWikiTestCase
extends PHPUnit_Framework_TestCase
{
6 public $runDisabled = false;
10 protected $oldTablePrefix;
11 protected $useTemporaryTables = true;
14 * Table name prefixes. Oracle likes it shorter.
16 const DB_PREFIX
= 'unittest_';
17 const ORA_DB_PREFIX
= 'ut_';
19 protected $supportedDBs = array(
25 function __construct( $name = null, array $data = array(), $dataName = '' ) {
27 $this->setName($name);
31 $this->dataName
= $dataName;
33 $this->backupGlobals
= false;
34 $this->backupStaticAttributes
= false;
37 function run( PHPUnit_Framework_TestResult
$result = NULL ) {
39 if( $this->needsDB() ) {
43 $this->db
= wfGetDB( DB_MASTER
);
45 $this->checkDbIsSupported();
47 $this->oldTablePrefix
= $wgDBprefix;
52 $this->addCoreDBData();
55 parent
::run( $result );
60 parent
::run( $result );
66 function __destruct() {
67 if( $this->needsDB() ) {
73 $rc = new ReflectionClass( $this );
74 return strpos( $rc->getDocComment(), '@group Database' ) !== false;
78 * Stub. If a test needs to add additional data to the database, it should
79 * implement this method and do so
81 function addDBData() {}
83 private function addCoreDBData() {
85 User
::resetIdByNameCache();
88 $user = User
::newFromName( 'UTSysop' );
90 if ( $user->idForName() == 0 ) {
91 $user->addToDatabase();
92 $user->setPassword( 'UTSysopPassword' );
94 $user->addGroup( 'sysop' );
95 $user->addGroup( 'bureaucrat' );
96 $user->saveSettings();
100 //Make 1 page with 1 revision
101 $article = new Article( Title
::newFromText( 'UTPage' ) );
102 $article->doEdit( 'UTContent',
106 User
::newFromName( 'UTSysop' ) );
109 private function initDB() {
112 $dbType = $this->db
->getType();
114 if ( $wgDBprefix === self
::DB_PREFIX ||
( $dbType == 'oracle' && $wgDBprefix === self
::ORA_DB_PREFIX
) ) {
115 throw new MWException( 'Cannot run unit tests, the database prefix is already "unittest_"' );
118 $tables = $this->listTables();
120 $prefix = $dbType != 'oracle' ? self
::DB_PREFIX
: self
::ORA_DB_PREFIX
;
122 $this->dbClone
= new CloneDatabase( $this->db
, $tables, $prefix );
123 $this->dbClone
->useTemporaryTables( false ); //reported problems with temp tables, disabling until fixed
124 $this->dbClone
->cloneTableStructure();
126 if ( $dbType == 'oracle' )
127 $this->db
->query( 'BEGIN FILL_WIKI_INFO; END;' );
129 if ( $dbType == 'oracle' ) {
130 # Insert 0 user to prevent FK violations
133 $this->db
->insert( 'user', array(
135 'user_name' => 'Anonymous' ) );
140 protected function destroyDB() {
142 if ( $this->useTemporaryTables
) {
143 # Don't need to do anything
145 //Temporary tables seem to be broken ATM, delete anyway
148 if( is_null( $this->db
) ) {
152 if( $this->db
->getType() == 'oracle' ) {
153 $tables = $this->db
->listTables( self
::ORA_DB_PREFIX
, __METHOD__
);
156 $tables = $this->db
->listTables( self
::DB_PREFIX
, __METHOD__
);
159 foreach ( $tables as $table ) {
161 $sql = $this->db
->getType() == 'oracle' ?
"DROP TABLE $table CASCADE CONSTRAINTS PURGE" : "DROP TABLE `$table`";
162 $this->db
->query( $sql, __METHOD__
);
163 } catch( Exception
$e ) {
167 if ( $this->db
->getType() == 'oracle' )
168 $this->db
->query( 'BEGIN FILL_WIKI_INFO; END;', __METHOD__
);
170 CloneDatabase
::changePrefix( $this->oldTablePrefix
);
173 function __call( $func, $args ) {
174 static $compatibility = array(
175 'assertInternalType' => 'assertType',
176 'assertNotInternalType' => 'assertNotType',
177 'assertInstanceOf' => 'assertType',
180 if ( method_exists( $this->suite
, $func ) ) {
181 return call_user_func_array( array( $this->suite
, $func ), $args);
182 } elseif ( isset( $compatibility[$func] ) ) {
183 return call_user_func_array( array( $this, $compatibility[$func] ), $args);
185 throw new MWException( "Called non-existant $func method on "
186 . get_class( $this ) );
190 static private function unprefixTable( $tableName ) {
192 return substr( $tableName, strlen( $wgDBprefix ) );
195 protected function listTables() {
198 $tables = $this->db
->listTables( $wgDBprefix, __METHOD__
);
199 $tables = array_map( array( __CLASS__
, 'unprefixTable' ), $tables );
204 protected function checkDbIsSupported() {
205 if( !in_array( $this->db
->getType(), $this->supportedDBs
) ) {
206 throw new MWException( $this->db
->getType() . " is not currently supported for unit testing." );
210 public function getCliArg( $offset ) {
212 if( isset( MediaWikiPHPUnitCommand
::$additionalOptions[$offset] ) ) {
213 return MediaWikiPHPUnitCommand
::$additionalOptions[$offset];
218 public function setCliArg( $offset, $value ) {
220 MediaWikiPHPUnitCommand
::$additionalOptions[$offset] = $value;