3 abstract class MediaWikiTestCase
extends PHPUnit_Framework_TestCase
{
6 public $runDisabled = false;
10 protected $oldTablePrefix;
11 protected $useTemporaryTables = true;
13 function __construct( $name = null, array $data = array(), $dataName = '' ) {
15 $this->setName($name);
19 $this->dataName
= $dataName;
21 $this->backupGlobals
= false;
22 $this->backupStaticAttributes
= false;
25 function run( PHPUnit_Framework_TestResult
$result = NULL ) {
27 if( $this->needsDB() ) {
31 $this->db
= wfGetDB( DB_MASTER
);
32 $this->oldTablePrefix
= $wgDBprefix;
37 $this->addCoreDBData();
40 parent
::run( $result );
45 parent
::run( $result );
51 function __destruct() {
56 $rc = new ReflectionClass( $this );
57 return strpos( $rc->getDocComment(), '@group Database' ) !== false;
60 function addDBData() {}
62 private function addCoreDBData() {
65 $user = User
::newFromName( 'UTSysop' );
67 if ( $user->idForName() == 0 ) {
68 $user->addToDatabase();
69 $user->setPassword( 'UTSysopPassword' );
71 $user->addGroup( 'sysop' );
72 $user->addGroup( 'bureaucrat' );
73 $user->saveSettings();
77 //Make 1 page with 1 revision
78 $article = new Article( Title
::newFromText( 'UTPage' ) );
79 $article->doEdit( 'UTContent',
83 User
::newFromName( 'UTSysop' ) );
86 private function initDB() {
89 $dbType = $this->db
->getType();
91 if ( $wgDBprefix === 'unittest_' ||
( $dbType == 'oracle' && $wgDBprefix === 'ut_' ) ) {
92 throw new MWException( 'Cannot run unit tests, the database prefix is already "unittest_"' );
95 $tables = $this->listTables();
97 $prefix = $dbType != 'oracle' ?
'unittest_' : 'ut_';
99 $this->dbClone
= new CloneDatabase( $this->db
, $tables, $prefix );
100 $this->dbClone
->cloneTableStructure();
102 if ( $dbType == 'oracle' )
103 $this->db
->query( 'BEGIN FILL_WIKI_INFO; END;' );
105 if ( $dbType == 'oracle' ) {
106 # Insert 0 user to prevent FK violations
109 $this->db
->insert( 'user', array(
111 'user_name' => 'Anonymous' ) );
116 protected function destroyDB() {
119 if ( $this->useTemporaryTables
) {
120 # Don't need to do anything
122 //Temporary tables seem to be broken ATM, delete anyway
125 if( $this->db
->getType() == 'oracle' ) {
126 $tables = $this->db
->listTables( 'ut_', __METHOD__
);
129 $tables = $this->db
->listTables( 'unittest_', __METHOD__
);
132 foreach ( $tables as $table ) {
133 if( $this->db
->tableExists( "`$table`" ) ) {
134 $sql = $this->db
->getType() == 'oracle' ?
"DROP TABLE $table DROP CONSTRAINTS" : "DROP TABLE `$table`";
135 $this->db
->query( $sql, __METHOD__
);
139 if ( $this->db
->getType() == 'oracle' )
140 $this->db
->query( 'BEGIN FILL_WIKI_INFO; END;', __METHOD__
);
142 CloneDatabase
::changePrefix( $this->oldTablePrefix
);
145 function __call( $func, $args ) {
146 static $compatibility = array(
147 'assertInternalType' => 'assertType',
148 'assertNotInternalType' => 'assertNotType',
149 'assertInstanceOf' => 'assertType',
152 if ( method_exists( $this->suite
, $func ) ) {
153 return call_user_func_array( array( $this->suite
, $func ), $args);
154 } elseif ( isset( $compatibility[$func] ) ) {
155 return call_user_func_array( array( $this, $compatibility[$func] ), $args);
157 throw new MWException( "Called non-existant $func method on "
158 . get_class( $this ) );
162 static private function unprefixTable( $tableName ) {
164 return substr( $tableName, strlen( $wgDBprefix ) );
167 protected function listTables() {
170 $tables = $this->db
->listTables( $wgDBprefix, __METHOD__
);
171 $tables = array_map( array( __CLASS__
, 'unprefixTable' ), $tables );