some more oracle-phpunit-fu (should not affect non-oracle)
[mediawiki.git] / tests / phpunit / MediaWikiTestCase.php
blob82b2a47e2b52beeeb21b0b84c32245fc9e840cf9
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 private static $dbSetup = false;
17 /**
18 * Table name prefixes. Oracle likes it shorter.
20 const DB_PREFIX = 'unittest_';
21 const ORA_DB_PREFIX = 'ut_';
23 protected $supportedDBs = array(
24 'mysql',
25 'sqlite',
26 'postgres',
27 'oracle'
30 function __construct( $name = null, array $data = array(), $dataName = '' ) {
31 parent::__construct( $name, $data, $dataName );
33 $this->backupGlobals = false;
34 $this->backupStaticAttributes = false;
37 function run( PHPUnit_Framework_TestResult $result = NULL ) {
38 /* Some functions require some kind of caching, and will end up using the db,
39 * which we can't allow, as that would open a new connection for mysql.
40 * Replace with a HashBag. They would not be going to persist anyway.
42 ObjectCache::$instances[CACHE_DB] = new HashBagOStuff;
44 if( $this->needsDB() ) {
45 global $wgDBprefix;
47 $this->useTemporaryTables = !$this->getCliArg( 'use-normal-tables' );
48 $this->reuseDB = $this->getCliArg('reuse-db');
50 $this->db = wfGetDB( DB_MASTER );
52 $this->checkDbIsSupported();
54 $this->oldTablePrefix = $wgDBprefix;
56 if( !self::$dbSetup ) {
57 $this->initDB();
58 self::$dbSetup = true;
61 $this->addCoreDBData();
62 $this->addDBData();
64 parent::run( $result );
66 $this->resetDB();
67 } else {
68 parent::run( $result );
72 function dbPrefix() {
73 return $this->db->getType() == 'oracle' ? self::ORA_DB_PREFIX : self::DB_PREFIX;
76 function needsDB() {
77 $rc = new ReflectionClass( $this );
78 return strpos( $rc->getDocComment(), '@group Database' ) !== false;
81 /**
82 * Stub. If a test needs to add additional data to the database, it should
83 * implement this method and do so
85 function addDBData() {}
87 private function addCoreDBData() {
88 if ( $this->db->getType() == 'oracle' ) {
90 # Insert 0 user to prevent FK violations
91 # Anonymous user
92 $this->db->insert( 'user', array(
93 'user_id' => 0,
94 'user_name' => 'Anonymous' ), __METHOD__, array( 'IGNORE' ) );
96 # Insert 0 page to prevent FK violations
97 # Blank page
98 $this->db->insert( 'page', array(
99 'page_id' => 0,
100 'page_namespace' => 0,
101 'page_title' => ' ',
102 'page_restrictions' => NULL,
103 'page_counter' => 0,
104 'page_is_redirect' => 0,
105 'page_is_new' => 0,
106 'page_random' => 0,
107 'page_touched' => $this->db->timestamp(),
108 'page_latest' => 0,
109 'page_len' => 0 ), __METHOD__, array( 'IGNORE' ) );
113 User::resetIdByNameCache();
115 //Make sysop user
116 $user = User::newFromName( 'UTSysop' );
118 if ( $user->idForName() == 0 ) {
119 $user->addToDatabase();
120 $user->setPassword( 'UTSysopPassword' );
122 $user->addGroup( 'sysop' );
123 $user->addGroup( 'bureaucrat' );
124 $user->saveSettings();
128 //Make 1 page with 1 revision
129 $article = new Article( Title::newFromText( 'UTPage' ) );
130 $article->doEdit( 'UTContent',
131 'UTPageSummary',
132 EDIT_NEW,
133 false,
134 User::newFromName( 'UTSysop' ) );
137 private function initDB() {
138 global $wgDBprefix;
139 if ( $wgDBprefix === $this->dbPrefix() ) {
140 throw new MWException( 'Cannot run unit tests, the database prefix is already "unittest_"' );
143 $dbClone = new CloneDatabase( $this->db, $this->listTables(), $this->dbPrefix() );
144 $dbClone->useTemporaryTables( $this->useTemporaryTables );
146 if ( ( $this->db->getType() == 'oracle' || !$this->useTemporaryTables ) && $this->reuseDB ) {
147 CloneDatabase::changePrefix( $this->dbPrefix() );
148 $this->resetDB();
149 return;
150 } else {
151 $dbClone->cloneTableStructure();
154 if ( $this->db->getType() == 'oracle' ) {
155 $this->db->query( 'BEGIN FILL_WIKI_INFO; END;' );
160 * Empty all tables so they can be repopulated for tests
162 private function resetDB() {
163 if( $this->db ) {
164 if ( $this->db->getType() == 'oracle' ) {
165 if ( $this->useTemporaryTables ) {
166 wfGetLB()->closeAll();
167 $this->db = wfGetDB( DB_MASTER );
168 } else {
169 foreach( $this->listTables() as $tbl ) {
170 if( $tbl == 'interwiki') continue;
171 $this->db->query( 'TRUNCATE TABLE '.$this->db->tableName($tbl), __METHOD__ );
174 } else {
175 foreach( $this->listTables() as $tbl ) {
176 if( $tbl == 'interwiki' || $tbl == 'user' ) continue;
177 $this->db->delete( $tbl, '*', __METHOD__ );
183 protected function destroyDB() {
184 if ( is_null( $this->db ) ||
185 ( $this->useTemporaryTables && $this->db->getType() != 'oracle' ) ||
186 ( $this->reuseDB ) ) {
187 # Don't need to do anything
188 return;
191 $tables = $this->db->listTables( $this->dbPrefix(), __METHOD__ );
193 foreach ( $tables as $table ) {
194 try {
195 $sql = $this->db->getType() == 'oracle' ? "DROP TABLE $table CASCADE CONSTRAINTS PURGE" : "DROP TABLE `$table`";
196 $this->db->query( $sql, __METHOD__ );
197 } catch( MWException $mwe ) {}
200 if ( $this->db->getType() == 'oracle' )
201 $this->db->query( 'BEGIN FILL_WIKI_INFO; END;', __METHOD__ );
203 CloneDatabase::changePrefix( $this->oldTablePrefix );
207 function __call( $func, $args ) {
208 static $compatibility = array(
209 'assertInternalType' => 'assertType',
210 'assertNotInternalType' => 'assertNotType',
211 'assertInstanceOf' => 'assertType',
212 'assertEmpty' => 'assertEmpty2',
215 if ( method_exists( $this->suite, $func ) ) {
216 return call_user_func_array( array( $this->suite, $func ), $args);
217 } elseif ( isset( $compatibility[$func] ) ) {
218 return call_user_func_array( array( $this, $compatibility[$func] ), $args);
219 } else {
220 throw new MWException( "Called non-existant $func method on "
221 . get_class( $this ) );
225 private function assertEmpty2( $value, $msg ) {
226 return $this->assertTrue( $value == '', $msg );
229 static private function unprefixTable( $tableName ) {
230 global $wgDBprefix;
231 return substr( $tableName, strlen( $wgDBprefix ) );
234 static private function isNotUnittest( $table ) {
235 return strpos( $table, 'unittest_' ) !== 0;
238 protected function listTables() {
239 global $wgDBprefix;
241 $tables = $this->db->listTables( $wgDBprefix, __METHOD__ );
242 $tables = array_map( array( __CLASS__, 'unprefixTable' ), $tables );
244 // Don't duplicate test tables from the previous fataled run
245 $tables = array_filter( $tables, array( __CLASS__, 'isNotUnittest' ) );
247 if ( $this->db->getType() == 'sqlite' ) {
248 $tables = array_flip( $tables );
249 // these are subtables of searchindex and don't need to be duped/dropped separately
250 unset( $tables['searchindex_content'] );
251 unset( $tables['searchindex_segdir'] );
252 unset( $tables['searchindex_segments'] );
253 $tables = array_flip( $tables );
255 return $tables;
258 protected function checkDbIsSupported() {
259 if( !in_array( $this->db->getType(), $this->supportedDBs ) ) {
260 throw new MWException( $this->db->getType() . " is not currently supported for unit testing." );
264 public function getCliArg( $offset ) {
266 if( isset( MediaWikiPHPUnitCommand::$additionalOptions[$offset] ) ) {
267 return MediaWikiPHPUnitCommand::$additionalOptions[$offset];
272 public function setCliArg( $offset, $value ) {
274 MediaWikiPHPUnitCommand::$additionalOptions[$offset] = $value;
278 public static function disableInterwikis( $prefix, &$data ) {
279 return false;
283 * Don't throw a warning if $function is deprecated and called later
285 * @param $function String
286 * @return null
288 function hideDeprecated( $function ) {
289 wfSuppressWarnings();
290 wfDeprecated( $function );
291 wfRestoreWarnings();