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 $rc = new ReflectionClass( $this );
75 return strpos( $rc->getDocComment(), '@group Database' ) !== false;
79 * Stub. If a test needs to add additional data to the database, it should
80 * implement this method and do so
82 function addDBData() {}
84 private function addCoreDBData() {
86 User
::resetIdByNameCache();
89 $user = User
::newFromName( 'UTSysop' );
91 if ( $user->idForName() == 0 ) {
92 $user->addToDatabase();
93 $user->setPassword( 'UTSysopPassword' );
95 $user->addGroup( 'sysop' );
96 $user->addGroup( 'bureaucrat' );
97 $user->saveSettings();
101 //Make 1 page with 1 revision
102 $article = new Article( Title
::newFromText( 'UTPage' ) );
103 $article->doEdit( 'UTContent',
107 User
::newFromName( 'UTSysop' ) );
110 private function initDB() {
113 $dbType = $this->db
->getType();
115 if ( $wgDBprefix === self
::DB_PREFIX ||
( $dbType == 'oracle' && $wgDBprefix === self
::ORA_DB_PREFIX
) ) {
116 throw new MWException( 'Cannot run unit tests, the database prefix is already "unittest_"' );
119 $tables = $this->listTables();
121 $prefix = $dbType != 'oracle' ? self
::DB_PREFIX
: self
::ORA_DB_PREFIX
;
123 $this->dbClone
= new CloneDatabase( $this->db
, $tables, $prefix );
124 $this->dbClone
->useTemporaryTables( $this->useTemporaryTables
);
125 $this->dbClone
->cloneTableStructure();
127 if ( $dbType == 'oracle' )
128 $this->db
->query( 'BEGIN FILL_WIKI_INFO; END;' );
130 if ( $dbType == 'oracle' ) {
131 # Insert 0 user to prevent FK violations
134 $this->db
->insert( 'user', array(
136 'user_name' => 'Anonymous' ) );
141 protected function destroyDB() {
143 if ( $this->useTemporaryTables
) {
144 # Don't need to do anything
146 //Temporary tables seem to be broken ATM, delete anyway
149 if( is_null( $this->db
) ) {
153 if( $this->db
->getType() == 'oracle' ) {
154 $tables = $this->db
->listTables( self
::ORA_DB_PREFIX
, __METHOD__
);
157 $tables = $this->db
->listTables( self
::DB_PREFIX
, __METHOD__
);
160 foreach ( $tables as $table ) {
162 $sql = $this->db
->getType() == 'oracle' ?
"DROP TABLE $table CASCADE CONSTRAINTS PURGE" : "DROP TABLE `$table`";
163 $this->db
->query( $sql, __METHOD__
);
164 } catch( Exception
$e ) {
168 if ( $this->db
->getType() == 'oracle' )
169 $this->db
->query( 'BEGIN FILL_WIKI_INFO; END;', __METHOD__
);
171 CloneDatabase
::changePrefix( $this->oldTablePrefix
);
174 function __call( $func, $args ) {
175 static $compatibility = array(
176 'assertInternalType' => 'assertType',
177 'assertNotInternalType' => 'assertNotType',
178 'assertInstanceOf' => 'assertType',
179 'assertEmpty' => 'assertEmpty2',
182 if ( method_exists( $this->suite
, $func ) ) {
183 return call_user_func_array( array( $this->suite
, $func ), $args);
184 } elseif ( isset( $compatibility[$func] ) ) {
185 return call_user_func_array( array( $this, $compatibility[$func] ), $args);
187 throw new MWException( "Called non-existant $func method on "
188 . get_class( $this ) );
192 private function assertEmpty2( $value, $msg ) {
193 return $this->assertTrue( $value == '', $msg );
196 static private function unprefixTable( $tableName ) {
198 return substr( $tableName, strlen( $wgDBprefix ) );
201 protected function listTables() {
204 $tables = $this->db
->listTables( $wgDBprefix, __METHOD__
);
205 $tables = array_map( array( __CLASS__
, 'unprefixTable' ), $tables );
210 protected function checkDbIsSupported() {
211 if( !in_array( $this->db
->getType(), $this->supportedDBs
) ) {
212 throw new MWException( $this->db
->getType() . " is not currently supported for unit testing." );
216 public function getCliArg( $offset ) {
218 if( isset( MediaWikiPHPUnitCommand
::$additionalOptions[$offset] ) ) {
219 return MediaWikiPHPUnitCommand
::$additionalOptions[$offset];
224 public function setCliArg( $offset, $value ) {
226 MediaWikiPHPUnitCommand
::$additionalOptions[$offset] = $value;