3 abstract class MediaWikiTestCase
extends PHPUnit_Framework_TestCase
{
6 public $runDisabled = false;
8 protected static $databaseSetupDone = false;
11 protected $oldTablePrefix;
12 protected $useTemporaryTables = true;
14 function __construct( $name = null, array $data = array(), $dataName = '' ) {
16 $this->setName($name);
20 $this->dataName
= $dataName;
23 function run( PHPUnit_Framework_TestResult
$result = NULL ) {
25 if( $this->needsDB() ) {
27 $this->destroyDBCheck();
30 $this->addCoreDBData();
34 parent
::run( $result );
37 function __destruct() {
38 $this->destroyDBCheck();
41 function destroyDBCheck() {
42 if( is_object( $this->dbClone
) && $this->dbClone
instanceof CloneDatabase
) {
48 $rc = new ReflectionClass( $this );
49 return strpos( $rc->getDocComment(), '@group Database' ) !== false;
52 function addDBData() {}
54 private function addCoreDBData() {
57 $user = User
::newFromName( 'UTSysop' );
59 if ( $user->idForName() == 0 ) {
60 $user->addToDatabase();
61 $user->setPassword( 'UTSysopPassword' );
63 $user->addGroup( 'sysop' );
64 $user->addGroup( 'bureaucrat' );
65 $user->saveSettings();
69 //Make 1 page with 1 revision
70 $article = new Article( Title
::newFromText( 'UTPage' ) );
71 $article->doEdit( 'UTContent',
75 User
::newFromName( 'UTSysop' ) );
78 private function initDB() {
81 if ( self
::$databaseSetupDone ) {
85 $this->db
= wfGetDB( DB_MASTER
);
86 $dbType = $this->db
->getType();
88 if ( $wgDBprefix === 'unittest_' ||
( $dbType == 'oracle' && $wgDBprefix === 'ut_' ) ) {
89 throw new MWException( 'Cannot run unit tests, the database prefix is already "unittest_"' );
92 self
::$databaseSetupDone = true;
93 $this->oldTablePrefix
= $wgDBprefix;
95 # SqlBagOStuff broke when using temporary tables on r40209 (bug 15892).
96 # It seems to have been fixed since (r55079?).
97 # If it fails, $wgCaches[CACHE_DB] = new HashBagOStuff(); should work around it.
99 # CREATE TEMPORARY TABLE breaks if there is more than one server
100 if ( wfGetLB()->getServerCount() != 1 ) {
101 $this->useTemporaryTables
= false;
104 $temporary = $this->useTemporaryTables ||
$dbType == 'postgres';
106 $tables = $this->listTables();
108 $prefix = $dbType != 'oracle' ?
'unittest_' : 'ut_';
110 $this->dbClone
= new CloneDatabase( $this->db
, $tables, $prefix );
111 $this->dbClone
->useTemporaryTables( $temporary );
112 $this->dbClone
->cloneTableStructure();
114 if ( $dbType == 'oracle' )
115 $this->db
->query( 'BEGIN FILL_WIKI_INFO; END;' );
117 if ( $dbType == 'oracle' ) {
118 # Insert 0 user to prevent FK violations
121 $this->db
->insert( 'user', array(
123 'user_name' => 'Anonymous' ) );
128 protected function destroyDB() {
129 if ( !self
::$databaseSetupDone ) {
133 $this->dbClone
->destroy();
134 self
::$databaseSetupDone = false;
136 if ( $this->useTemporaryTables
) {
137 # Don't need to do anything
139 //Temporary tables seem to be broken ATM, delete anyway
142 if( $this->db
->getType() == 'oracle' ) {
143 $tables = $this->db
->listTables( 'ut_', __METHOD__
);
146 $tables = $this->db
->listTables( 'unittest_', __METHOD__
);
149 foreach ( $tables as $table ) {
150 $sql = $this->db
->getType() == 'oracle' ?
"DROP TABLE $table DROP CONSTRAINTS" : "DROP TABLE `$table`";
151 $this->db
->query( $sql );
154 if ( $this->db
->getType() == 'oracle' )
155 $this->db
->query( 'BEGIN FILL_WIKI_INFO; END;' );
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 );