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 = '' ) {
26 parent
::__construct( $name, $data, $dataName );
28 $this->backupGlobals
= false;
29 $this->backupStaticAttributes
= false;
32 function run( PHPUnit_Framework_TestResult
$result = NULL ) {
34 if( $this->needsDB() ) {
38 $this->db
= wfGetDB( DB_MASTER
);
40 $this->checkDbIsSupported();
42 $this->oldTablePrefix
= $wgDBprefix;
47 $this->addCoreDBData();
50 parent
::run( $result );
55 parent
::run( $result );
61 function __destruct() {
62 if( $this->needsDB() ) {
68 $rc = new ReflectionClass( $this );
69 return strpos( $rc->getDocComment(), '@group Database' ) !== false;
73 * Stub. If a test needs to add additional data to the database, it should
74 * implement this method and do so
76 function addDBData() {}
78 private function addCoreDBData() {
80 User
::resetIdByNameCache();
83 $user = User
::newFromName( 'UTSysop' );
85 if ( $user->idForName() == 0 ) {
86 $user->addToDatabase();
87 $user->setPassword( 'UTSysopPassword' );
89 $user->addGroup( 'sysop' );
90 $user->addGroup( 'bureaucrat' );
91 $user->saveSettings();
95 //Make 1 page with 1 revision
96 $article = new Article( Title
::newFromText( 'UTPage' ) );
97 $article->doEdit( 'UTContent',
101 User
::newFromName( 'UTSysop' ) );
104 private function initDB() {
107 $dbType = $this->db
->getType();
109 if ( $wgDBprefix === self
::DB_PREFIX ||
( $dbType == 'oracle' && $wgDBprefix === self
::ORA_DB_PREFIX
) ) {
110 throw new MWException( 'Cannot run unit tests, the database prefix is already "unittest_"' );
113 $tables = $this->listTables();
115 $prefix = $dbType != 'oracle' ? self
::DB_PREFIX
: self
::ORA_DB_PREFIX
;
117 $this->dbClone
= new CloneDatabase( $this->db
, $tables, $prefix );
118 $this->dbClone
->useTemporaryTables( false ); //reported problems with temp tables, disabling until fixed
119 $this->dbClone
->cloneTableStructure();
121 if ( $dbType == 'oracle' )
122 $this->db
->query( 'BEGIN FILL_WIKI_INFO; END;' );
124 if ( $dbType == 'oracle' ) {
125 # Insert 0 user to prevent FK violations
128 $this->db
->insert( 'user', array(
130 'user_name' => 'Anonymous' ) );
135 protected function destroyDB() {
137 if ( $this->useTemporaryTables
) {
138 # Don't need to do anything
140 //Temporary tables seem to be broken ATM, delete anyway
143 if( is_null( $this->db
) ) {
147 if( $this->db
->getType() == 'oracle' ) {
148 $tables = $this->db
->listTables( self
::ORA_DB_PREFIX
, __METHOD__
);
151 $tables = $this->db
->listTables( self
::DB_PREFIX
, __METHOD__
);
154 foreach ( $tables as $table ) {
156 $sql = $this->db
->getType() == 'oracle' ?
"DROP TABLE $table CASCADE CONSTRAINTS PURGE" : "DROP TABLE `$table`";
157 $this->db
->query( $sql, __METHOD__
);
158 } catch( Exception
$e ) {
162 if ( $this->db
->getType() == 'oracle' )
163 $this->db
->query( 'BEGIN FILL_WIKI_INFO; END;', __METHOD__
);
165 CloneDatabase
::changePrefix( $this->oldTablePrefix
);
168 function __call( $func, $args ) {
169 static $compatibility = array(
170 'assertInternalType' => 'assertType',
171 'assertNotInternalType' => 'assertNotType',
172 'assertInstanceOf' => 'assertType',
173 'assertEmpty' => 'assertEmpty2',
176 if ( method_exists( $this->suite
, $func ) ) {
177 return call_user_func_array( array( $this->suite
, $func ), $args);
178 } elseif ( isset( $compatibility[$func] ) ) {
179 return call_user_func_array( array( $this, $compatibility[$func] ), $args);
181 throw new MWException( "Called non-existant $func method on "
182 . get_class( $this ) );
186 static private assertEmpty2( $value, $msg ) {
187 return $this->assertTrue( $value == '', $msg );
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;