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() ) {
29 $this->db
= wfGetDB( DB_MASTER
);
34 $this->addCoreDBData();
38 parent
::run( $result );
41 function __destruct() {
46 $rc = new ReflectionClass( $this );
47 return strpos( $rc->getDocComment(), '@group Database' ) !== false;
50 function addDBData() {}
52 private function addCoreDBData() {
55 $user = User
::newFromName( 'UTSysop' );
57 if ( $user->idForName() == 0 ) {
58 $user->addToDatabase();
59 $user->setPassword( 'UTSysopPassword' );
61 $user->addGroup( 'sysop' );
62 $user->addGroup( 'bureaucrat' );
63 $user->saveSettings();
67 //Make 1 page with 1 revision
68 $article = new Article( Title
::newFromText( 'UTPage' ) );
69 $article->doEdit( 'UTContent',
73 User
::newFromName( 'UTSysop' ) );
76 private function initDB() {
79 $dbType = $this->db
->getType();
81 if ( $wgDBprefix === 'unittest_' ||
( $dbType == 'oracle' && $wgDBprefix === 'ut_' ) ) {
82 throw new MWException( 'Cannot run unit tests, the database prefix is already "unittest_"' );
85 $this->oldTablePrefix
= $wgDBprefix;
87 $tables = $this->listTables();
89 $prefix = $dbType != 'oracle' ?
'unittest_' : 'ut_';
91 $this->dbClone
= new CloneDatabase( $this->db
, $tables, $prefix );
92 $this->dbClone
->cloneTableStructure();
94 if ( $dbType == 'oracle' )
95 $this->db
->query( 'BEGIN FILL_WIKI_INFO; END;' );
97 if ( $dbType == 'oracle' ) {
98 # Insert 0 user to prevent FK violations
101 $this->db
->insert( 'user', array(
103 'user_name' => 'Anonymous' ) );
108 protected function destroyDB() {
111 if ( $this->useTemporaryTables
) {
112 # Don't need to do anything
114 //Temporary tables seem to be broken ATM, delete anyway
117 if( $this->db
->getType() == 'oracle' ) {
118 $tables = $this->db
->listTables( 'ut_', __METHOD__
);
121 $tables = $this->db
->listTables( 'unittest_', __METHOD__
);
124 foreach ( $tables as $table ) {
125 if( $this->db
->tableExists( "`$table`" ) ) {
126 $sql = $this->db
->getType() == 'oracle' ?
"DROP TABLE $table DROP CONSTRAINTS" : "DROP TABLE `$table`";
127 $this->db
->query( $sql, __METHOD__
);
131 if ( $this->db
->getType() == 'oracle' )
132 $this->db
->query( 'BEGIN FILL_WIKI_INFO; END;', __METHOD__
);
134 CloneDatabase
::changePrefix( $this->oldTablePrefix
);
137 function __call( $func, $args ) {
138 static $compatibility = array(
139 'assertInternalType' => 'assertType',
140 'assertNotInternalType' => 'assertNotType',
141 'assertInstanceOf' => 'assertType',
144 if ( method_exists( $this->suite
, $func ) ) {
145 return call_user_func_array( array( $this->suite
, $func ), $args);
146 } elseif ( isset( $compatibility[$func] ) ) {
147 return call_user_func_array( array( $this, $compatibility[$func] ), $args);
149 throw new MWException( "Called non-existant $func method on "
150 . get_class( $this ) );
154 static private function unprefixTable( $tableName ) {
156 return substr( $tableName, strlen( $wgDBprefix ) );
159 protected function listTables() {
162 $tables = $this->db
->listTables( $wgDBprefix, __METHOD__
);
163 $tables = array_map( array( __CLASS__
, 'unprefixTable' ), $tables );