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 public function finalSetup() {
32 global $wgMainCacheType, $wgMessageCacheType, $wgParserCacheType;
33 global $wgLanguageConverterCacheType, $wgUseDatabaseMessages;
34 global $wgLocaltimezone, $wgLocalisationCacheConf;
35 global $wgDevelopmentWarnings;
37 // Inject test autoloader
38 require_once __DIR__
. '/../TestsAutoLoader.php';
40 // wfWarn should cause tests to fail
41 $wgDevelopmentWarnings = true;
43 $wgMainCacheType = CACHE_NONE
;
44 $wgMessageCacheType = CACHE_NONE
;
45 $wgParserCacheType = CACHE_NONE
;
46 $wgLanguageConverterCacheType = CACHE_NONE
;
48 $wgUseDatabaseMessages = false; # Set for future resets
50 // Assume UTC for testing purposes
51 $wgLocaltimezone = 'UTC';
53 $wgLocalisationCacheConf['storeClass'] = 'LCStoreNull';
55 // Bug 44192 Do not attempt to send a real e-mail
56 Hooks
::clear( 'AlternateUserMailer' );
58 'AlternateUserMailer',
65 public function execute() {
68 # Make sure we have --configuration or PHPUnit might complain
69 if ( !in_array( '--configuration', $_SERVER['argv'] ) ) {
70 //Hack to eliminate the need to use the Makefile (which sucks ATM)
71 array_splice( $_SERVER['argv'], 1, 0,
72 array( '--configuration', $IP . '/tests/phpunit/suite.xml' ) );
75 # --with-phpunitdir let us override the default PHPUnit version
76 if ( $this->hasOption( 'with-phpunitdir' ) ) {
77 $phpunitDir = $this->getOption( 'with-phpunitdir' );
79 if ( !is_dir( $phpunitDir ) ) {
80 $this->error( "--with-phpunitdir should be set to an existing directory", 1 );
82 if ( !is_readable( $phpunitDir . "/PHPUnit/Runner/Version.php" ) ) {
83 $this->error( "No usable PHPUnit installation in $phpunitDir.\nAborting.\n", 1 );
86 # Now prepends provided PHPUnit directory
87 $this->output( "Will attempt loading PHPUnit from `$phpunitDir`\n" );
88 set_include_path( $phpunitDir . PATH_SEPARATOR
. get_include_path() );
90 # Cleanup $args array so the option and its value do not
92 $key = array_search( '--with-phpunitdir', $_SERVER['argv'] );
93 unset( $_SERVER['argv'][$key] ); // the option
94 unset( $_SERVER['argv'][$key +
1] ); // its value
95 $_SERVER['argv'] = array_values( $_SERVER['argv'] );
98 if ( !wfIsWindows() ) {
99 # If we are not running on windows then we can enable phpunit colors
100 # Windows does not come anymore with ANSI.SYS loaded by default
101 # PHPUnit uses the suite.xml parameters to enable/disable colors
102 # which can be then forced to be enabled with --colors.
103 # The below code injects a parameter just like if the user called
104 # Probably fix bug 29226
105 $key = array_search( '--colors', $_SERVER['argv'] );
106 if( $key === false ) {
107 array_splice( $_SERVER['argv'], 1, 0, '--colors' );
111 # Makes MediaWiki PHPUnit directory includable so the PHPUnit will
112 # be able to resolve relative files inclusion such as suites/*
113 # PHPUnit uses stream_resolve_include_path() internally
115 $key = array_search( '--include-path', $_SERVER['argv'] );
116 if( $key === false ) {
117 array_splice( $_SERVER['argv'], 1, 0,
122 array_splice( $_SERVER['argv'], 1, 0, '--include-path' );
126 public function getDbType() {
127 return Maintenance
::DB_ADMIN
;
131 $maintClass = 'PHPUnitMaintClass';
132 require RUN_MAINTENANCE_IF_MAIN
;
134 if ( !class_exists( 'PHPUnit_Runner_Version' ) ) {
135 require_once 'PHPUnit/Runner/Version.php';
138 if ( PHPUnit_Runner_Version
::id() !== '@package_version@'
139 && version_compare( PHPUnit_Runner_Version
::id(), '3.7.0', '<' )
141 die( 'PHPUnit 3.7.0 or later required, you have ' . PHPUnit_Runner_Version
::id() . ".\n" );
144 if ( !class_exists( 'PHPUnit_TextUI_Command' ) ) {
145 require_once 'PHPUnit/Autoload.php';
148 // Prevent segfault when we have lots of unit tests (bug 62623)
149 if ( version_compare( PHP_VERSION
, '5.4.0', '<' )
150 && version_compare( PHP_VERSION
, '5.3.0', '>=' )
152 register_shutdown_function( function() {
158 MediaWikiPHPUnitCommand
::main();