3 abstract class MediaWikiTestCase
extends PHPUnit_Framework_TestCase
{
6 public $runDisabled = false;
10 protected $oldTablePrefix;
11 protected $useTemporaryTables = true;
13 protected $supportedDBs = array(
18 function __construct( $name = null, array $data = array(), $dataName = '' ) {
20 $this->setName($name);
24 $this->dataName
= $dataName;
26 $this->backupGlobals
= false;
27 $this->backupStaticAttributes
= false;
30 function run( PHPUnit_Framework_TestResult
$result = NULL ) {
32 if( $this->needsDB() ) {
36 $this->db
= wfGetDB( DB_MASTER
);
38 $this->checkDbIsSupported();
40 $this->oldTablePrefix
= $wgDBprefix;
45 $this->addCoreDBData();
48 parent
::run( $result );
53 parent
::run( $result );
59 function __destruct() {
60 if( $this->needsDB() ) {
66 $rc = new ReflectionClass( $this );
67 return strpos( $rc->getDocComment(), '@group Database' ) !== false;
70 function addDBData() {}
72 private function addCoreDBData() {
75 $user = User
::newFromName( 'UTSysop' );
77 if ( $user->idForName() == 0 ) {
78 $user->addToDatabase();
79 $user->setPassword( 'UTSysopPassword' );
81 $user->addGroup( 'sysop' );
82 $user->addGroup( 'bureaucrat' );
83 $user->saveSettings();
87 //Make 1 page with 1 revision
88 $article = new Article( Title
::newFromText( 'UTPage' ) );
89 $article->doEdit( 'UTContent',
93 User
::newFromName( 'UTSysop' ) );
96 private function initDB() {
99 $dbType = $this->db
->getType();
101 if ( $wgDBprefix === 'unittest_' ||
( $dbType == 'oracle' && $wgDBprefix === 'ut_' ) ) {
102 throw new MWException( 'Cannot run unit tests, the database prefix is already "unittest_"' );
105 $tables = $this->listTables();
107 $prefix = $dbType != 'oracle' ?
'unittest_' : 'ut_';
109 $this->dbClone
= new CloneDatabase( $this->db
, $tables, $prefix );
110 $this->dbClone
->useTemporaryTables( false ); //reported problems with temp tables, disabling until fixed
111 $this->dbClone
->cloneTableStructure();
113 if ( $dbType == 'oracle' )
114 $this->db
->query( 'BEGIN FILL_WIKI_INFO; END;' );
116 if ( $dbType == 'oracle' ) {
117 # Insert 0 user to prevent FK violations
120 $this->db
->insert( 'user', array(
122 'user_name' => 'Anonymous' ) );
127 protected function destroyDB() {
129 if ( $this->useTemporaryTables
) {
130 # Don't need to do anything
132 //Temporary tables seem to be broken ATM, delete anyway
135 if( is_null( $this->db
) ) {
139 if( $this->db
->getType() == 'oracle' ) {
140 $tables = $this->db
->listTables( 'ut_', __METHOD__
);
143 $tables = $this->db
->listTables( 'unittest_', __METHOD__
);
146 foreach ( $tables as $table ) {
148 $sql = $this->db
->getType() == 'oracle' ?
"DROP TABLE $table DROP CONSTRAINTS" : "DROP TABLE `$table`";
149 $this->db
->query( $sql, __METHOD__
);
150 } catch( Exception
$e ) {
154 if ( $this->db
->getType() == 'oracle' )
155 $this->db
->query( 'BEGIN FILL_WIKI_INFO; END;', __METHOD__
);
157 CloneDatabase
::changePrefix( $this->oldTablePrefix
);
160 function __call( $func, $args ) {
161 static $compatibility = array(
162 'assertInternalType' => 'assertType',
163 'assertNotInternalType' => 'assertNotType',
164 'assertInstanceOf' => 'assertType',
167 if ( method_exists( $this->suite
, $func ) ) {
168 return call_user_func_array( array( $this->suite
, $func ), $args);
169 } elseif ( isset( $compatibility[$func] ) ) {
170 return call_user_func_array( array( $this, $compatibility[$func] ), $args);
172 throw new MWException( "Called non-existant $func method on "
173 . get_class( $this ) );
177 static private function unprefixTable( $tableName ) {
179 return substr( $tableName, strlen( $wgDBprefix ) );
182 protected function listTables() {
185 $tables = $this->db
->listTables( $wgDBprefix, __METHOD__
);
186 $tables = array_map( array( __CLASS__
, 'unprefixTable' ), $tables );
191 protected function checkDbIsSupported() {
192 if( !in_array( $this->db
->getType(), $this->supportedDBs
) ) {
193 throw new MWException( $this->db
->getType() . " is not currently supported for unit testing." );
197 public function getCliArg( $offset ) {
199 if( isset( MediaWikiPHPUnitCommand
::$additionalOptions[$offset] ) ) {
200 return MediaWikiPHPUnitCommand
::$additionalOptions[$offset];
205 public function setCliArg( $offset, $value ) {
207 MediaWikiPHPUnitCommand
::$additionalOptions[$offset] = $value;