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