Move MediaWikiTestCase class to a new file and load it from phpunit.php
[lhc/web/wiklou.git] / tests / phpunit / MediaWikiTestCase.php
1 <?php
2
3 abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
4 public $suite;
5 public $regex = '';
6 public $runDisabled = false;
7
8 protected static $databaseSetupDone = false;
9 protected $db;
10 protected $dbClone;
11 protected $oldTablePrefix;
12 protected $useTemporaryTables = true;
13
14 function __construct( $name = null, array $data = array(), $dataName = '' ) {
15 if ($name !== null) {
16 $this->setName($name);
17 }
18
19 $this->data = $data;
20 $this->dataName = $dataName;
21 }
22
23 function run( PHPUnit_Framework_TestResult $result = NULL ) {
24 if( $this->needsDB() && !is_object( $this->dbClone ) ) {
25 $this->initDB();
26 $this->addCoreDBData();
27 $this->addDBData();
28 }
29 parent::run( $result );
30 }
31
32 function __destruct() {
33 if( is_object( $this->dbClone ) && $this->dbClone instanceof CloneDatabase ) {
34 $this->destroyDB();
35 }
36 }
37
38 function needsDB() {
39 $rc = new ReflectionClass( $this );
40 return strpos( $rc->getDocComment(), '@group Database' ) !== false;
41 }
42
43 function addDBData() {}
44
45 private function addCoreDBData() {
46
47 //Make sysop user
48 $user = User::newFromName( 'UTSysop' );
49
50 if ( $user->idForName() == 0 ) {
51 $user->addToDatabase();
52 $user->setPassword( 'UTSysopPassword' );
53
54 $user->addGroup( 'sysop' );
55 $user->addGroup( 'bureaucrat' );
56 $user->saveSettings();
57 }
58
59
60 //Make 1 page with 1 revision
61 $article = new Article( Title::newFromText( 'UTPage' ) );
62 $article->doEdit( 'UTContent',
63 'UTPageSummary',
64 EDIT_NEW,
65 false,
66 User::newFromName( 'UTSysop' ) );
67 }
68
69 private function initDB() {
70 global $wgDBprefix;
71
72 if ( self::$databaseSetupDone ) {
73 return;
74 }
75
76 $this->db = wfGetDB( DB_MASTER );
77 $dbType = $this->db->getType();
78
79 if ( $wgDBprefix === 'unittest_' || ( $dbType == 'oracle' && $wgDBprefix === 'ut_' ) ) {
80 throw new MWException( 'Cannot run unit tests, the database prefix is already "unittest_"' );
81 }
82
83 self::$databaseSetupDone = true;
84 $this->oldTablePrefix = $wgDBprefix;
85
86 # SqlBagOStuff broke when using temporary tables on r40209 (bug 15892).
87 # It seems to have been fixed since (r55079?).
88 # If it fails, $wgCaches[CACHE_DB] = new HashBagOStuff(); should work around it.
89
90 # CREATE TEMPORARY TABLE breaks if there is more than one server
91 if ( wfGetLB()->getServerCount() != 1 ) {
92 $this->useTemporaryTables = false;
93 }
94
95 $temporary = $this->useTemporaryTables || $dbType == 'postgres';
96
97 $tables = $this->listTables();
98
99 $prefix = $dbType != 'oracle' ? 'unittest_' : 'ut_';
100
101 $this->dbClone = new CloneDatabase( $this->db, $tables, $prefix );
102 $this->dbClone->useTemporaryTables( $temporary );
103 $this->dbClone->cloneTableStructure();
104
105 if ( $dbType == 'oracle' )
106 $this->db->query( 'BEGIN FILL_WIKI_INFO; END;' );
107
108 if ( $dbType == 'oracle' ) {
109 # Insert 0 user to prevent FK violations
110
111 # Anonymous user
112 $this->db->insert( 'user', array(
113 'user_id' => 0,
114 'user_name' => 'Anonymous' ) );
115 }
116
117 }
118
119 protected function destroyDB() {
120 if ( !self::$databaseSetupDone ) {
121 return;
122 }
123
124 $this->dbClone->destroy();
125 self::$databaseSetupDone = false;
126
127 if ( $this->useTemporaryTables ) {
128 # Don't need to do anything
129 //return;
130 //Temporary tables seem to be broken ATM, delete anyway
131 }
132
133 if( $this->db->getType() == 'oracle' ) {
134 $tables = $this->db->listTables( 'ut_', __METHOD__ );
135 }
136 else {
137 $tables = $this->db->listTables( 'unittest_', __METHOD__ );
138 }
139
140 foreach ( $tables as $table ) {
141 $sql = $this->db->getType() == 'oracle' ? "DROP TABLE $table DROP CONSTRAINTS" : "DROP TABLE `$table`";
142 $this->db->query( $sql );
143 }
144
145 if ( $this->db->getType() == 'oracle' )
146 $this->db->query( 'BEGIN FILL_WIKI_INFO; END;' );
147
148
149 }
150
151 function __call( $func, $args ) {
152 static $compatibility = array(
153 'assertInternalType' => 'assertType',
154 'assertNotInternalType' => 'assertNotType',
155 'assertInstanceOf' => 'assertType',
156 );
157
158 if ( method_exists( $this->suite, $func ) ) {
159 return call_user_func_array( array( $this->suite, $func ), $args);
160 } elseif ( isset( $compatibility[$func] ) ) {
161 return call_user_func_array( array( $this, $compatibility[$func] ), $args);
162 } else {
163 throw new MWException( "Called non-existant $func method on "
164 . get_class( $this ) );
165 }
166 }
167
168 protected function listTables() {
169 global $wgDBprefix;
170
171 $tables = $this->db->listTables( $wgDBprefix, __METHOD__ );
172 $tables = array_map( create_function( '$table', 'global $wgDBprefix; return substr( $table, strlen( $wgDBprefix ) );' ), $tables );
173 return $tables;
174
175 }
176 }
177