MediaWikiTestCase: factored prefix generation to a separate function
[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 dbPrefix() {
74 return $this->db->getType() == 'oracle' ? self::ORA_DB_PREFIX : self::DB_PREFIX;
75 }
76
77 function needsDB() {
78 $rc = new ReflectionClass( $this );
79 return strpos( $rc->getDocComment(), '@group Database' ) !== false;
80 }
81
82 /**
83 * Stub. If a test needs to add additional data to the database, it should
84 * implement this method and do so
85 */
86 function addDBData() {}
87
88 private function addCoreDBData() {
89
90 User::resetIdByNameCache();
91
92 //Make sysop user
93 $user = User::newFromName( 'UTSysop' );
94
95 if ( $user->idForName() == 0 ) {
96 $user->addToDatabase();
97 $user->setPassword( 'UTSysopPassword' );
98
99 $user->addGroup( 'sysop' );
100 $user->addGroup( 'bureaucrat' );
101 $user->saveSettings();
102 }
103
104
105 //Make 1 page with 1 revision
106 $article = new Article( Title::newFromText( 'UTPage' ) );
107 $article->doEdit( 'UTContent',
108 'UTPageSummary',
109 EDIT_NEW,
110 false,
111 User::newFromName( 'UTSysop' ) );
112 }
113
114 private function initDB() {
115 global $wgDBprefix;
116
117 $dbType = $this->db->getType();
118
119 if ( $wgDBprefix === $this->dbPrefix() ) {
120 throw new MWException( 'Cannot run unit tests, the database prefix is already "unittest_"' );
121 }
122
123 $tables = $this->listTables();
124
125 $this->dbClone = new CloneDatabase( $this->db, $tables, $this->dbPrefix() );
126 $this->dbClone->useTemporaryTables( $this->useTemporaryTables );
127 $this->dbClone->cloneTableStructure();
128
129 if ( $dbType == 'oracle' )
130 $this->db->query( 'BEGIN FILL_WIKI_INFO; END;' );
131
132 if ( $dbType == 'oracle' ) {
133 # Insert 0 user to prevent FK violations
134
135 # Anonymous user
136 $this->db->insert( 'user', array(
137 'user_id' => 0,
138 'user_name' => 'Anonymous' ) );
139 }
140
141 }
142
143 protected function destroyDB() {
144
145 if ( $this->useTemporaryTables ) {
146 # Don't need to do anything
147 //return;
148 //Temporary tables seem to be broken ATM, delete anyway
149 }
150
151 if( is_null( $this->db ) ) {
152 return;
153 }
154
155 $tables = $this->db->listTables( $this->dbPrefix(), __METHOD__ );
156
157 foreach ( $tables as $table ) {
158 try {
159 $sql = $this->db->getType() == 'oracle' ? "DROP TABLE $table CASCADE CONSTRAINTS PURGE" : "DROP TABLE `$table`";
160 $this->db->query( $sql, __METHOD__ );
161 } catch( Exception $e ) {
162 }
163 }
164
165 if ( $this->db->getType() == 'oracle' )
166 $this->db->query( 'BEGIN FILL_WIKI_INFO; END;', __METHOD__ );
167
168 CloneDatabase::changePrefix( $this->oldTablePrefix );
169 }
170
171 function __call( $func, $args ) {
172 static $compatibility = array(
173 'assertInternalType' => 'assertType',
174 'assertNotInternalType' => 'assertNotType',
175 'assertInstanceOf' => 'assertType',
176 'assertEmpty' => 'assertEmpty2',
177 );
178
179 if ( method_exists( $this->suite, $func ) ) {
180 return call_user_func_array( array( $this->suite, $func ), $args);
181 } elseif ( isset( $compatibility[$func] ) ) {
182 return call_user_func_array( array( $this, $compatibility[$func] ), $args);
183 } else {
184 throw new MWException( "Called non-existant $func method on "
185 . get_class( $this ) );
186 }
187 }
188
189 private function assertEmpty2( $value, $msg ) {
190 return $this->assertTrue( $value == '', $msg );
191 }
192
193 static private function unprefixTable( $tableName ) {
194 global $wgDBprefix;
195 return substr( $tableName, strlen( $wgDBprefix ) );
196 }
197
198 protected function listTables() {
199 global $wgDBprefix;
200
201 $tables = $this->db->listTables( $wgDBprefix, __METHOD__ );
202 $tables = array_map( array( __CLASS__, 'unprefixTable' ), $tables );
203 return $tables;
204
205 }
206
207 protected function checkDbIsSupported() {
208 if( !in_array( $this->db->getType(), $this->supportedDBs ) ) {
209 throw new MWException( $this->db->getType() . " is not currently supported for unit testing." );
210 }
211 }
212
213 public function getCliArg( $offset ) {
214
215 if( isset( MediaWikiPHPUnitCommand::$additionalOptions[$offset] ) ) {
216 return MediaWikiPHPUnitCommand::$additionalOptions[$offset];
217 }
218
219 }
220
221 public function setCliArg( $offset, $value ) {
222
223 MediaWikiPHPUnitCommand::$additionalOptions[$offset] = $value;
224
225 }
226 }
227