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(
24 function __construct( $name = null, array $data = array(), $dataName = '' ) {
26 $this->setName($name);
30 $this->dataName
= $dataName;
32 $this->backupGlobals
= false;
33 $this->backupStaticAttributes
= false;
36 function run( PHPUnit_Framework_TestResult
$result = NULL ) {
38 if( $this->needsDB() ) {
42 $this->db
= wfGetDB( DB_MASTER
);
44 $this->checkDbIsSupported();
46 $this->oldTablePrefix
= $wgDBprefix;
51 $this->addCoreDBData();
54 parent
::run( $result );
59 parent
::run( $result );
65 function __destruct() {
66 if( $this->needsDB() ) {
72 $rc = new ReflectionClass( $this );
73 return strpos( $rc->getDocComment(), '@group Database' ) !== false;
77 * Stub. If a test needs to add additional data to the database, it should
78 * implement this method and do so
80 function addDBData() {}
82 private function addCoreDBData() {
85 $user = User
::newFromName( 'UTSysop' );
87 if ( $user->idForName() == 0 ) {
88 $user->addToDatabase();
89 $user->setPassword( 'UTSysopPassword' );
91 $user->addGroup( 'sysop' );
92 $user->addGroup( 'bureaucrat' );
93 $user->saveSettings();
97 //Make 1 page with 1 revision
98 $article = new Article( Title
::newFromText( 'UTPage' ) );
99 $article->doEdit( 'UTContent',
103 User
::newFromName( 'UTSysop' ) );
106 private function initDB() {
109 $dbType = $this->db
->getType();
111 if ( $wgDBprefix === self
::DB_PREFIX ||
( $dbType == 'oracle' && $wgDBprefix === self
::ORA_DB_PREFIX
) ) {
112 throw new MWException( 'Cannot run unit tests, the database prefix is already "unittest_"' );
115 $tables = $this->listTables();
117 $prefix = $dbType != 'oracle' ? self
::DB_PREFIX
: self
::ORA_DB_PREFIX
;
119 $this->dbClone
= new CloneDatabase( $this->db
, $tables, $prefix );
120 $this->dbClone
->useTemporaryTables( false ); //reported problems with temp tables, disabling until fixed
121 $this->dbClone
->cloneTableStructure();
123 if ( $dbType == 'oracle' )
124 $this->db
->query( 'BEGIN FILL_WIKI_INFO; END;' );
126 if ( $dbType == 'oracle' ) {
127 # Insert 0 user to prevent FK violations
130 $this->db
->insert( 'user', array(
132 'user_name' => 'Anonymous' ) );
137 protected function destroyDB() {
139 if ( $this->useTemporaryTables
) {
140 # Don't need to do anything
142 //Temporary tables seem to be broken ATM, delete anyway
145 if( is_null( $this->db
) ) {
149 if( $this->db
->getType() == 'oracle' ) {
150 $tables = $this->db
->listTables( self
::ORA_DB_PREFIX
, __METHOD__
);
153 $tables = $this->db
->listTables( self
::DB_PREFIX
, __METHOD__
);
156 foreach ( $tables as $table ) {
158 $sql = $this->db
->getType() == 'oracle' ?
"DROP TABLE $table DROP CONSTRAINTS" : "DROP TABLE `$table`";
159 $this->db
->query( $sql, __METHOD__
);
160 } catch( Exception
$e ) {
164 if ( $this->db
->getType() == 'oracle' )
165 $this->db
->query( 'BEGIN FILL_WIKI_INFO; END;', __METHOD__
);
167 CloneDatabase
::changePrefix( $this->oldTablePrefix
);
170 function __call( $func, $args ) {
171 static $compatibility = array(
172 'assertInternalType' => 'assertType',
173 'assertNotInternalType' => 'assertNotType',
174 'assertInstanceOf' => 'assertType',
177 if ( method_exists( $this->suite
, $func ) ) {
178 return call_user_func_array( array( $this->suite
, $func ), $args);
179 } elseif ( isset( $compatibility[$func] ) ) {
180 return call_user_func_array( array( $this, $compatibility[$func] ), $args);
182 throw new MWException( "Called non-existant $func method on "
183 . get_class( $this ) );
187 static private function unprefixTable( $tableName ) {
189 return substr( $tableName, strlen( $wgDBprefix ) );
192 protected function listTables() {
195 $tables = $this->db
->listTables( $wgDBprefix, __METHOD__
);
196 $tables = array_map( array( __CLASS__
, 'unprefixTable' ), $tables );
201 protected function checkDbIsSupported() {
202 if( !in_array( $this->db
->getType(), $this->supportedDBs
) ) {
203 throw new MWException( $this->db
->getType() . " is not currently supported for unit testing." );
207 public function getCliArg( $offset ) {
209 if( isset( MediaWikiPHPUnitCommand
::$additionalOptions[$offset] ) ) {
210 return MediaWikiPHPUnitCommand
::$additionalOptions[$offset];
215 public function setCliArg( $offset, $value ) {
217 MediaWikiPHPUnitCommand
::$additionalOptions[$offset] = $value;