3 abstract class MediaWikiTestCase
extends PHPUnit_Framework_TestCase
{
6 public $runDisabled = false;
12 protected $oldTablePrefix;
13 protected $useTemporaryTables = true;
14 protected $reuseDB = false;
15 protected $tablesUsed = array(); // tables with data
17 private static $dbSetup = false;
20 * Table name prefixes. Oracle likes it shorter.
22 const DB_PREFIX
= 'unittest_';
23 const ORA_DB_PREFIX
= 'ut_';
25 protected $supportedDBs = array(
32 function __construct( $name = null, array $data = array(), $dataName = '' ) {
33 parent
::__construct( $name, $data, $dataName );
35 $this->backupGlobals
= false;
36 $this->backupStaticAttributes
= false;
39 function run( PHPUnit_Framework_TestResult
$result = NULL ) {
40 /* Some functions require some kind of caching, and will end up using the db,
41 * which we can't allow, as that would open a new connection for mysql.
42 * Replace with a HashBag. They would not be going to persist anyway.
44 ObjectCache
::$instances[CACHE_DB
] = new HashBagOStuff
;
46 if( $this->needsDB() ) {
49 $this->useTemporaryTables
= !$this->getCliArg( 'use-normal-tables' );
50 $this->reuseDB
= $this->getCliArg('reuse-db');
52 $this->db
= wfGetDB( DB_MASTER
);
54 $this->checkDbIsSupported();
56 $this->oldTablePrefix
= $wgDBprefix;
58 if( !self
::$dbSetup ) {
60 self
::$dbSetup = true;
63 $this->addCoreDBData();
66 parent
::run( $result );
70 parent
::run( $result );
75 return $this->db
->getType() == 'oracle' ? self
::ORA_DB_PREFIX
: self
::DB_PREFIX
;
79 $rc = new ReflectionClass( $this );
80 return strpos( $rc->getDocComment(), '@group Database' ) !== false;
84 * Stub. If a test needs to add additional data to the database, it should
85 * implement this method and do so
87 function addDBData() {}
89 private function addCoreDBData() {
90 # disabled for performance
91 #$this->tablesUsed[] = 'page';
92 #$this->tablesUsed[] = 'revision';
94 if ( $this->db
->getType() == 'oracle' ) {
96 # Insert 0 user to prevent FK violations
98 $this->db
->insert( 'user', array(
100 'user_name' => 'Anonymous' ), __METHOD__
, array( 'IGNORE' ) );
102 # Insert 0 page to prevent FK violations
104 $this->db
->insert( 'page', array(
106 'page_namespace' => 0,
108 'page_restrictions' => NULL,
110 'page_is_redirect' => 0,
113 'page_touched' => $this->db
->timestamp(),
115 'page_len' => 0 ), __METHOD__
, array( 'IGNORE' ) );
119 User
::resetIdByNameCache();
122 $user = User
::newFromName( 'UTSysop' );
124 if ( $user->idForName() == 0 ) {
125 $user->addToDatabase();
126 $user->setPassword( 'UTSysopPassword' );
128 $user->addGroup( 'sysop' );
129 $user->addGroup( 'bureaucrat' );
130 $user->saveSettings();
134 //Make 1 page with 1 revision
135 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
136 if ( !$page->getId() == 0 ) {
137 $page->doEdit( 'UTContent',
141 User
::newFromName( 'UTSysop' ) );
145 private function initDB() {
147 if ( $wgDBprefix === $this->dbPrefix() ) {
148 throw new MWException( 'Cannot run unit tests, the database prefix is already "unittest_"' );
151 $tablesCloned = $this->listTables();
152 $dbClone = new CloneDatabase( $this->db
, $tablesCloned, $this->dbPrefix() );
153 $dbClone->useTemporaryTables( $this->useTemporaryTables
);
155 if ( ( $this->db
->getType() == 'oracle' ||
!$this->useTemporaryTables
) && $this->reuseDB
) {
156 CloneDatabase
::changePrefix( $this->dbPrefix() );
160 $dbClone->cloneTableStructure();
163 if ( $this->db
->getType() == 'oracle' ) {
164 $this->db
->query( 'BEGIN FILL_WIKI_INFO; END;' );
169 * Empty all tables so they can be repopulated for tests
171 private function resetDB() {
173 if ( $this->db
->getType() == 'oracle' ) {
174 if ( $this->useTemporaryTables
) {
175 wfGetLB()->closeAll();
176 $this->db
= wfGetDB( DB_MASTER
);
178 foreach( $this->tablesUsed
as $tbl ) {
179 if( $tbl == 'interwiki') continue;
180 $this->db
->query( 'TRUNCATE TABLE '.$this->db
->tableName($tbl), __METHOD__
);
184 foreach( $this->tablesUsed
as $tbl ) {
185 if( $tbl == 'interwiki' ||
$tbl == 'user' ) continue;
186 $this->db
->delete( $tbl, '*', __METHOD__
);
192 function __call( $func, $args ) {
193 static $compatibility = array(
194 'assertInternalType' => 'assertType',
195 'assertNotInternalType' => 'assertNotType',
196 'assertInstanceOf' => 'assertType',
197 'assertEmpty' => 'assertEmpty2',
200 if ( method_exists( $this->suite
, $func ) ) {
201 return call_user_func_array( array( $this->suite
, $func ), $args);
202 } elseif ( isset( $compatibility[$func] ) ) {
203 return call_user_func_array( array( $this, $compatibility[$func] ), $args);
205 throw new MWException( "Called non-existant $func method on "
206 . get_class( $this ) );
210 private function assertEmpty2( $value, $msg ) {
211 return $this->assertTrue( $value == '', $msg );
214 static private function unprefixTable( $tableName ) {
216 return substr( $tableName, strlen( $wgDBprefix ) );
219 static private function isNotUnittest( $table ) {
220 return strpos( $table, 'unittest_' ) !== 0;
223 protected function listTables() {
226 $tables = $this->db
->listTables( $wgDBprefix, __METHOD__
);
227 $tables = array_map( array( __CLASS__
, 'unprefixTable' ), $tables );
229 // Don't duplicate test tables from the previous fataled run
230 $tables = array_filter( $tables, array( __CLASS__
, 'isNotUnittest' ) );
232 if ( $this->db
->getType() == 'sqlite' ) {
233 $tables = array_flip( $tables );
234 // these are subtables of searchindex and don't need to be duped/dropped separately
235 unset( $tables['searchindex_content'] );
236 unset( $tables['searchindex_segdir'] );
237 unset( $tables['searchindex_segments'] );
238 $tables = array_flip( $tables );
243 protected function checkDbIsSupported() {
244 if( !in_array( $this->db
->getType(), $this->supportedDBs
) ) {
245 throw new MWException( $this->db
->getType() . " is not currently supported for unit testing." );
249 public function getCliArg( $offset ) {
251 if( isset( MediaWikiPHPUnitCommand
::$additionalOptions[$offset] ) ) {
252 return MediaWikiPHPUnitCommand
::$additionalOptions[$offset];
257 public function setCliArg( $offset, $value ) {
259 MediaWikiPHPUnitCommand
::$additionalOptions[$offset] = $value;
263 public static function disableInterwikis( $prefix, &$data ) {
268 * Don't throw a warning if $function is deprecated and called later
270 * @param $function String
273 function hideDeprecated( $function ) {
274 wfSuppressWarnings();
275 wfDeprecated( $function );