MediaWiki  REL1_23
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 through this entry point or not
00010 define( 'MW_PHPUNIT_TEST', true );
00011 
00012 // Start up MediaWiki in command-line mode
00013 require_once dirname( dirname( __DIR__ ) ) . "/maintenance/Maintenance.php";
00014 
00015 class PHPUnitMaintClass extends Maintenance {
00016 
00017     public function __construct() {
00018         parent::__construct();
00019         $this->addOption( 'with-phpunitdir',
00020             'Directory to include PHPUnit from, for example when using a git fetchout from upstream. Path will be prepended to PHP `include_path`.',
00021             false, # not required
00022             true # need arg
00023         );
00024     }
00025 
00026     public function finalSetup() {
00027         parent::finalSetup();
00028 
00029         global $wgMainCacheType, $wgMessageCacheType, $wgParserCacheType;
00030         global $wgLanguageConverterCacheType, $wgUseDatabaseMessages;
00031         global $wgLocaltimezone, $wgLocalisationCacheConf;
00032         global $wgDevelopmentWarnings;
00033 
00034         // Inject test autoloader
00035         require_once __DIR__ . '/../TestsAutoLoader.php';
00036 
00037         // wfWarn should cause tests to fail
00038         $wgDevelopmentWarnings = true;
00039 
00040         $wgMainCacheType = CACHE_NONE;
00041         $wgMessageCacheType = CACHE_NONE;
00042         $wgParserCacheType = CACHE_NONE;
00043         $wgLanguageConverterCacheType = CACHE_NONE;
00044 
00045         $wgUseDatabaseMessages = false; # Set for future resets
00046 
00047         // Assume UTC for testing purposes
00048         $wgLocaltimezone = 'UTC';
00049 
00050         $wgLocalisationCacheConf['storeClass'] = 'LCStoreNull';
00051 
00052         // Bug 44192 Do not attempt to send a real e-mail
00053         Hooks::clear( 'AlternateUserMailer' );
00054         Hooks::register(
00055             'AlternateUserMailer',
00056             function () {
00057                 return false;
00058             }
00059         );
00060     }
00061 
00062     public function execute() {
00063         global $IP;
00064 
00065         # Make sure we have --configuration or PHPUnit might complain
00066         if ( !in_array( '--configuration', $_SERVER['argv'] ) ) {
00067             //Hack to eliminate the need to use the Makefile (which sucks ATM)
00068             array_splice( $_SERVER['argv'], 1, 0,
00069                 array( '--configuration', $IP . '/tests/phpunit/suite.xml' ) );
00070         }
00071 
00072         # --with-phpunitdir let us override the default PHPUnit version
00073         if ( $this->hasOption( 'with-phpunitdir' ) ) {
00074             $phpunitDir = $this->getOption( 'with-phpunitdir' );
00075             # Sanity checks
00076             if ( !is_dir( $phpunitDir ) ) {
00077                 $this->error( "--with-phpunitdir should be set to an existing directory", 1 );
00078             }
00079             if ( !is_readable( $phpunitDir . "/PHPUnit/Runner/Version.php" ) ) {
00080                 $this->error( "No usable PHPUnit installation in $phpunitDir.\nAborting.\n", 1 );
00081             }
00082 
00083             # Now prepends provided PHPUnit directory
00084             $this->output( "Will attempt loading PHPUnit from `$phpunitDir`\n" );
00085             set_include_path( $phpunitDir . PATH_SEPARATOR . get_include_path() );
00086 
00087             # Cleanup $args array so the option and its value do not
00088             # pollute PHPUnit
00089             $key = array_search( '--with-phpunitdir', $_SERVER['argv'] );
00090             unset( $_SERVER['argv'][$key] ); // the option
00091             unset( $_SERVER['argv'][$key + 1] ); // its value
00092             $_SERVER['argv'] = array_values( $_SERVER['argv'] );
00093         }
00094     }
00095 
00096     public function getDbType() {
00097         return Maintenance::DB_ADMIN;
00098     }
00099 }
00100 
00101 $maintClass = 'PHPUnitMaintClass';
00102 require RUN_MAINTENANCE_IF_MAIN;
00103 
00104 if ( !class_exists( 'PHPUnit_Runner_Version' ) ) {
00105     require_once 'PHPUnit/Runner/Version.php';
00106 }
00107 
00108 if ( PHPUnit_Runner_Version::id() !== '@package_version@'
00109     && version_compare( PHPUnit_Runner_Version::id(), '3.7.0', '<' )
00110 ) {
00111     die( 'PHPUnit 3.7.0 or later required, you have ' . PHPUnit_Runner_Version::id() . ".\n" );
00112 }
00113 
00114 if ( !class_exists( 'PHPUnit_TextUI_Command' ) ) {
00115     require_once 'PHPUnit/Autoload.php';
00116 }
00117 
00118 // Prevent segfault when we have lots of unit tests (bug 62623)
00119 if ( version_compare( PHP_VERSION, '5.4.0', '<' )
00120     && version_compare( PHP_VERSION, '5.3.0', '>=' )
00121 ) {
00122     register_shutdown_function( function() {
00123         gc_collect_cycles();
00124         gc_disable();
00125     } );
00126 }
00127 
00128 MediaWikiPHPUnitCommand::main();