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(
28 function __construct( $name = null, array $data = array(), $dataName = '' ) {
29 parent
::__construct( $name, $data, $dataName );
31 $this->backupGlobals
= false;
32 $this->backupStaticAttributes
= false;
35 function run( PHPUnit_Framework_TestResult
$result = NULL ) {
36 /* Some functions require some kind of caching, and will end up using the db,
37 * which we can't allow, as that would open a new connection for mysql.
38 * Replace with a HashBag. They would not be going to persist anyway.
40 ObjectCache
::$instances[CACHE_DB
] = new HashBagOStuff
;
42 if( $this->needsDB() ) {
46 $this->db
= wfGetDB( DB_MASTER
);
48 $this->checkDbIsSupported();
50 $this->oldTablePrefix
= $wgDBprefix;
52 if( !self
::$dbSetup ) {
54 self
::$dbSetup = true;
58 $this->addCoreDBData();
61 parent
::run( $result );
63 $this->removeDBData();
66 parent
::run( $result );
70 function __destruct() {
71 if( $this->needsDB() ) {
77 return $this->db
->getType() == 'oracle' ? self
::ORA_DB_PREFIX
: self
::DB_PREFIX
;
81 $rc = new ReflectionClass( $this );
82 return strpos( $rc->getDocComment(), '@group Database' ) !== false;
86 * Stub. If a test needs to add additional data to the database, it should
87 * implement this method and do so
89 function addDBData() {}
92 * Stub. If a test needs to remove data from the database. Called after
95 function removeDBData() {}
97 private function addCoreDBData() {
99 User
::resetIdByNameCache();
102 $user = User
::newFromName( 'UTSysop' );
104 if ( $user->idForName() == 0 ) {
105 $user->addToDatabase();
106 $user->setPassword( 'UTSysopPassword' );
108 $user->addGroup( 'sysop' );
109 $user->addGroup( 'bureaucrat' );
110 $user->saveSettings();
114 //Make 1 page with 1 revision
115 $article = new Article( Title
::newFromText( 'UTPage' ) );
116 $article->doEdit( 'UTContent',
120 User
::newFromName( 'UTSysop' ) );
123 private function initDB() {
125 if ( $wgDBprefix === $this->dbPrefix() ) {
126 throw new MWException( 'Cannot run unit tests, the database prefix is already "unittest_"' );
129 $dbClone = new CloneDatabase( $this->db
, $this->listTables(), $this->dbPrefix() );
130 $dbClone->useTemporaryTables( $this->useTemporaryTables
);
131 $dbClone->cloneTableStructure();
135 * Empty all tables so they can be repopulated for tests
137 private function resetDB() {
139 foreach( $this->listTables() as $tbl ) {
140 $this->db
->delete( $tbl, '*', __METHOD__
);
143 if ( $this->db
->getType() == 'oracle' ) {
144 $this->db
->query( 'BEGIN FILL_WIKI_INFO; END;' );
146 # Insert 0 user to prevent FK violations
148 $this->db
->insert( 'user', array(
150 'user_name' => 'Anonymous' ) );
155 protected function destroyDB() {
157 if ( $this->useTemporaryTables
) {
158 # Don't need to do anything
160 //Temporary tables seem to be broken ATM, delete anyway
163 if( is_null( $this->db
) ) {
167 $tables = $this->db
->listTables( $this->dbPrefix(), __METHOD__
);
169 foreach ( $tables as $table ) {
171 $sql = $this->db
->getType() == 'oracle' ?
"DROP TABLE $table CASCADE CONSTRAINTS PURGE" : "DROP TABLE `$table`";
172 $this->db
->query( $sql, __METHOD__
);
173 } catch( Exception
$e ) {
177 if ( $this->db
->getType() == 'oracle' )
178 $this->db
->query( 'BEGIN FILL_WIKI_INFO; END;', __METHOD__
);
180 CloneDatabase
::changePrefix( $this->oldTablePrefix
);
183 function __call( $func, $args ) {
184 static $compatibility = array(
185 'assertInternalType' => 'assertType',
186 'assertNotInternalType' => 'assertNotType',
187 'assertInstanceOf' => 'assertType',
188 'assertEmpty' => 'assertEmpty2',
191 if ( method_exists( $this->suite
, $func ) ) {
192 return call_user_func_array( array( $this->suite
, $func ), $args);
193 } elseif ( isset( $compatibility[$func] ) ) {
194 return call_user_func_array( array( $this, $compatibility[$func] ), $args);
196 throw new MWException( "Called non-existant $func method on "
197 . get_class( $this ) );
201 private function assertEmpty2( $value, $msg ) {
202 return $this->assertTrue( $value == '', $msg );
205 static private function unprefixTable( $tableName ) {
207 return substr( $tableName, strlen( $wgDBprefix ) );
210 protected function listTables() {
213 $tables = $this->db
->listTables( $wgDBprefix, __METHOD__
);
214 $tables = array_map( array( __CLASS__
, 'unprefixTable' ), $tables );
216 if ( $this->db
->getType() == 'sqlite' ) {
217 $tables = array_flip( $tables );
218 // these are subtables of searchindex and don't need to be duped/dropped separately
219 unset( $tables['searchindex_content'] );
220 unset( $tables['searchindex_segdir'] );
221 unset( $tables['searchindex_segments'] );
222 $tables = array_flip( $tables );
228 protected function checkDbIsSupported() {
229 if( !in_array( $this->db
->getType(), $this->supportedDBs
) ) {
230 throw new MWException( $this->db
->getType() . " is not currently supported for unit testing." );
234 public function getCliArg( $offset ) {
236 if( isset( MediaWikiPHPUnitCommand
::$additionalOptions[$offset] ) ) {
237 return MediaWikiPHPUnitCommand
::$additionalOptions[$offset];
242 public function setCliArg( $offset, $value ) {
244 MediaWikiPHPUnitCommand
::$additionalOptions[$offset] = $value;