A set method doesn't need to return anything (and besides, niether does $this->base...
[mediawiki.git] / tests / phpunit / MediaWikiTestCase.php
blob6ec8bdc7fb7926a6dac66ac70104d98cf05a86ef
1 <?php
3 abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
4 public $suite;
5 public $regex = '';
6 public $runDisabled = false;
8 /**
9 * @var DatabaseBase
11 protected $db;
12 protected $oldTablePrefix;
13 protected $useTemporaryTables = true;
14 protected $reuseDB = false;
15 protected $tablesUsed = array(); // tables with data
17 private static $dbSetup = false;
19 /**
20 * Table name prefixes. Oracle likes it shorter.
22 const DB_PREFIX = 'unittest_';
23 const ORA_DB_PREFIX = 'ut_';
25 protected $supportedDBs = array(
26 'mysql',
27 'sqlite',
28 'postgres',
29 'oracle'
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() ) {
47 global $wgDBprefix;
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 ) {
59 $this->initDB();
60 self::$dbSetup = true;
63 $this->addCoreDBData();
64 $this->addDBData();
66 parent::run( $result );
68 $this->resetDB();
69 } else {
70 parent::run( $result );
74 function dbPrefix() {
75 return $this->db->getType() == 'oracle' ? self::ORA_DB_PREFIX : self::DB_PREFIX;
78 function needsDB() {
79 $rc = new ReflectionClass( $this );
80 return strpos( $rc->getDocComment(), '@group Database' ) !== false;
83 /**
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
97 # Anonymous user
98 $this->db->insert( 'user', array(
99 'user_id' => 0,
100 'user_name' => 'Anonymous' ), __METHOD__, array( 'IGNORE' ) );
102 # Insert 0 page to prevent FK violations
103 # Blank page
104 $this->db->insert( 'page', array(
105 'page_id' => 0,
106 'page_namespace' => 0,
107 'page_title' => ' ',
108 'page_restrictions' => NULL,
109 'page_counter' => 0,
110 'page_is_redirect' => 0,
111 'page_is_new' => 0,
112 'page_random' => 0,
113 'page_touched' => $this->db->timestamp(),
114 'page_latest' => 0,
115 'page_len' => 0 ), __METHOD__, array( 'IGNORE' ) );
119 User::resetIdByNameCache();
121 //Make sysop user
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',
138 'UTPageSummary',
139 EDIT_NEW,
140 false,
141 User::newFromName( 'UTSysop' ) );
145 private function initDB() {
146 global $wgDBprefix;
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() );
157 $this->resetDB();
158 return;
159 } else {
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() {
172 if( $this->db ) {
173 if ( $this->db->getType() == 'oracle' ) {
174 if ( $this->useTemporaryTables ) {
175 wfGetLB()->closeAll();
176 $this->db = wfGetDB( DB_MASTER );
177 } else {
178 foreach( $this->tablesUsed as $tbl ) {
179 if( $tbl == 'interwiki') continue;
180 $this->db->query( 'TRUNCATE TABLE '.$this->db->tableName($tbl), __METHOD__ );
183 } else {
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);
204 } else {
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 ) {
215 global $wgDBprefix;
216 return substr( $tableName, strlen( $wgDBprefix ) );
219 static private function isNotUnittest( $table ) {
220 return strpos( $table, 'unittest_' ) !== 0;
223 protected function listTables() {
224 global $wgDBprefix;
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 );
240 return $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 ) {
264 return false;
268 * Don't throw a warning if $function is deprecated and called later
270 * @param $function String
271 * @return null
273 function hideDeprecated( $function ) {
274 wfSuppressWarnings();
275 wfDeprecated( $function );
276 wfRestoreWarnings();