3 abstract class MediaWikiTestCase
extends PHPUnit_Framework_TestCase
{
6 public $runDisabled = false;
12 protected $oldTablePrefix;
13 protected $useTemporaryTables = true;
14 private static $dbSetup = false;
17 * Table name prefixes. Oracle likes it shorter.
19 const DB_PREFIX
= 'unittest_';
20 const ORA_DB_PREFIX
= 'ut_';
22 protected $supportedDBs = array(
29 function __construct( $name = null, array $data = array(), $dataName = '' ) {
30 parent
::__construct( $name, $data, $dataName );
32 $this->backupGlobals
= false;
33 $this->backupStaticAttributes
= false;
36 function run( PHPUnit_Framework_TestResult
$result = NULL ) {
37 /* Some functions require some kind of caching, and will end up using the db,
38 * which we can't allow, as that would open a new connection for mysql.
39 * Replace with a HashBag. They would not be going to persist anyway.
41 ObjectCache
::$instances[CACHE_DB
] = new HashBagOStuff
;
43 if( $this->needsDB() ) {
47 $this->db
= wfGetDB( DB_MASTER
);
49 $this->checkDbIsSupported();
51 $this->oldTablePrefix
= $wgDBprefix;
53 if( !self
::$dbSetup ) {
55 self
::$dbSetup = true;
58 $this->addCoreDBData();
61 parent
::run( $result );
65 parent
::run( $result );
70 return $this->db
->getType() == 'oracle' ? self
::ORA_DB_PREFIX
: self
::DB_PREFIX
;
74 $rc = new ReflectionClass( $this );
75 return strpos( $rc->getDocComment(), '@group Database' ) !== false;
79 * Stub. If a test needs to add additional data to the database, it should
80 * implement this method and do so
82 function addDBData() {}
84 private function addCoreDBData() {
86 User
::resetIdByNameCache();
89 $user = User
::newFromName( 'UTSysop' );
91 if ( $user->idForName() == 0 ) {
92 $user->addToDatabase();
93 $user->setPassword( 'UTSysopPassword' );
95 $user->addGroup( 'sysop' );
96 $user->addGroup( 'bureaucrat' );
97 $user->saveSettings();
101 //Make 1 page with 1 revision
102 $article = new Article( Title
::newFromText( 'UTPage' ) );
103 $article->doEdit( 'UTContent',
107 User
::newFromName( 'UTSysop' ) );
110 private function initDB() {
112 if ( $wgDBprefix === $this->dbPrefix() ) {
113 throw new MWException( 'Cannot run unit tests, the database prefix is already "unittest_"' );
116 $dbClone = new CloneDatabase( $this->db
, $this->listTables(), $this->dbPrefix() );
117 $dbClone->useTemporaryTables( $this->useTemporaryTables
);
118 $dbClone->cloneTableStructure();
120 if ( $this->db
->getType() == 'oracle' ) {
121 $this->db
->query( 'BEGIN FILL_WIKI_INFO; END;' );
123 # Insert 0 user to prevent FK violations
125 $this->db
->insert( 'user', array(
127 'user_name' => 'Anonymous' ) );
132 * Empty all tables so they can be repopulated for tests
134 private function resetDB() {
136 foreach( $this->listTables() as $tbl ) {
137 if( $tbl == 'interwiki' ||
$tbl == 'user' ) continue;
138 $this->db
->delete( $tbl, '*', __METHOD__
);
143 protected function destroyDB() {
144 if ( $this->useTemporaryTables ||
is_null( $this->db
) ) {
145 # Don't need to do anything
149 $tables = $this->db
->listTables( $this->dbPrefix(), __METHOD__
);
151 foreach ( $tables as $table ) {
153 $sql = $this->db
->getType() == 'oracle' ?
"DROP TABLE $table CASCADE CONSTRAINTS PURGE" : "DROP TABLE `$table`";
154 $this->db
->query( $sql, __METHOD__
);
155 } catch( MWException
$mwe ) {}
158 if ( $this->db
->getType() == 'oracle' )
159 $this->db
->query( 'BEGIN FILL_WIKI_INFO; END;', __METHOD__
);
161 CloneDatabase
::changePrefix( $this->oldTablePrefix
);
165 function __call( $func, $args ) {
166 static $compatibility = array(
167 'assertInternalType' => 'assertType',
168 'assertNotInternalType' => 'assertNotType',
169 'assertInstanceOf' => 'assertType',
170 'assertEmpty' => 'assertEmpty2',
173 if ( method_exists( $this->suite
, $func ) ) {
174 return call_user_func_array( array( $this->suite
, $func ), $args);
175 } elseif ( isset( $compatibility[$func] ) ) {
176 return call_user_func_array( array( $this, $compatibility[$func] ), $args);
178 throw new MWException( "Called non-existant $func method on "
179 . get_class( $this ) );
183 private function assertEmpty2( $value, $msg ) {
184 return $this->assertTrue( $value == '', $msg );
187 static private function unprefixTable( $tableName ) {
189 return substr( $tableName, strlen( $wgDBprefix ) );
192 static private function isNotUnittest( $table ) {
193 return strpos( $table, 'unittest_' ) !== 0;
196 protected function listTables() {
199 $tables = $this->db
->listTables( $wgDBprefix, __METHOD__
);
200 $tables = array_map( array( __CLASS__
, 'unprefixTable' ), $tables );
202 // Don't duplicate test tables from the previous fataled run
203 $tables = array_filter( $tables, array( __CLASS__
, 'isNotUnittest' ) );
205 if ( $this->db
->getType() == 'sqlite' ) {
206 $tables = array_flip( $tables );
207 // these are subtables of searchindex and don't need to be duped/dropped separately
208 unset( $tables['searchindex_content'] );
209 unset( $tables['searchindex_segdir'] );
210 unset( $tables['searchindex_segments'] );
211 $tables = array_flip( $tables );
216 protected function checkDbIsSupported() {
217 if( !in_array( $this->db
->getType(), $this->supportedDBs
) ) {
218 throw new MWException( $this->db
->getType() . " is not currently supported for unit testing." );
222 public function getCliArg( $offset ) {
224 if( isset( MediaWikiPHPUnitCommand
::$additionalOptions[$offset] ) ) {
225 return MediaWikiPHPUnitCommand
::$additionalOptions[$offset];
230 public function setCliArg( $offset, $value ) {
232 MediaWikiPHPUnitCommand
::$additionalOptions[$offset] = $value;
236 public static function disableInterwikis( $prefix, &$data ) {
241 * Don't throw a warning if $function is deprecated and called later
243 * @param $function String
246 function hideDeprecated( $function ) {
247 wfSuppressWarnings();
248 wfDeprecated( $function );