3 abstract class MediaWikiTestCase
extends PHPUnit_Framework_TestCase
{
6 public $runDisabled = false;
10 protected $oldTablePrefix;
11 protected $useTemporaryTables = true;
14 * Table name prefixes. Oracle likes it shorter.
16 const DB_PREFIX
= 'unittest_';
17 const ORA_DB_PREFIX
= 'ut_';
19 protected $supportedDBs = array(
25 function __construct( $name = null, array $data = array(), $dataName = '' ) {
26 parent
::__construct( $name, $data, $dataName );
28 $this->backupGlobals
= false;
29 $this->backupStaticAttributes
= false;
32 function run( PHPUnit_Framework_TestResult
$result = NULL ) {
34 /* Some functions require some kind of caching, and will end up using the db,
35 * which we can't allow, as that would open a new connection for mysql.
36 * Replace with a HashBag. They would not be going to persist anyway.
38 $wgCaches[CACHE_DB
] = new HashBagOStuff
;
40 if( $this->needsDB() ) {
44 $this->db
= wfGetDB( DB_MASTER
);
46 $this->checkDbIsSupported();
48 $this->oldTablePrefix
= $wgDBprefix;
53 $this->addCoreDBData();
56 parent
::run( $result );
61 parent
::run( $result );
67 function __destruct() {
68 if( $this->needsDB() ) {
74 return $this->db
->getType() == 'oracle' ? self
::ORA_DB_PREFIX
: self
::DB_PREFIX
;
78 $rc = new ReflectionClass( $this );
79 return strpos( $rc->getDocComment(), '@group Database' ) !== false;
83 * Stub. If a test needs to add additional data to the database, it should
84 * implement this method and do so
86 function addDBData() {}
88 private function addCoreDBData() {
90 User
::resetIdByNameCache();
93 $user = User
::newFromName( 'UTSysop' );
95 if ( $user->idForName() == 0 ) {
96 $user->addToDatabase();
97 $user->setPassword( 'UTSysopPassword' );
99 $user->addGroup( 'sysop' );
100 $user->addGroup( 'bureaucrat' );
101 $user->saveSettings();
105 //Make 1 page with 1 revision
106 $article = new Article( Title
::newFromText( 'UTPage' ) );
107 $article->doEdit( 'UTContent',
111 User
::newFromName( 'UTSysop' ) );
114 private function initDB() {
117 $dbType = $this->db
->getType();
119 if ( $wgDBprefix === $this->dbPrefix() ) {
120 throw new MWException( 'Cannot run unit tests, the database prefix is already "unittest_"' );
123 $tables = $this->listTables();
125 $this->dbClone
= new CloneDatabase( $this->db
, $tables, $this->dbPrefix() );
126 $this->dbClone
->useTemporaryTables( $this->useTemporaryTables
);
127 $this->dbClone
->cloneTableStructure();
129 if ( $dbType == 'oracle' )
130 $this->db
->query( 'BEGIN FILL_WIKI_INFO; END;' );
132 if ( $dbType == 'oracle' ) {
133 # Insert 0 user to prevent FK violations
136 $this->db
->insert( 'user', array(
138 'user_name' => 'Anonymous' ) );
143 protected function destroyDB() {
145 if ( $this->useTemporaryTables
) {
146 # Don't need to do anything
148 //Temporary tables seem to be broken ATM, delete anyway
151 if( is_null( $this->db
) ) {
155 $tables = $this->db
->listTables( $this->dbPrefix(), __METHOD__
);
157 foreach ( $tables as $table ) {
159 $sql = $this->db
->getType() == 'oracle' ?
"DROP TABLE $table CASCADE CONSTRAINTS PURGE" : "DROP TABLE `$table`";
160 $this->db
->query( $sql, __METHOD__
);
161 } catch( Exception
$e ) {
165 if ( $this->db
->getType() == 'oracle' )
166 $this->db
->query( 'BEGIN FILL_WIKI_INFO; END;', __METHOD__
);
168 CloneDatabase
::changePrefix( $this->oldTablePrefix
);
171 function __call( $func, $args ) {
172 static $compatibility = array(
173 'assertInternalType' => 'assertType',
174 'assertNotInternalType' => 'assertNotType',
175 'assertInstanceOf' => 'assertType',
176 'assertEmpty' => 'assertEmpty2',
179 if ( method_exists( $this->suite
, $func ) ) {
180 return call_user_func_array( array( $this->suite
, $func ), $args);
181 } elseif ( isset( $compatibility[$func] ) ) {
182 return call_user_func_array( array( $this, $compatibility[$func] ), $args);
184 throw new MWException( "Called non-existant $func method on "
185 . get_class( $this ) );
189 private function assertEmpty2( $value, $msg ) {
190 return $this->assertTrue( $value == '', $msg );
193 static private function unprefixTable( $tableName ) {
195 return substr( $tableName, strlen( $wgDBprefix ) );
198 protected function listTables() {
201 $tables = $this->db
->listTables( $wgDBprefix, __METHOD__
);
202 $tables = array_map( array( __CLASS__
, 'unprefixTable' ), $tables );
207 protected function checkDbIsSupported() {
208 if( !in_array( $this->db
->getType(), $this->supportedDBs
) ) {
209 throw new MWException( $this->db
->getType() . " is not currently supported for unit testing." );
213 public function getCliArg( $offset ) {
215 if( isset( MediaWikiPHPUnitCommand
::$additionalOptions[$offset] ) ) {
216 return MediaWikiPHPUnitCommand
::$additionalOptions[$offset];
221 public function setCliArg( $offset, $value ) {
223 MediaWikiPHPUnitCommand
::$additionalOptions[$offset] = $value;