MediaWiki  master
phpunit.php
Go to the documentation of this file.
1 #!/usr/bin/env php
2 <?php
9 // Set a flag which can be used to detect when other scripts have been entered
10 // through this entry point or not.
11 define( 'MW_PHPUNIT_TEST', true );
12 
13 $wgPhpUnitClass = 'PHPUnit_TextUI_Command';
14 
15 // Start up MediaWiki in command-line mode
16 require_once dirname( dirname( __DIR__ ) ) . "/maintenance/Maintenance.php";
17 
19 
20  public static $additionalOptions = [
21  'regex' => false,
22  'file' => false,
23  'use-filebackend' => false,
24  'use-bagostuff' => false,
25  'use-jobqueue' => false,
26  'keep-uploads' => false,
27  'use-normal-tables' => false,
28  'reuse-db' => false,
29  'wiki' => false,
30  ];
31 
32  public function __construct() {
33  parent::__construct();
34  $this->addOption(
35  'with-phpunitclass',
36  'Class name of the PHPUnit entry point to use',
37  false,
38  true
39  );
40  $this->addOption(
41  'debug-tests',
42  'Log testing activity to the PHPUnitCommand log channel.',
43  false, # not required
44  false # no arg needed
45  );
46  $this->addOption(
47  'regex',
48  'Only run parser tests that match the given regex.',
49  false,
50  true
51  );
52  $this->addOption( 'file', 'File describing parser tests.', false, true );
53  $this->addOption( 'use-filebackend', 'Use filebackend', false, true );
54  $this->addOption( 'use-bagostuff', 'Use bagostuff', false, true );
55  $this->addOption( 'use-jobqueue', 'Use jobqueue', false, true );
56  $this->addOption(
57  'keep-uploads',
58  'Re-use the same upload directory for each test, don\'t delete it.',
59  false,
60  false
61  );
62  $this->addOption( 'use-normal-tables', 'Use normal DB tables.', false, false );
63  $this->addOption(
64  'reuse-db', 'Init DB only if tables are missing and keep after finish.',
65  false,
66  false
67  );
68  }
69 
70  public function finalSetup() {
71  parent::finalSetup();
72 
81 
82  // Inject test autoloader
83  require_once __DIR__ . '/../TestsAutoLoader.php';
84 
85  // wfWarn should cause tests to fail
86  $wgDevelopmentWarnings = true;
87 
88  // Make sure all caches and stashes are either disabled or use
89  // in-process cache only to prevent tests from using any preconfigured
90  // cache meant for the local wiki from outside the test run.
91  // See also MediaWikiTestCase::run() which mocks CACHE_DB and APC.
92 
93  // Disabled in DefaultSettings, override local settings
94  $wgMainWANCache =
95  $wgMainCacheType = CACHE_NONE;
96  // Uses CACHE_ANYTHING in DefaultSettings, use hash instead of db
97  $wgMessageCacheType =
98  $wgParserCacheType =
100  $wgLanguageConverterCacheType = 'hash';
101  // Uses db-replicated in DefaultSettings
102  $wgMainStash = 'hash';
103  // Use memory job queue
104  $wgJobTypeConf = [
105  'default' => [ 'class' => 'JobQueueMemory', 'order' => 'fifo' ],
106  ];
107 
108  $wgUseDatabaseMessages = false; # Set for future resets
109 
110  // Assume UTC for testing purposes
111  $wgLocaltimezone = 'UTC';
112 
113  $wgLocalisationCacheConf['storeClass'] = 'LCStoreNull';
114 
115  // Generic MediaWiki\Session\SessionManager configuration for tests
116  // We use CookieSessionProvider because things might be expecting
117  // cookies to show up in a FauxRequest somewhere.
118  $wgSessionProviders = [
119  [
121  'args' => [ [
122  'priority' => 30,
123  'callUserSetCookiesHook' => true,
124  ] ],
125  ],
126  ];
127 
128  // Single-iteration PBKDF2 session secret derivation, for speed.
129  $wgSessionPbkdf2Iterations = 1;
130 
131  // Generic AuthManager configuration for testing
132  $wgAuthManagerConfig = [
133  'preauth' => [],
134  'primaryauth' => [
135  [
137  'args' => [ [
138  'authoritative' => false,
139  ] ],
140  ],
141  [
143  'args' => [ [
144  'authoritative' => true,
145  ] ],
146  ],
147  ],
148  'secondaryauth' => [],
149  ];
150  $wgAuth = $wgDisableAuthManager ? new AuthPlugin : new MediaWiki\Auth\AuthManagerAuthPlugin();
151 
152  // Bug 44192 Do not attempt to send a real e-mail
153  Hooks::clear( 'AlternateUserMailer' );
155  'AlternateUserMailer',
156  function () {
157  return false;
158  }
159  );
160  // xdebug's default of 100 is too low for MediaWiki
161  ini_set( 'xdebug.max_nesting_level', 1000 );
162 
163  // Bug T116683 serialize_precision of 100
164  // may break testing against floating point values
165  // treated with PHP's serialize()
166  ini_set( 'serialize_precision', 17 );
167 
168  // TODO: we should call MediaWikiTestCase::prepareServices( new GlobalVarConfig() ) here.
169  // But PHPUnit may not be loaded yet, so we have to wait until just
170  // before PHPUnit_TextUI_Command::main() is executed at the end of this file.
171  }
172 
173  public function execute() {
174  global $IP;
175 
176  // Deregister handler from MWExceptionHandler::installHandle so that PHPUnit's own handler
177  // stays in tact.
178  // Has to in execute() instead of finalSetup(), because finalSetup() runs before
179  // doMaintenance.php includes Setup.php, which calls MWExceptionHandler::installHandle().
180  restore_error_handler();
181 
182  $this->forceFormatServerArgv();
183 
184  # Make sure we have --configuration or PHPUnit might complain
185  if ( !in_array( '--configuration', $_SERVER['argv'] ) ) {
186  // Hack to eliminate the need to use the Makefile (which sucks ATM)
187  array_splice( $_SERVER['argv'], 1, 0,
188  [ '--configuration', $IP . '/tests/phpunit/suite.xml' ] );
189  }
190 
191  if ( $this->hasOption( 'with-phpunitclass' ) ) {
193  $wgPhpUnitClass = $this->getOption( 'with-phpunitclass' );
194 
195  # Cleanup $args array so the option and its value do not
196  # pollute PHPUnit
197  $key = array_search( '--with-phpunitclass', $_SERVER['argv'] );
198  unset( $_SERVER['argv'][$key] ); // the option
199  unset( $_SERVER['argv'][$key + 1] ); // its value
200  $_SERVER['argv'] = array_values( $_SERVER['argv'] );
201  }
202 
203  $key = array_search( '--debug-tests', $_SERVER['argv'] );
204  if ( $key !== false && array_search( '--printer', $_SERVER['argv'] ) === false ) {
205  unset( $_SERVER['argv'][$key] );
206  array_splice( $_SERVER['argv'], 1, 0, 'MediaWikiPHPUnitTestListener' );
207  array_splice( $_SERVER['argv'], 1, 0, '--printer' );
208  }
209 
210  foreach ( self::$additionalOptions as $option => $default ) {
211  $key = array_search( '--' . $option, $_SERVER['argv'] );
212  if ( $key !== false ) {
213  unset( $_SERVER['argv'][$key] );
214  if ( $this->mParams[$option]['withArg'] ) {
215  self::$additionalOptions[$option] = $_SERVER['argv'][$key + 1];
216  unset( $_SERVER['argv'][$key + 1] );
217  } else {
218  self::$additionalOptions[$option] = true;
219  }
220  }
221  }
222 
223  }
224 
225  public function getDbType() {
226  return Maintenance::DB_ADMIN;
227  }
228 
233  private function forceFormatServerArgv() {
234  $argv = [];
235  foreach ( $_SERVER['argv'] as $key => $arg ) {
236  if ( $key === 0 ) {
237  $argv[0] = $arg;
238  } elseif ( strstr( $arg, '=' ) ) {
239  foreach ( explode( '=', $arg, 2 ) as $argPart ) {
240  $argv[] = $argPart;
241  }
242  } else {
243  $argv[] = $arg;
244  }
245  }
246  $_SERVER['argv'] = $argv;
247  }
248 
249 }
250 
251 $maintClass = 'PHPUnitMaintClass';
253 
254 if ( !class_exists( 'PHPUnit_Framework_TestCase' ) ) {
255  echo "PHPUnit not found. Please install it and other dev dependencies by
256 running `composer install` in MediaWiki root directory.\n";
257  exit( 1 );
258 }
259 if ( !class_exists( $wgPhpUnitClass ) ) {
260  echo "PHPUnit entry point '" . $wgPhpUnitClass . "' not found. Please make sure you installed
261 the containing component and check the spelling of the class name.\n";
262  exit( 1 );
263 }
264 
265 echo defined( 'HHVM_VERSION' ) ?
266  'Using HHVM ' . HHVM_VERSION . ' (' . PHP_VERSION . ")\n" :
267  'Using PHP ' . PHP_VERSION . "\n";
268 
269 // Prepare global services for unit tests.
270 // FIXME: this should be done in the finalSetup() method,
271 // but PHPUnit may not have been loaded at that point.
273 
static $additionalOptions
Definition: phpunit.php:20
$wgMainStash
Main object stash type.
$IP
Definition: WebStart.php:58
$maintClass
Definition: phpunit.php:251
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: maintenance.txt:39
Authentication plugin interface.
Definition: AuthPlugin.php:38
$wgAuthManagerConfig
Configure AuthManager.
hasOption($name)
Checks to see if a particular param exists.
$wgPhpUnitClass
Definition: phpunit.php:13
$wgMainWANCache
Main Wide-Area-Network cache type.
$wgAuth $wgAuth
Authentication plugin.
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
when a variable name is used in a it is silently declared as a new local masking the global
Definition: design.txt:93
$wgSessionPbkdf2Iterations
Number of internal PBKDF2 iterations to use when deriving session secrets.
forceFormatServerArgv()
Force the format of elements in $_SERVER['argv'].
Definition: phpunit.php:233
Accesses configuration settings from $GLOBALS.
addOption($name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
static register($name, $callback)
Attach an event handler to a given hook.
Definition: Hooks.php:49
Backwards-compatibility wrapper for AuthManager via $wgAuth.
$wgParserCacheType
The cache type for storing article HTML.
static prepareServices(Config $bootstrapConfig)
Prepare service configuration for unit testing.
static clear($name)
Clears hooks registered via Hooks::register().
Definition: Hooks.php:66
$wgLocalisationCacheConf
Localisation cache configuration.
$wgSessionProviders
MediaWiki\Session\SessionProvider configuration.
$wgMessageCacheType
The cache type for storing the contents of the MediaWiki namespace.
$wgUseDatabaseMessages
Translation using MediaWiki: namespace.
const DB_ADMIN
Definition: Maintenance.php:58
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such and we might be restricted by PHP settings such as safe mode or open_basedir We cannot assume that the software even has read access anywhere useful Many shared hosts run all users web applications under the same so they can t rely on Unix and must forbid reads to even standard directories like tmp lest users read each others files We cannot assume that the user has the ability to install or run any programs not written as web accessible PHP scripts Since anything that works on cheap shared hosting will work if you have shell or root access MediaWiki s design is based around catering to the lowest common denominator Although we support higher end setups as the way many things work by default is tailored toward shared hosting These defaults are unconventional from the point of view of and they certainly aren t ideal for someone who s installing MediaWiki as MediaWiki does not conform to normal Unix filesystem layout Hopefully we ll offer direct support for standard layouts in the future
CACHE_MEMCACHED $wgMainCacheType
Definition: memcached.txt:63
$wgLanguageConverterCacheType
The cache type for storing language conversion tables, which are used when parsing certain text and i...
$wgLocaltimezone
Fake out the timezone that the server thinks it's in.
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
getOption($name, $default=null)
Get an option, or return the default.
$wgSessionCacheType
The cache type for storing session data.
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
$wgJobTypeConf
Map of job types to configuration arrays.
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
Some quick notes on the file repository architecture Functionality as driven by data model *The repository object stores configuration information about a file storage method *The file object is a process local cache of information about a particular file Thus the file object is the primary public entry point for obtaining information about since access via the file object can be whereas access via the repository should not be cached Functions which can act on any file specified in their parameters typically find their place either in the repository where reference to repository specific configuration is needed
Definition: README:3
$wgDisableAuthManager
Disable AuthManager.
def main
Definition: Makefile.py:302
const CACHE_NONE
Definition: Defines.php:102
$wgDevelopmentWarnings
If set to true MediaWiki will throw notices for some possible error conditions and for deprecated fun...
static __construct()
Definition: phpunit.php:32