MediaWiki REL1_31
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.
11define( 'MW_PHPUNIT_TEST', true );
12
13// Start up MediaWiki in command-line mode
14require_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 'mwdebug' => false,
25 'reuse-db' => false,
26 'wiki' => false,
27 'profiler' => false,
28 ];
29
30 public function __construct() {
31 parent::__construct();
32 $this->addOption(
33 'with-phpunitclass',
34 'Class name of the PHPUnit entry point to use',
35 false,
36 true
37 );
38 $this->addOption(
39 'debug-tests',
40 'Log testing activity to the PHPUnitCommand log channel.',
41 false, # not required
42 false # no arg needed
43 );
44 $this->addOption( 'file', 'File describing parser tests.', false, true );
45 $this->addOption( 'use-filebackend', 'Use filebackend', false, true );
46 $this->addOption( 'use-bagostuff', 'Use bagostuff', false, true );
47 $this->addOption( 'use-jobqueue', 'Use jobqueue', false, true );
48 $this->addOption( 'use-normal-tables', 'Use normal DB tables.', false, false );
49 $this->addOption(
50 'reuse-db', 'Init DB only if tables are missing and keep after finish.',
51 false,
52 false
53 );
54 }
55
56 public function finalSetup() {
57 parent::finalSetup();
58
59 // Inject test autoloader
61
63 }
64
65 public function execute() {
66 global $IP;
67
68 // Deregister handler from MWExceptionHandler::installHandle so that PHPUnit's own handler
69 // stays in tact.
70 // Has to in execute() instead of finalSetup(), because finalSetup() runs before
71 // doMaintenance.php includes Setup.php, which calls MWExceptionHandler::installHandle().
72 restore_error_handler();
73
74 $this->forceFormatServerArgv();
75
76 # Make sure we have --configuration or PHPUnit might complain
77 if ( !in_array( '--configuration', $_SERVER['argv'] ) ) {
78 // Hack to eliminate the need to use the Makefile (which sucks ATM)
79 array_splice( $_SERVER['argv'], 1, 0,
80 [ '--configuration', $IP . '/tests/phpunit/suite.xml' ] );
81 }
82
83 $phpUnitClass = PHPUnit_TextUI_Command::class;
84
85 if ( $this->hasOption( 'with-phpunitclass' ) ) {
86 $phpUnitClass = $this->getOption( 'with-phpunitclass' );
87
88 # Cleanup $args array so the option and its value do not
89 # pollute PHPUnit
90 $key = array_search( '--with-phpunitclass', $_SERVER['argv'] );
91 unset( $_SERVER['argv'][$key] ); // the option
92 unset( $_SERVER['argv'][$key + 1] ); // its value
93 $_SERVER['argv'] = array_values( $_SERVER['argv'] );
94 }
95
96 $key = array_search( '--debug-tests', $_SERVER['argv'] );
97 if ( $key !== false && array_search( '--printer', $_SERVER['argv'] ) === false ) {
98 unset( $_SERVER['argv'][$key] );
99 array_splice( $_SERVER['argv'], 1, 0, 'MediaWikiPHPUnitTestListener' );
100 array_splice( $_SERVER['argv'], 1, 0, '--printer' );
101 }
102
103 foreach ( self::$additionalOptions as $option => $default ) {
104 $key = array_search( '--' . $option, $_SERVER['argv'] );
105 if ( $key !== false ) {
106 unset( $_SERVER['argv'][$key] );
107 if ( $this->mParams[$option]['withArg'] ) {
108 self::$additionalOptions[$option] = $_SERVER['argv'][$key + 1];
109 unset( $_SERVER['argv'][$key + 1] );
110 } else {
111 self::$additionalOptions[$option] = true;
112 }
113 }
114 }
115
116 if ( !class_exists( 'PHPUnit\\Framework\\TestCase' ) ) {
117 echo "PHPUnit not found. Please install it and other dev dependencies by
118 running `composer install` in MediaWiki root directory.\n";
119 exit( 1 );
120 }
121 if ( !class_exists( $phpUnitClass ) ) {
122 echo "PHPUnit entry point '" . $phpUnitClass . "' not found. Please make sure you installed
123 the containing component and check the spelling of the class name.\n";
124 exit( 1 );
125 }
126
127 // Start an output buffer to avoid headers being sent by constructors,
128 // data providers, etc. (T206476)
129 ob_start();
130
131 fwrite( STDERR, defined( 'HHVM_VERSION' ) ?
132 'Using HHVM ' . HHVM_VERSION . ' (' . PHP_VERSION . ")\n" :
133 'Using PHP ' . PHP_VERSION . "\n" );
134
135 // Prepare global services for unit tests.
136 MediaWikiTestCase::prepareServices( new GlobalVarConfig() );
137
138 $phpUnitClass::main();
139 }
140
141 public function getDbType() {
143 }
144
145 protected function addOption( $name, $description, $required = false,
146 $withArg = false, $shortName = false, $multiOccurrence = false
147 ) {
148 // ignore --quiet which does not really make sense for unit tests
149 if ( $name !== 'quiet' ) {
150 parent::addOption( $name, $description, $required, $withArg, $shortName, $multiOccurrence );
151 }
152 }
153
158 private function forceFormatServerArgv() {
159 $argv = [];
160 foreach ( $_SERVER['argv'] as $key => $arg ) {
161 if ( $key === 0 ) {
162 $argv[0] = $arg;
163 } elseif ( strstr( $arg, '=' ) ) {
164 foreach ( explode( '=', $arg, 2 ) as $argPart ) {
165 $argv[] = $argPart;
166 }
167 } else {
168 $argv[] = $arg;
169 }
170 }
171 $_SERVER['argv'] = $argv;
172 }
173
174}
175
176$maintClass = 'PHPUnitMaintClass';
$IP
Definition WebStart.php:52
global $argv
Accesses configuration settings from $GLOBALS.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
static requireTestsAutoloader()
Call this to set up the autoloader to allow classes to be used from the tests directory.
hasOption( $name)
Checks to see if a particular param exists.
getOption( $name, $default=null)
Get an option, or return the default.
getDbType()
Does the script need different DB access? By default, we give Maintenance scripts normal rights to th...
Definition phpunit.php:141
forceFormatServerArgv()
Force the format of elements in $_SERVER['argv'].
Definition phpunit.php:158
execute()
Do the actual work.
Definition phpunit.php:65
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition phpunit.php:145
static $additionalOptions
Definition phpunit.php:18
__construct()
Default constructor.
Definition phpunit.php:30
finalSetup()
Handle some last-minute setup here.
Definition phpunit.php:56
static applyInitialConfig()
This should be called before Setup.php, e.g.
Definition TestSetup.php:11
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
when a variable name is used in a it is silently declared as a new local masking the global
Definition design.txt:95
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
Allows to change the fields on the form that will be generated $name
Definition hooks.txt:302
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:37
require_once RUN_MAINTENANCE_IF_MAIN
$maintClass
Definition phpunit.php:176