Remove unused globals
[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 $db;
9 protected $dbClone;
10 protected $oldTablePrefix;
11 protected $useTemporaryTables = true;
12
13 protected $supportedDBs = array(
14 'mysql',
15 'sqlite'
16 );
17
18 function __construct( $name = null, array $data = array(), $dataName = '' ) {
19 if ($name !== null) {
20 $this->setName($name);
21 }
22
23 $this->data = $data;
24 $this->dataName = $dataName;
25
26 $this->backupGlobals = false;
27 $this->backupStaticAttributes = false;
28 }
29
30 function run( PHPUnit_Framework_TestResult $result = NULL ) {
31
32 if( $this->needsDB() ) {
33
34 global $wgDBprefix;
35
36 $this->db = wfGetDB( DB_MASTER );
37
38 $this->checkDbIsSupported();
39
40 $this->oldTablePrefix = $wgDBprefix;
41
42 $this->destroyDB();
43
44 $this->initDB();
45 $this->addCoreDBData();
46 $this->addDBData();
47
48 parent::run( $result );
49
50 $this->destroyDB();
51 }
52 else {
53 parent::run( $result );
54
55 }
56
57 }
58
59 function __destruct() {
60 if( $this->needsDB() ) {
61 $this->destroyDB();
62 }
63 }
64
65 function needsDB() {
66 $rc = new ReflectionClass( $this );
67 return strpos( $rc->getDocComment(), '@group Database' ) !== false;
68 }
69
70 function addDBData() {}
71
72 private function addCoreDBData() {
73
74 //Make sysop user
75 $user = User::newFromName( 'UTSysop' );
76
77 if ( $user->idForName() == 0 ) {
78 $user->addToDatabase();
79 $user->setPassword( 'UTSysopPassword' );
80
81 $user->addGroup( 'sysop' );
82 $user->addGroup( 'bureaucrat' );
83 $user->saveSettings();
84 }
85
86
87 //Make 1 page with 1 revision
88 $article = new Article( Title::newFromText( 'UTPage' ) );
89 $article->doEdit( 'UTContent',
90 'UTPageSummary',
91 EDIT_NEW,
92 false,
93 User::newFromName( 'UTSysop' ) );
94 }
95
96 private function initDB() {
97 global $wgDBprefix;
98
99 $dbType = $this->db->getType();
100
101 if ( $wgDBprefix === 'unittest_' || ( $dbType == 'oracle' && $wgDBprefix === 'ut_' ) ) {
102 throw new MWException( 'Cannot run unit tests, the database prefix is already "unittest_"' );
103 }
104
105 $tables = $this->listTables();
106
107 $prefix = $dbType != 'oracle' ? 'unittest_' : 'ut_';
108
109 $this->dbClone = new CloneDatabase( $this->db, $tables, $prefix );
110 $this->dbClone->useTemporaryTables( false ); //reported problems with temp tables, disabling until fixed
111 $this->dbClone->cloneTableStructure();
112
113 if ( $dbType == 'oracle' )
114 $this->db->query( 'BEGIN FILL_WIKI_INFO; END;' );
115
116 if ( $dbType == 'oracle' ) {
117 # Insert 0 user to prevent FK violations
118
119 # Anonymous user
120 $this->db->insert( 'user', array(
121 'user_id' => 0,
122 'user_name' => 'Anonymous' ) );
123 }
124
125 }
126
127 protected function destroyDB() {
128
129 if ( $this->useTemporaryTables ) {
130 # Don't need to do anything
131 //return;
132 //Temporary tables seem to be broken ATM, delete anyway
133 }
134
135 if( is_null( $this->db ) ) {
136 return;
137 }
138
139 if( $this->db->getType() == 'oracle' ) {
140 $tables = $this->db->listTables( 'ut_', __METHOD__ );
141 }
142 else {
143 $tables = $this->db->listTables( 'unittest_', __METHOD__ );
144 }
145
146 foreach ( $tables as $table ) {
147 try {
148 $sql = $this->db->getType() == 'oracle' ? "DROP TABLE $table DROP CONSTRAINTS" : "DROP TABLE `$table`";
149 $this->db->query( $sql, __METHOD__ );
150 } catch( Exception $e ) {
151 }
152 }
153
154 if ( $this->db->getType() == 'oracle' )
155 $this->db->query( 'BEGIN FILL_WIKI_INFO; END;', __METHOD__ );
156
157 CloneDatabase::changePrefix( $this->oldTablePrefix );
158 }
159
160 function __call( $func, $args ) {
161 static $compatibility = array(
162 'assertInternalType' => 'assertType',
163 'assertNotInternalType' => 'assertNotType',
164 'assertInstanceOf' => 'assertType',
165 );
166
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);
171 } else {
172 throw new MWException( "Called non-existant $func method on "
173 . get_class( $this ) );
174 }
175 }
176
177 static private function unprefixTable( $tableName ) {
178 global $wgDBprefix;
179 return substr( $tableName, strlen( $wgDBprefix ) );
180 }
181
182 protected function listTables() {
183 global $wgDBprefix;
184
185 $tables = $this->db->listTables( $wgDBprefix, __METHOD__ );
186 $tables = array_map( array( __CLASS__, 'unprefixTable' ), $tables );
187 return $tables;
188
189 }
190
191 protected function checkDbIsSupported() {
192 if( !in_array( $this->db->getType(), $this->supportedDBs ) ) {
193 throw new MWException( $this->db->getType() . " is not currently supported for unit testing." );
194 }
195 }
196
197 public function getCliArg( $offset ) {
198
199 if( isset( MediaWikiPHPUnitCommand::$additionalOptions[$offset] ) ) {
200 return MediaWikiPHPUnitCommand::$additionalOptions[$offset];
201 }
202
203 }
204
205 public function setCliArg( $offset, $value ) {
206
207 MediaWikiPHPUnitCommand::$additionalOptions[$offset] = $value;
208
209 }
210 }
211