MediaWiki  1.28.0
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 
18  public static $additionalOptions = [
19  'file' => false,
20  'use-filebackend' => false,
21  'use-bagostuff' => false,
22  'use-jobqueue' => false,
23  'use-normal-tables' => false,
24  'reuse-db' => false,
25  'wiki' => false,
26  'profiler' => false,
27  ];
28 
29  public function __construct() {
30  parent::__construct();
31  $this->addOption(
32  'with-phpunitclass',
33  'Class name of the PHPUnit entry point to use',
34  false,
35  true
36  );
37  $this->addOption(
38  'debug-tests',
39  'Log testing activity to the PHPUnitCommand log channel.',
40  false, # not required
41  false # no arg needed
42  );
43  $this->addOption( 'file', 'File describing parser tests.', false, true );
44  $this->addOption( 'use-filebackend', 'Use filebackend', false, true );
45  $this->addOption( 'use-bagostuff', 'Use bagostuff', false, true );
46  $this->addOption( 'use-jobqueue', 'Use jobqueue', false, true );
47  $this->addOption( 'use-normal-tables', 'Use normal DB tables.', false, false );
48  $this->addOption(
49  'reuse-db', 'Init DB only if tables are missing and keep after finish.',
50  false,
51  false
52  );
53  }
54 
55  public function finalSetup() {
56  parent::finalSetup();
57 
58  // Inject test autoloader
59  self::requireTestsAutoloader();
60 
62  }
63 
64  public function execute() {
65  global $IP;
66 
67  // Deregister handler from MWExceptionHandler::installHandle so that PHPUnit's own handler
68  // stays in tact.
69  // Has to in execute() instead of finalSetup(), because finalSetup() runs before
70  // doMaintenance.php includes Setup.php, which calls MWExceptionHandler::installHandle().
71  restore_error_handler();
72 
73  $this->forceFormatServerArgv();
74 
75  # Make sure we have --configuration or PHPUnit might complain
76  if ( !in_array( '--configuration', $_SERVER['argv'] ) ) {
77  // Hack to eliminate the need to use the Makefile (which sucks ATM)
78  array_splice( $_SERVER['argv'], 1, 0,
79  [ '--configuration', $IP . '/tests/phpunit/suite.xml' ] );
80  }
81 
82  $phpUnitClass = 'PHPUnit_TextUI_Command';
83 
84  if ( $this->hasOption( 'with-phpunitclass' ) ) {
85  $phpUnitClass = $this->getOption( 'with-phpunitclass' );
86 
87  # Cleanup $args array so the option and its value do not
88  # pollute PHPUnit
89  $key = array_search( '--with-phpunitclass', $_SERVER['argv'] );
90  unset( $_SERVER['argv'][$key] ); // the option
91  unset( $_SERVER['argv'][$key + 1] ); // its value
92  $_SERVER['argv'] = array_values( $_SERVER['argv'] );
93  }
94 
95  $key = array_search( '--debug-tests', $_SERVER['argv'] );
96  if ( $key !== false && array_search( '--printer', $_SERVER['argv'] ) === false ) {
97  unset( $_SERVER['argv'][$key] );
98  array_splice( $_SERVER['argv'], 1, 0, 'MediaWikiPHPUnitTestListener' );
99  array_splice( $_SERVER['argv'], 1, 0, '--printer' );
100  }
101 
102  foreach ( self::$additionalOptions as $option => $default ) {
103  $key = array_search( '--' . $option, $_SERVER['argv'] );
104  if ( $key !== false ) {
105  unset( $_SERVER['argv'][$key] );
106  if ( $this->mParams[$option]['withArg'] ) {
107  self::$additionalOptions[$option] = $_SERVER['argv'][$key + 1];
108  unset( $_SERVER['argv'][$key + 1] );
109  } else {
110  self::$additionalOptions[$option] = true;
111  }
112  }
113  }
114 
115  if ( !class_exists( 'PHPUnit_Framework_TestCase' ) ) {
116  echo "PHPUnit not found. Please install it and other dev dependencies by
117  running `composer install` in MediaWiki root directory.\n";
118  exit( 1 );
119  }
120  if ( !class_exists( $phpUnitClass ) ) {
121  echo "PHPUnit entry point '" . $phpUnitClass . "' not found. Please make sure you installed
122  the containing component and check the spelling of the class name.\n";
123  exit( 1 );
124  }
125 
126  echo defined( 'HHVM_VERSION' ) ?
127  'Using HHVM ' . HHVM_VERSION . ' (' . PHP_VERSION . ")\n" :
128  'Using PHP ' . PHP_VERSION . "\n";
129 
130  // Prepare global services for unit tests.
132 
134  }
135 
136  public function getDbType() {
137  return Maintenance::DB_ADMIN;
138  }
139 
144  private function forceFormatServerArgv() {
145  $argv = [];
146  foreach ( $_SERVER['argv'] as $key => $arg ) {
147  if ( $key === 0 ) {
148  $argv[0] = $arg;
149  } elseif ( strstr( $arg, '=' ) ) {
150  foreach ( explode( '=', $arg, 2 ) as $argPart ) {
151  $argv[] = $argPart;
152  }
153  } else {
154  $argv[] = $arg;
155  }
156  }
157  $_SERVER['argv'] = $argv;
158  }
159 
160 }
161 
162 $maintClass = 'PHPUnitMaintClass';
static $additionalOptions
Definition: phpunit.php:18
$IP
Definition: WebStart.php:58
$maintClass
Definition: phpunit.php:162
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: maintenance.txt:39
hasOption($name)
Checks to see if a particular param exists.
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
forceFormatServerArgv()
Force the format of elements in $_SERVER['argv'].
Definition: phpunit.php:144
addOption($name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
static prepareServices(Config $bootstrapConfig)
Prepare service configuration for unit testing.
const DB_ADMIN
Definition: Maintenance.php:59
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.
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content as context as context the output can only depend on parameters provided to this hook not on global state indicating whether full HTML should be generated If generation of HTML may be but other information should still be present in the ParserOutput object to manipulate or replace but no entry for that model exists in $wgContentHandlers if desired whether it is OK to use $contentModel on $title Handler functions that modify $ok should generally return false to prevent further hooks from further modifying $ok inclusive false for true for descending in case the handler function wants to provide a converted Content object Note that $result getContentModel() must return $toModel. 'CustomEditor'$rcid is used in generating this variable which contains information about the new such as the revision s whether the revision was marked as a minor edit or not
Definition: hooks.txt:1156
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
this class mediates it Skin Encapsulates a look and feel for the wiki All of the functions that render HTML and make choices about how to render it are here and are called from various other places when needed(most notably, OutputPage::addWikiText()).The StandardSkin object is a complete implementation
def main
Definition: Makefile.py:302
static applyInitialConfig()
This should be called before Setup.php, e.g.
Definition: TestSetup.php:13
static __construct()
Definition: phpunit.php:29