r79180 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r79179‎ | r79180 | r79181 >
Date:15:52, 29 December 2010
Author:platonides
Status:ok
Tags:
Comment:
Move MediaWikiTestCase class to a new file and load it from phpunit.php
This restores the ability to run single tests with
php phpunit.php includes/filename.php
Modified paths:
  • /trunk/phase3/tests/phpunit/MediaWikiTestCase.php (added) (history)
  • /trunk/phase3/tests/phpunit/bootstrap.php (modified) (history)
  • /trunk/phase3/tests/phpunit/phpunit.php (modified) (history)

Diff [purge]

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

Status & tagging log