Finally commit the testing stuff I have been with this week. Could be considered...
[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 /**
14 * Table name prefixes. Oracle likes it shorter.
15 */
16 const DB_PREFIX = 'unittest_';
17 const ORA_DB_PREFIX = 'ut_';
18
19 protected $supportedDBs = array(
20 'mysql',
21 'sqlite',
22 'oracle'
23 );
24
25 function __construct( $name = null, array $data = array(), $dataName = '' ) {
26 parent::__construct( $name, $data, $dataName );
27
28 $this->backupGlobals = false;
29 $this->backupStaticAttributes = false;
30 }
31
32 function run( PHPUnit_Framework_TestResult $result = NULL ) {
33 global $wgCaches;
34 /* Some functions require some kind of caching, and will end up using the db,
35 * which we can't allow, as that would open a new connection for mysql.
36 * Replace with a HashBag. They would not be going to persist anyway.
37 */
38 $wgCaches[CACHE_DB] = new HashBagOStuff;
39
40 if( $this->needsDB() ) {
41
42 global $wgDBprefix;
43
44 $this->db = wfGetDB( DB_MASTER );
45
46 $this->checkDbIsSupported();
47
48 $this->oldTablePrefix = $wgDBprefix;
49
50 $this->destroyDB();
51
52 $this->initDB();
53 $this->addCoreDBData();
54 $this->addDBData();
55
56 parent::run( $result );
57
58 $this->destroyDB();
59 }
60 else {
61 parent::run( $result );
62
63 }
64
65 }
66
67 function __destruct() {
68 if( $this->needsDB() ) {
69 $this->destroyDB();
70 }
71 }
72
73 function needsDB() {
74 $rc = new ReflectionClass( $this );
75 return strpos( $rc->getDocComment(), '@group Database' ) !== false;
76 }
77
78 /**
79 * Stub. If a test needs to add additional data to the database, it should
80 * implement this method and do so
81 */
82 function addDBData() {}
83
84 private function addCoreDBData() {
85
86 User::resetIdByNameCache();
87
88 //Make sysop user
89 $user = User::newFromName( 'UTSysop' );
90
91 if ( $user->idForName() == 0 ) {
92 $user->addToDatabase();
93 $user->setPassword( 'UTSysopPassword' );
94
95 $user->addGroup( 'sysop' );
96 $user->addGroup( 'bureaucrat' );
97 $user->saveSettings();
98 }
99
100
101 //Make 1 page with 1 revision
102 $article = new Article( Title::newFromText( 'UTPage' ) );
103 $article->doEdit( 'UTContent',
104 'UTPageSummary',
105 EDIT_NEW,
106 false,
107 User::newFromName( 'UTSysop' ) );
108 }
109
110 private function initDB() {
111 global $wgDBprefix;
112
113 $dbType = $this->db->getType();
114
115 if ( $wgDBprefix === self::DB_PREFIX || ( $dbType == 'oracle' && $wgDBprefix === self::ORA_DB_PREFIX ) ) {
116 throw new MWException( 'Cannot run unit tests, the database prefix is already "unittest_"' );
117 }
118
119 $tables = $this->listTables();
120
121 $prefix = $dbType != 'oracle' ? self::DB_PREFIX : self::ORA_DB_PREFIX;
122
123 $this->dbClone = new CloneDatabase( $this->db, $tables, $prefix );
124 $this->dbClone->useTemporaryTables( $this->useTemporaryTables );
125 $this->dbClone->cloneTableStructure();
126
127 if ( $dbType == 'oracle' )
128 $this->db->query( 'BEGIN FILL_WIKI_INFO; END;' );
129
130 if ( $dbType == 'oracle' ) {
131 # Insert 0 user to prevent FK violations
132
133 # Anonymous user
134 $this->db->insert( 'user', array(
135 'user_id' => 0,
136 'user_name' => 'Anonymous' ) );
137 }
138
139 }
140
141 protected function destroyDB() {
142
143 if ( $this->useTemporaryTables ) {
144 # Don't need to do anything
145 //return;
146 //Temporary tables seem to be broken ATM, delete anyway
147 }
148
149 if( is_null( $this->db ) ) {
150 return;
151 }
152
153 if( $this->db->getType() == 'oracle' ) {
154 $tables = $this->db->listTables( self::ORA_DB_PREFIX, __METHOD__ );
155 }
156 else {
157 $tables = $this->db->listTables( self::DB_PREFIX, __METHOD__ );
158 }
159
160 foreach ( $tables as $table ) {
161 try {
162 $sql = $this->db->getType() == 'oracle' ? "DROP TABLE $table CASCADE CONSTRAINTS PURGE" : "DROP TABLE `$table`";
163 $this->db->query( $sql, __METHOD__ );
164 } catch( Exception $e ) {
165 }
166 }
167
168 if ( $this->db->getType() == 'oracle' )
169 $this->db->query( 'BEGIN FILL_WIKI_INFO; END;', __METHOD__ );
170
171 CloneDatabase::changePrefix( $this->oldTablePrefix );
172 }
173
174 function __call( $func, $args ) {
175 static $compatibility = array(
176 'assertInternalType' => 'assertType',
177 'assertNotInternalType' => 'assertNotType',
178 'assertInstanceOf' => 'assertType',
179 'assertEmpty' => 'assertEmpty2',
180 );
181
182 if ( method_exists( $this->suite, $func ) ) {
183 return call_user_func_array( array( $this->suite, $func ), $args);
184 } elseif ( isset( $compatibility[$func] ) ) {
185 return call_user_func_array( array( $this, $compatibility[$func] ), $args);
186 } else {
187 throw new MWException( "Called non-existant $func method on "
188 . get_class( $this ) );
189 }
190 }
191
192 private function assertEmpty2( $value, $msg ) {
193 return $this->assertTrue( $value == '', $msg );
194 }
195
196 static private function unprefixTable( $tableName ) {
197 global $wgDBprefix;
198 return substr( $tableName, strlen( $wgDBprefix ) );
199 }
200
201 protected function listTables() {
202 global $wgDBprefix;
203
204 $tables = $this->db->listTables( $wgDBprefix, __METHOD__ );
205 $tables = array_map( array( __CLASS__, 'unprefixTable' ), $tables );
206 return $tables;
207
208 }
209
210 protected function checkDbIsSupported() {
211 if( !in_array( $this->db->getType(), $this->supportedDBs ) ) {
212 throw new MWException( $this->db->getType() . " is not currently supported for unit testing." );
213 }
214 }
215
216 public function getCliArg( $offset ) {
217
218 if( isset( MediaWikiPHPUnitCommand::$additionalOptions[$offset] ) ) {
219 return MediaWikiPHPUnitCommand::$additionalOptions[$offset];
220 }
221
222 }
223
224 public function setCliArg( $offset, $value ) {
225
226 MediaWikiPHPUnitCommand::$additionalOptions[$offset] = $value;
227
228 }
229 }
230