Merge "Fix missing import in mediawiki.ui"
[lhc/web/wiklou.git] / tests / phpunit / phpunit.php
1 #!/usr/bin/env php
2 <?php
3 /**
4 * Bootstrapping for MediaWiki PHPUnit tests
5 *
6 * @file
7 */
8
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 );
12
13 // Start up MediaWiki in command-line mode
14 require_once dirname( dirname( __DIR__ ) ) . "/maintenance/Maintenance.php";
15
16 class PHPUnitMaintClass extends Maintenance {
17
18 public function __construct() {
19 parent::__construct();
20 $this->addOption(
21 'with-phpunitdir',
22 'Directory to include PHPUnit from, for example when using a git '
23 . 'fetchout from upstream. Path will be prepended to PHP `include_path`.',
24 false, # not required
25 true # need arg
26 );
27 }
28
29 public function finalSetup() {
30 parent::finalSetup();
31
32 global $wgMainCacheType, $wgMessageCacheType, $wgParserCacheType;
33 global $wgLanguageConverterCacheType, $wgUseDatabaseMessages;
34 global $wgLocaltimezone, $wgLocalisationCacheConf;
35 global $wgDevelopmentWarnings;
36
37 // Inject test autoloader
38 require_once __DIR__ . '/../TestsAutoLoader.php';
39
40 // wfWarn should cause tests to fail
41 $wgDevelopmentWarnings = true;
42
43 $wgMainCacheType = CACHE_NONE;
44 $wgMessageCacheType = CACHE_NONE;
45 $wgParserCacheType = CACHE_NONE;
46 $wgLanguageConverterCacheType = CACHE_NONE;
47
48 $wgUseDatabaseMessages = false; # Set for future resets
49
50 // Assume UTC for testing purposes
51 $wgLocaltimezone = 'UTC';
52
53 $wgLocalisationCacheConf['storeClass'] = 'LCStoreNull';
54
55 // Bug 44192 Do not attempt to send a real e-mail
56 Hooks::clear( 'AlternateUserMailer' );
57 Hooks::register(
58 'AlternateUserMailer',
59 function () {
60 return false;
61 }
62 );
63 }
64
65 public function execute() {
66 global $IP;
67
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' ) );
73 }
74
75 # --with-phpunitdir let us override the default PHPUnit version
76 if ( $this->hasOption( 'with-phpunitdir' ) ) {
77 $phpunitDir = $this->getOption( 'with-phpunitdir' );
78 # Sanity checks
79 if ( !is_dir( $phpunitDir ) ) {
80 $this->error( "--with-phpunitdir should be set to an existing directory", 1 );
81 }
82 if ( !is_readable( $phpunitDir . "/PHPUnit/Runner/Version.php" ) ) {
83 $this->error( "No usable PHPUnit installation in $phpunitDir.\nAborting.\n", 1 );
84 }
85
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() );
89
90 # Cleanup $args array so the option and its value do not
91 # pollute PHPUnit
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'] );
96 }
97
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' );
108 }
109 }
110
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
114 # See bug 32022
115 $key = array_search( '--include-path', $_SERVER['argv'] );
116 if( $key === false ) {
117 array_splice( $_SERVER['argv'], 1, 0,
118 __DIR__
119 . PATH_SEPARATOR
120 . get_include_path()
121 );
122 array_splice( $_SERVER['argv'], 1, 0, '--include-path' );
123 }
124 }
125
126 public function getDbType() {
127 return Maintenance::DB_ADMIN;
128 }
129 }
130
131 $maintClass = 'PHPUnitMaintClass';
132 require RUN_MAINTENANCE_IF_MAIN;
133
134 if ( !class_exists( 'PHPUnit_Runner_Version' ) ) {
135 require_once 'PHPUnit/Runner/Version.php';
136 }
137
138 if ( PHPUnit_Runner_Version::id() !== '@package_version@'
139 && version_compare( PHPUnit_Runner_Version::id(), '3.7.0', '<' )
140 ) {
141 die( 'PHPUnit 3.7.0 or later required, you have ' . PHPUnit_Runner_Version::id() . ".\n" );
142 }
143
144 if ( !class_exists( 'PHPUnit_TextUI_Command' ) ) {
145 require_once 'PHPUnit/Autoload.php';
146 }
147
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', '>=' )
151 ) {
152 register_shutdown_function( function() {
153 gc_collect_cycles();
154 gc_disable();
155 } );
156 }
157
158 MediaWikiPHPUnitCommand::main();