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 // Start up MediaWiki in command-line mode
14 require_once dirname( dirname( __DIR__ ) ) . "/maintenance/Maintenance.php";
15 
17  public function __construct() {
18  parent::__construct();
19  $this->setAllowUnregisteredOptions( true );
20  $this->addOption(
21  'debug-tests',
22  'Log testing activity to the PHPUnitCommand log channel (deprecated, always on).',
23  false, # not required
24  false # no arg needed
25  );
26  $this->addOption( 'use-filebackend', 'Use filebackend', false, true );
27  $this->addOption( 'use-bagostuff', 'Use bagostuff', false, true );
28  $this->addOption( 'use-jobqueue', 'Use jobqueue', false, true );
29  $this->addOption( 'use-normal-tables', 'Use normal DB tables.', false, false );
30  $this->addOption(
31  'reuse-db', 'Init DB only if tables are missing and keep after finish.',
32  false,
33  false
34  );
35  }
36 
37  public function finalSetup() {
38  parent::finalSetup();
39 
40  // Inject test autoloader
41  self::requireTestsAutoloader();
42 
44  }
45 
46  public function execute() {
47  // Deregister handler from MWExceptionHandler::installHandle so that PHPUnit's own handler
48  // stays in tact.
49  // Has to in execute() instead of finalSetup(), because finalSetup() runs before
50  // doMaintenance.php includes Setup.php, which calls MWExceptionHandler::installHandle().
51  restore_error_handler();
52 
53  $this->forceFormatServerArgv();
54 
55  if ( !class_exists( 'PHPUnit\\Framework\\TestCase' ) ) {
56  echo "PHPUnit not found. Please install it and other dev dependencies by
57  running `composer install` in MediaWiki root directory.\n";
58  exit( 1 );
59  }
60 
61  fwrite( STDERR, defined( 'HHVM_VERSION' ) ?
62  'Using HHVM ' . HHVM_VERSION . ' (' . PHP_VERSION . ")\n" :
63  'Using PHP ' . PHP_VERSION . "\n" );
64 
65  // Tell PHPUnit to ignore options meant for MediaWiki
66  $ignore = [];
67  foreach ( $this->mParams as $name => $param ) {
68  if ( empty( $param['withArg'] ) ) {
69  $ignore[] = $name;
70  } else {
71  $ignore[] = "$name=";
72  }
73  }
74 
75  // Pass through certain options to MediaWikiTestCase
76  $cliArgs = [];
77  foreach (
78  [
79  'use-filebackend',
80  'use-bagostuff',
81  'use-jobqueue',
82  'use-normal-tables',
83  'reuse-db'
84  ] as $name
85  ) {
86  $cliArgs[$name] = $this->getOption( $name );
87  }
88 
89  $command = new MediaWikiPHPUnitCommand( $ignore, $cliArgs );
90  $command->run( $_SERVER['argv'], true );
91  }
92 
93  public function getDbType() {
94  return Maintenance::DB_ADMIN;
95  }
96 
97  protected function addOption( $name, $description, $required = false,
98  $withArg = false, $shortName = false, $multiOccurrence = false
99  ) {
100  // ignore --quiet which does not really make sense for unit tests
101  if ( $name !== 'quiet' ) {
102  parent::addOption( $name, $description, $required, $withArg, $shortName, $multiOccurrence );
103  }
104  }
105 
110  private function forceFormatServerArgv() {
111  $argv = [];
112  foreach ( $_SERVER['argv'] as $key => $arg ) {
113  if ( $key === 0 ) {
114  $argv[0] = $arg;
115  } elseif ( strstr( $arg, '=' ) ) {
116  foreach ( explode( '=', $arg, 2 ) as $argPart ) {
117  $argv[] = $argPart;
118  }
119  } else {
120  $argv[] = $arg;
121  }
122  }
123  $_SERVER['argv'] = $argv;
124  }
125 
126 }
127 
128 $maintClass = 'PHPUnitMaintClass';
getOption( $name, $default=null)
Get an option, or return the default.
$command
Definition: cdb.php:65
$maintClass
Definition: phpunit.php:128
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: maintenance.txt:39
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
forceFormatServerArgv()
Force the format of elements in $_SERVER[&#39;argv&#39;].
Definition: phpunit.php:110
const DB_ADMIN
Definition: Maintenance.php:86
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
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
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:271
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Definition: phpunit.php:97
static applyInitialConfig()
This should be called before Setup.php, e.g.
Definition: TestSetup.php:11
setAllowUnregisteredOptions( $allow)
Sets whether to allow unregistered options, which are options passed to a script that do not match an...