3 abstract class MediaWikiTestCase
extends PHPUnit_Framework_TestCase
{
6 public $runDisabled = false;
12 protected $oldTablePrefix;
13 protected $useTemporaryTables = true;
14 protected $reuseDB = false;
15 private static $dbSetup = false;
16 private static $tablesCloned = array();
19 * Table name prefixes. Oracle likes it shorter.
21 const DB_PREFIX
= 'unittest_';
22 const ORA_DB_PREFIX
= 'ut_';
24 protected $supportedDBs = array(
31 function __construct( $name = null, array $data = array(), $dataName = '' ) {
32 parent
::__construct( $name, $data, $dataName );
34 $this->backupGlobals
= false;
35 $this->backupStaticAttributes
= false;
38 function run( PHPUnit_Framework_TestResult
$result = NULL ) {
39 /* Some functions require some kind of caching, and will end up using the db,
40 * which we can't allow, as that would open a new connection for mysql.
41 * Replace with a HashBag. They would not be going to persist anyway.
43 ObjectCache
::$instances[CACHE_DB
] = new HashBagOStuff
;
45 if( $this->needsDB() ) {
48 $this->useTemporaryTables
= !$this->getCliArg( 'use-normal-tables' );
49 $this->reuseDB
= $this->getCliArg('reuse-db');
51 $this->db
= wfGetDB( DB_MASTER
);
53 $this->checkDbIsSupported();
55 $this->oldTablePrefix
= $wgDBprefix;
57 if( !self
::$dbSetup ) {
58 self
::$tablesCloned = $this->initDB();
59 self
::$dbSetup = true;
62 $this->addCoreDBData();
65 parent
::run( $result );
69 parent
::run( $result );
74 return $this->db
->getType() == 'oracle' ? self
::ORA_DB_PREFIX
: self
::DB_PREFIX
;
78 $rc = new ReflectionClass( $this );
79 return strpos( $rc->getDocComment(), '@group Database' ) !== false;
83 * Stub. If a test needs to add additional data to the database, it should
84 * implement this method and do so
86 function addDBData() {}
88 private function addCoreDBData() {
89 if ( $this->db
->getType() == 'oracle' ) {
91 # Insert 0 user to prevent FK violations
93 $this->db
->insert( 'user', array(
95 'user_name' => 'Anonymous' ), __METHOD__
, array( 'IGNORE' ) );
97 # Insert 0 page to prevent FK violations
99 $this->db
->insert( 'page', array(
101 'page_namespace' => 0,
103 'page_restrictions' => NULL,
105 'page_is_redirect' => 0,
108 'page_touched' => $this->db
->timestamp(),
110 'page_len' => 0 ), __METHOD__
, array( 'IGNORE' ) );
114 User
::resetIdByNameCache();
117 $user = User
::newFromName( 'UTSysop' );
119 if ( $user->idForName() == 0 ) {
120 $user->addToDatabase();
121 $user->setPassword( 'UTSysopPassword' );
123 $user->addGroup( 'sysop' );
124 $user->addGroup( 'bureaucrat' );
125 $user->saveSettings();
129 //Make 1 page with 1 revision
130 $article = new Article( Title
::newFromText( 'UTPage' ) );
131 $article->doEdit( 'UTContent',
135 User
::newFromName( 'UTSysop' ) );
138 private function initDB() {
140 if ( $wgDBprefix === $this->dbPrefix() ) {
141 throw new MWException( 'Cannot run unit tests, the database prefix is already "unittest_"' );
144 $tablesCloned = $this->listTables();
145 $dbClone = new CloneDatabase( $this->db
, $tablesCloned, $this->dbPrefix() );
146 $dbClone->useTemporaryTables( $this->useTemporaryTables
);
148 if ( ( $this->db
->getType() == 'oracle' ||
!$this->useTemporaryTables
) && $this->reuseDB
) {
149 CloneDatabase
::changePrefix( $this->dbPrefix() );
153 $dbClone->cloneTableStructure();
156 if ( $this->db
->getType() == 'oracle' ) {
157 $this->db
->query( 'BEGIN FILL_WIKI_INFO; END;' );
160 return $tablesCloned;
164 * Empty all tables so they can be repopulated for tests
166 private function resetDB() {
168 if ( $this->db
->getType() == 'oracle' ) {
169 if ( $this->useTemporaryTables
) {
170 wfGetLB()->closeAll();
171 $this->db
= wfGetDB( DB_MASTER
);
173 foreach( self
::$tablesCloned as $tbl ) {
174 if( $tbl == 'interwiki') continue;
175 $this->db
->query( 'TRUNCATE TABLE '.$this->db
->tableName($tbl), __METHOD__
);
179 foreach( self
::$tablesCloned as $tbl ) {
180 if( $tbl == 'interwiki' ||
$tbl == 'user' ) continue;
181 $this->db
->delete( $tbl, '*', __METHOD__
);
187 function __call( $func, $args ) {
188 static $compatibility = array(
189 'assertInternalType' => 'assertType',
190 'assertNotInternalType' => 'assertNotType',
191 'assertInstanceOf' => 'assertType',
192 'assertEmpty' => 'assertEmpty2',
195 if ( method_exists( $this->suite
, $func ) ) {
196 return call_user_func_array( array( $this->suite
, $func ), $args);
197 } elseif ( isset( $compatibility[$func] ) ) {
198 return call_user_func_array( array( $this, $compatibility[$func] ), $args);
200 throw new MWException( "Called non-existant $func method on "
201 . get_class( $this ) );
205 private function assertEmpty2( $value, $msg ) {
206 return $this->assertTrue( $value == '', $msg );
209 static private function unprefixTable( $tableName ) {
211 return substr( $tableName, strlen( $wgDBprefix ) );
214 static private function isNotUnittest( $table ) {
215 return strpos( $table, 'unittest_' ) !== 0;
218 protected function listTables() {
221 $tables = $this->db
->listTables( $wgDBprefix, __METHOD__
);
222 $tables = array_map( array( __CLASS__
, 'unprefixTable' ), $tables );
224 // Don't duplicate test tables from the previous fataled run
225 $tables = array_filter( $tables, array( __CLASS__
, 'isNotUnittest' ) );
227 if ( $this->db
->getType() == 'sqlite' ) {
228 $tables = array_flip( $tables );
229 // these are subtables of searchindex and don't need to be duped/dropped separately
230 unset( $tables['searchindex_content'] );
231 unset( $tables['searchindex_segdir'] );
232 unset( $tables['searchindex_segments'] );
233 $tables = array_flip( $tables );
238 protected function checkDbIsSupported() {
239 if( !in_array( $this->db
->getType(), $this->supportedDBs
) ) {
240 throw new MWException( $this->db
->getType() . " is not currently supported for unit testing." );
244 public function getCliArg( $offset ) {
246 if( isset( MediaWikiPHPUnitCommand
::$additionalOptions[$offset] ) ) {
247 return MediaWikiPHPUnitCommand
::$additionalOptions[$offset];
252 public function setCliArg( $offset, $value ) {
254 MediaWikiPHPUnitCommand
::$additionalOptions[$offset] = $value;
258 public static function disableInterwikis( $prefix, &$data ) {
263 * Don't throw a warning if $function is deprecated and called later
265 * @param $function String
268 function hideDeprecated( $function ) {
269 wfSuppressWarnings();
270 wfDeprecated( $function );