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