Move debug-tests out of MediaWikiPHPUnitCommand
[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
18 public function __construct() {
19 parent::__construct();
20 $this->addOption(
21 'with-phpunitdir',
22 'Directory to include PHPUnit from, for example when using a git '
23 . 'fetchout from upstream. Path will be prepended to PHP `include_path`.',
24 false, # not required
25 true # need arg
26 );
27 $this->addOption(
28 'debug-tests',
29 'Log testing activity to the PHPUnitCommand log channel.',
30 false, # not required
31 false # no arg needed
32 );
33 }
34
35 public function finalSetup() {
36 parent::finalSetup();
37
38 global $wgMainCacheType, $wgMessageCacheType, $wgParserCacheType;
39 global $wgLanguageConverterCacheType, $wgUseDatabaseMessages;
40 global $wgLocaltimezone, $wgLocalisationCacheConf;
41 global $wgDevelopmentWarnings;
42
43 // Inject test autoloader
44 require_once __DIR__ . '/../TestsAutoLoader.php';
45
46 // wfWarn should cause tests to fail
47 $wgDevelopmentWarnings = true;
48
49 $wgMainCacheType = CACHE_NONE;
50 $wgMessageCacheType = CACHE_NONE;
51 $wgParserCacheType = CACHE_NONE;
52 $wgLanguageConverterCacheType = CACHE_NONE;
53
54 $wgUseDatabaseMessages = false; # Set for future resets
55
56 // Assume UTC for testing purposes
57 $wgLocaltimezone = 'UTC';
58
59 $wgLocalisationCacheConf['storeClass'] = 'LCStoreNull';
60
61 // Bug 44192 Do not attempt to send a real e-mail
62 Hooks::clear( 'AlternateUserMailer' );
63 Hooks::register(
64 'AlternateUserMailer',
65 function () {
66 return false;
67 }
68 );
69 // xdebug's default of 100 is too low for MediaWiki
70 ini_set( 'xdebug.max_nesting_level', 1000 );
71 }
72
73 public function execute() {
74 global $IP;
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 array( '--configuration', $IP . '/tests/phpunit/suite.xml' ) );
81 }
82
83 # --with-phpunitdir let us override the default PHPUnit version
84 if ( $this->hasOption( 'with-phpunitdir' ) ) {
85 $phpunitDir = $this->getOption( 'with-phpunitdir' );
86 # Sanity checks
87 if ( !is_dir( $phpunitDir ) ) {
88 $this->error( "--with-phpunitdir should be set to an existing directory", 1 );
89 }
90 if ( !is_readable( $phpunitDir . "/PHPUnit/Runner/Version.php" ) ) {
91 $this->error( "No usable PHPUnit installation in $phpunitDir.\nAborting.\n", 1 );
92 }
93
94 # Now prepends provided PHPUnit directory
95 $this->output( "Will attempt loading PHPUnit from `$phpunitDir`\n" );
96 set_include_path( $phpunitDir . PATH_SEPARATOR . get_include_path() );
97
98 # Cleanup $args array so the option and its value do not
99 # pollute PHPUnit
100 $key = array_search( '--with-phpunitdir', $_SERVER['argv'] );
101 unset( $_SERVER['argv'][$key] ); // the option
102 unset( $_SERVER['argv'][$key + 1] ); // its value
103 $_SERVER['argv'] = array_values( $_SERVER['argv'] );
104 }
105
106 if ( !wfIsWindows() ) {
107 # If we are not running on windows then we can enable phpunit colors
108 # Windows does not come anymore with ANSI.SYS loaded by default
109 # PHPUnit uses the suite.xml parameters to enable/disable colors
110 # which can be then forced to be enabled with --colors.
111 # The below code injects a parameter just like if the user called
112 # Probably fix bug 29226
113 $key = array_search( '--colors', $_SERVER['argv'] );
114 if ( $key === false ) {
115 array_splice( $_SERVER['argv'], 1, 0, '--colors' );
116 }
117 }
118
119 # Makes MediaWiki PHPUnit directory includable so the PHPUnit will
120 # be able to resolve relative files inclusion such as suites/*
121 # PHPUnit uses stream_resolve_include_path() internally
122 # See bug 32022
123 $key = array_search( '--include-path', $_SERVER['argv'] );
124 if ( $key === false ) {
125 array_splice( $_SERVER['argv'], 1, 0,
126 __DIR__
127 . PATH_SEPARATOR
128 . get_include_path()
129 );
130 array_splice( $_SERVER['argv'], 1, 0, '--include-path' );
131 }
132
133 $key = array_search( '--debug-tests', $_SERVER['argv'] );
134 if( $key !== false && array_search( '--printer', $_SERVER['argv'] ) === false ) {
135 unset( $_SERVER['argv'][$key] );
136 array_splice( $_SERVER['argv'], 1, 0, 'MediaWikiPHPUnitTestListener' );
137 array_splice( $_SERVER['argv'], 1, 0, '--printer' );
138 }
139
140 }
141
142 public function getDbType() {
143 return Maintenance::DB_ADMIN;
144 }
145 }
146
147 $maintClass = 'PHPUnitMaintClass';
148 require RUN_MAINTENANCE_IF_MAIN;
149
150 if ( !class_exists( 'PHPUnit_Runner_Version' ) ) {
151 require_once 'PHPUnit/Runner/Version.php';
152 }
153
154 if ( PHPUnit_Runner_Version::id() !== '@package_version@'
155 && version_compare( PHPUnit_Runner_Version::id(), '3.7.0', '<' )
156 ) {
157 die( 'PHPUnit 3.7.0 or later required, you have ' . PHPUnit_Runner_Version::id() . ".\n" );
158 }
159
160 if ( !class_exists( 'PHPUnit_TextUI_Command' ) ) {
161 require_once 'PHPUnit/Autoload.php';
162 }
163
164 // Prevent segfault when we have lots of unit tests (bug 62623)
165 if ( version_compare( PHP_VERSION, '5.4.0', '<' )
166 && version_compare( PHP_VERSION, '5.3.0', '>=' )
167 ) {
168 register_shutdown_function( function () {
169 gc_collect_cycles();
170 gc_disable();
171 } );
172 }
173
174 MediaWikiPHPUnitCommand::main();