4 * Bootstrapping for MediaWiki PHPUnit tests
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 );
13 // Start up MediaWiki in command-line mode
14 require_once dirname( dirname( __DIR__
) ) . "/maintenance/Maintenance.php";
16 class PHPUnitMaintClass
extends Maintenance
{
18 public function __construct() {
19 parent
::__construct();
22 'Directory to include PHPUnit from, for example when using a git '
23 . 'fetchout from upstream. Path will be prepended to PHP `include_path`.',
29 'Log testing activity to the PHPUnitCommand log channel.',
35 public function finalSetup() {
38 global $wgMainCacheType, $wgMessageCacheType, $wgParserCacheType;
39 global $wgLanguageConverterCacheType, $wgUseDatabaseMessages;
40 global $wgLocaltimezone, $wgLocalisationCacheConf;
41 global $wgDevelopmentWarnings;
43 // Inject test autoloader
44 require_once __DIR__
. '/../TestsAutoLoader.php';
46 // wfWarn should cause tests to fail
47 $wgDevelopmentWarnings = true;
49 $wgMainCacheType = CACHE_NONE
;
50 $wgMessageCacheType = CACHE_NONE
;
51 $wgParserCacheType = CACHE_NONE
;
52 $wgLanguageConverterCacheType = CACHE_NONE
;
54 $wgUseDatabaseMessages = false; # Set for future resets
56 // Assume UTC for testing purposes
57 $wgLocaltimezone = 'UTC';
59 $wgLocalisationCacheConf['storeClass'] = 'LCStoreNull';
61 // Bug 44192 Do not attempt to send a real e-mail
62 Hooks
::clear( 'AlternateUserMailer' );
64 'AlternateUserMailer',
69 // xdebug's default of 100 is too low for MediaWiki
70 ini_set( 'xdebug.max_nesting_level', 1000 );
73 public function execute() {
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' ) );
83 # --with-phpunitdir let us override the default PHPUnit version
84 if ( $this->hasOption( 'with-phpunitdir' ) ) {
85 $phpunitDir = $this->getOption( 'with-phpunitdir' );
87 if ( !is_dir( $phpunitDir ) ) {
88 $this->error( "--with-phpunitdir should be set to an existing directory", 1 );
90 if ( !is_readable( $phpunitDir . "/PHPUnit/Runner/Version.php" ) ) {
91 $this->error( "No usable PHPUnit installation in $phpunitDir.\nAborting.\n", 1 );
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() );
98 # Cleanup $args array so the option and its value do not
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'] );
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' );
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
123 $key = array_search( '--include-path', $_SERVER['argv'] );
124 if ( $key === false ) {
125 array_splice( $_SERVER['argv'], 1, 0,
130 array_splice( $_SERVER['argv'], 1, 0, '--include-path' );
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' );
142 public function getDbType() {
143 return Maintenance
::DB_ADMIN
;
147 $maintClass = 'PHPUnitMaintClass';
148 require RUN_MAINTENANCE_IF_MAIN
;
150 if ( !class_exists( 'PHPUnit_Runner_Version' ) ) {
151 require_once 'PHPUnit/Runner/Version.php';
154 if ( PHPUnit_Runner_Version
::id() !== '@package_version@'
155 && version_compare( PHPUnit_Runner_Version
::id(), '3.7.0', '<' )
157 die( 'PHPUnit 3.7.0 or later required, you have ' . PHPUnit_Runner_Version
::id() . ".\n" );
160 if ( !class_exists( 'PHPUnit_TextUI_Command' ) ) {
161 require_once 'PHPUnit/Autoload.php';
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', '>=' )
168 register_shutdown_function( function () {
174 MediaWikiPHPUnitCommand
::main();