Follow-up Ifa346c8a92: LanguageNameUtils: CONSTRUCTOR_OTPIONS, not constructorOptions
[lhc/web/wiklou.git] / tests / phpunit / phpunit.php
1 #!/usr/bin/env php
2 <?php
3 /**
4 * Bootstrapping for MediaWiki PHPUnit tests
5 *
6 * @file
7 */
8
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
16 class PHPUnitMaintClass extends Maintenance {
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 setup() {
38 parent::setup();
39
40 require_once __DIR__ . '/../common/TestSetup.php';
41 TestSetup::snapshotGlobals();
42 }
43
44 public function finalSetup() {
45 parent::finalSetup();
46
47 // Inject test autoloader
48 self::requireTestsAutoloader();
49
50 TestSetup::applyInitialConfig();
51 }
52
53 public function execute() {
54 // Deregister handler from MWExceptionHandler::installHandle so that PHPUnit's own handler
55 // stays in tact.
56 // Has to in execute() instead of finalSetup(), because finalSetup() runs before
57 // doMaintenance.php includes Setup.php, which calls MWExceptionHandler::installHandle().
58 restore_error_handler();
59
60 $this->forceFormatServerArgv();
61
62 if ( !class_exists( 'PHPUnit\\Framework\\TestCase' ) ) {
63 echo "PHPUnit not found. Please install it and other dev dependencies by
64 running `composer install` in MediaWiki root directory.\n";
65 exit( 1 );
66 }
67
68 fwrite( STDERR, defined( 'HHVM_VERSION' ) ?
69 'Using HHVM ' . HHVM_VERSION . ' (' . PHP_VERSION . ")\n" :
70 'Using PHP ' . PHP_VERSION . "\n" );
71
72 // Tell PHPUnit to ignore options meant for MediaWiki
73 $ignore = [];
74 foreach ( $this->mParams as $name => $param ) {
75 if ( empty( $param['withArg'] ) ) {
76 $ignore[] = $name;
77 } else {
78 $ignore[] = "$name=";
79 }
80 }
81
82 // Pass through certain options to MediaWikiTestCase
83 $cliArgs = [];
84 foreach (
85 [
86 'use-filebackend',
87 'use-bagostuff',
88 'use-jobqueue',
89 'use-normal-tables',
90 'reuse-db'
91 ] as $name
92 ) {
93 $cliArgs[$name] = $this->getOption( $name );
94 }
95
96 $command = new MediaWikiPHPUnitCommand( $ignore, $cliArgs );
97 $command->run( $_SERVER['argv'], true );
98 }
99
100 public function getDbType() {
101 return Maintenance::DB_ADMIN;
102 }
103
104 protected function addOption( $name, $description, $required = false,
105 $withArg = false, $shortName = false, $multiOccurrence = false
106 ) {
107 // ignore --quiet which does not really make sense for unit tests
108 if ( $name !== 'quiet' ) {
109 parent::addOption( $name, $description, $required, $withArg, $shortName, $multiOccurrence );
110 }
111 }
112
113 /**
114 * Force the format of elements in $_SERVER['argv']
115 * - Split args such as "wiki=enwiki" into two separate arg elements "wiki" and "enwiki"
116 */
117 private function forceFormatServerArgv() {
118 $argv = [];
119 foreach ( $_SERVER['argv'] as $key => $arg ) {
120 if ( $key === 0 ) {
121 $argv[0] = $arg;
122 } elseif ( strstr( $arg, '=' ) ) {
123 foreach ( explode( '=', $arg, 2 ) as $argPart ) {
124 $argv[] = $argPart;
125 }
126 } else {
127 $argv[] = $arg;
128 }
129 }
130 $_SERVER['argv'] = $argv;
131 }
132
133 }
134
135 $maintClass = 'PHPUnitMaintClass';
136 require RUN_MAINTENANCE_IF_MAIN;