CommentStore: Hard-deprecate newKey()
[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 finalSetup() {
38 parent::finalSetup();
39
40 // Inject test autoloader
41 self::requireTestsAutoloader();
42
43 TestSetup::applyInitialConfig();
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 echo 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
106 /**
107 * Force the format of elements in $_SERVER['argv']
108 * - Split args such as "wiki=enwiki" into two separate arg elements "wiki" and "enwiki"
109 */
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';
129 require RUN_MAINTENANCE_IF_MAIN;