MediaWiki  REL1_24
phpunit.php
Go to the documentation of this file.
00001 #!/usr/bin/env php
00002 <?php
00009 // Set a flag which can be used to detect when other scripts have been entered
00010 // through this entry point or not.
00011 define( 'MW_PHPUNIT_TEST', true );
00012 
00013 // Start up MediaWiki in command-line mode
00014 require_once dirname( dirname( __DIR__ ) ) . "/maintenance/Maintenance.php";
00015 
00016 class PHPUnitMaintClass extends Maintenance {
00017 
00018     public static $additionalOptions = array(
00019         'regex' => false,
00020         'file' => false,
00021         'use-filebackend' => false,
00022         'use-bagostuff' => false,
00023         'use-jobqueue' => false,
00024         'keep-uploads' => false,
00025         'use-normal-tables' => false,
00026         'reuse-db' => false,
00027         'wiki' => false,
00028     );
00029 
00030     public function __construct() {
00031         parent::__construct();
00032         $this->addOption(
00033             'with-phpunitdir',
00034             'Directory to include PHPUnit from, for example when using a git '
00035                 . 'fetchout from upstream. Path will be prepended to PHP `include_path`.',
00036             false, # not required
00037             true # need arg
00038         );
00039         $this->addOption(
00040             'debug-tests',
00041             'Log testing activity to the PHPUnitCommand log channel.',
00042             false, # not required
00043             false # no arg needed
00044         );
00045         $this->addOption( 'regex', 'Only run parser tests that match the given regex.', false, true );
00046         $this->addOption( 'file', 'File describing parser tests.', false, true );
00047         $this->addOption( 'use-filebackend', 'Use filebackend', false, true );
00048         $this->addOption( 'use-bagostuff', 'Use bagostuff', false, true );
00049         $this->addOption( 'use-jobqueue', 'Use jobqueue', false, true );
00050         $this->addOption( 'keep-uploads', 'Re-use the same upload directory for each test, don\'t delete it.', false, false );
00051         $this->addOption( 'use-normal-tables', 'Use normal DB tables.', false, false );
00052         $this->addOption( 'reuse-db', 'Init DB only if tables are missing and keep after finish.', false, false );
00053     }
00054 
00055     public function finalSetup() {
00056         parent::finalSetup();
00057 
00058         global $wgMainCacheType, $wgMessageCacheType, $wgParserCacheType;
00059         global $wgLanguageConverterCacheType, $wgUseDatabaseMessages;
00060         global $wgLocaltimezone, $wgLocalisationCacheConf;
00061         global $wgDevelopmentWarnings;
00062 
00063         // Inject test autoloader
00064         require_once __DIR__ . '/../TestsAutoLoader.php';
00065 
00066         // wfWarn should cause tests to fail
00067         $wgDevelopmentWarnings = true;
00068 
00069         $wgMainCacheType = CACHE_NONE;
00070         $wgMessageCacheType = CACHE_NONE;
00071         $wgParserCacheType = CACHE_NONE;
00072         $wgLanguageConverterCacheType = CACHE_NONE;
00073 
00074         $wgUseDatabaseMessages = false; # Set for future resets
00075 
00076         // Assume UTC for testing purposes
00077         $wgLocaltimezone = 'UTC';
00078 
00079         $wgLocalisationCacheConf['storeClass'] = 'LCStoreNull';
00080 
00081         // Bug 44192 Do not attempt to send a real e-mail
00082         Hooks::clear( 'AlternateUserMailer' );
00083         Hooks::register(
00084             'AlternateUserMailer',
00085             function () {
00086                 return false;
00087             }
00088         );
00089         // xdebug's default of 100 is too low for MediaWiki
00090         ini_set( 'xdebug.max_nesting_level', 1000 );
00091     }
00092 
00093     public function execute() {
00094         global $IP;
00095 
00096         $this->forceFormatServerArgv();
00097 
00098         # Make sure we have --configuration or PHPUnit might complain
00099         if ( !in_array( '--configuration', $_SERVER['argv'] ) ) {
00100             //Hack to eliminate the need to use the Makefile (which sucks ATM)
00101             array_splice( $_SERVER['argv'], 1, 0,
00102                 array( '--configuration', $IP . '/tests/phpunit/suite.xml' ) );
00103         }
00104 
00105         # --with-phpunitdir let us override the default PHPUnit version
00106         # Can use with either or phpunit.phar in the directory or the
00107         # full PHPUnit code base.
00108         if ( $this->hasOption( 'with-phpunitdir' ) ) {
00109             $phpunitDir = $this->getOption( 'with-phpunitdir' );
00110 
00111             # prepends provided PHPUnit directory or phar
00112             $this->output( "Will attempt loading PHPUnit from `$phpunitDir`\n" );
00113             set_include_path( $phpunitDir . PATH_SEPARATOR . get_include_path() );
00114 
00115             # Cleanup $args array so the option and its value do not
00116             # pollute PHPUnit
00117             $key = array_search( '--with-phpunitdir', $_SERVER['argv'] );
00118             unset( $_SERVER['argv'][$key] ); // the option
00119             unset( $_SERVER['argv'][$key + 1] ); // its value
00120             $_SERVER['argv'] = array_values( $_SERVER['argv'] );
00121         }
00122 
00123         if ( !wfIsWindows() ) {
00124             # If we are not running on windows then we can enable phpunit colors
00125             # Windows does not come anymore with ANSI.SYS loaded by default
00126             # PHPUnit uses the suite.xml parameters to enable/disable colors
00127             # which can be then forced to be enabled with --colors.
00128             # The below code injects a parameter just like if the user called
00129             # Probably fix bug 29226
00130             $key = array_search( '--colors', $_SERVER['argv'] );
00131             if ( $key === false ) {
00132                 array_splice( $_SERVER['argv'], 1, 0, '--colors' );
00133             }
00134         }
00135 
00136         # Makes MediaWiki PHPUnit directory includable so the PHPUnit will
00137         # be able to resolve relative files inclusion such as suites/*
00138         # PHPUnit uses stream_resolve_include_path() internally
00139         # See bug 32022
00140         $key = array_search( '--include-path', $_SERVER['argv'] );
00141         if ( $key === false ) {
00142             array_splice( $_SERVER['argv'], 1, 0,
00143                 __DIR__
00144                 . PATH_SEPARATOR
00145                 . get_include_path()
00146             );
00147             array_splice( $_SERVER['argv'], 1, 0, '--include-path' );
00148         }
00149 
00150         $key = array_search( '--debug-tests', $_SERVER['argv'] );
00151         if ( $key !== false && array_search( '--printer', $_SERVER['argv'] ) === false ) {
00152             unset( $_SERVER['argv'][$key] );
00153             array_splice( $_SERVER['argv'], 1, 0, 'MediaWikiPHPUnitTestListener' );
00154             array_splice( $_SERVER['argv'], 1, 0, '--printer' );
00155         }
00156 
00157         foreach ( self::$additionalOptions as $option => $default ) {
00158             $key = array_search( '--' . $option, $_SERVER['argv'] );
00159             if ( $key !== false ) {
00160                 unset( $_SERVER['argv'][$key] );
00161                 if ( $this->mParams[$option]['withArg'] ) {
00162                     self::$additionalOptions[$option] = $_SERVER['argv'][$key + 1];
00163                     unset( $_SERVER['argv'][$key + 1] );
00164                 } else {
00165                     self::$additionalOptions[$option] = true;
00166                 }
00167             }
00168         }
00169 
00170     }
00171 
00172     public function getDbType() {
00173         return Maintenance::DB_ADMIN;
00174     }
00175 
00180     private function forceFormatServerArgv() {
00181         $argv = array();
00182         foreach ( $_SERVER['argv'] as $key => $arg ) {
00183             if ( $key === 0 ) {
00184                 $argv[0] = $arg;
00185             } elseif ( strstr( $arg, '=' ) ) {
00186                 foreach ( explode( '=', $arg, 2 ) as $argPart ) {
00187                     $argv[] = $argPart;
00188                 }
00189             } else {
00190                 $argv[] = $arg;
00191             }
00192         }
00193         $_SERVER['argv'] = $argv;
00194     }
00195 
00196 }
00197 
00198 $maintClass = 'PHPUnitMaintClass';
00199 require RUN_MAINTENANCE_IF_MAIN;
00200 
00201 // Prevent segfault when we have lots of unit tests (bug 62623)
00202 if ( version_compare( PHP_VERSION, '5.4.0', '<' ) ) {
00203     register_shutdown_function( function () {
00204         gc_collect_cycles();
00205         gc_disable();
00206     } );
00207 }
00208 
00209 
00210 $ok = false;
00211 
00212 foreach ( array(
00213     stream_resolve_include_path( 'phpunit.phar' ),
00214     'PHPUnit/Runner/Version.php',
00215     'PHPUnit/Autoload.php'
00216 ) as $includePath ) {
00217     @include_once $includePath;
00218     if ( class_exists( 'PHPUnit_TextUI_Command' ) ) {
00219         $ok = true;
00220         break;
00221     }
00222 }
00223 
00224 if ( !$ok ) {
00225     die( "Couldn't find a usable PHPUnit.\n" );
00226 }
00227 
00228 $puVersion = PHPUnit_Runner_Version::id();
00229 if ( $puVersion !== '@package_version@' && version_compare( $puVersion, '3.7.0', '<' ) ) {
00230     die( "PHPUnit 3.7.0 or later required; you have {$puVersion}.\n" );
00231 }
00232 
00233 PHPUnit_TextUI_Command::main();