Merge "Leave table names with numbers untouched in DatabaseBase::generalizeSQL"
[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 // xdebug's default of 100 is too low for MediaWiki
64 ini_set( 'xdebug.max_nesting_level', 1000 );
65 }
66
67 public function execute() {
68 global $IP;
69
70 # Make sure we have --configuration or PHPUnit might complain
71 if ( !in_array( '--configuration', $_SERVER['argv'] ) ) {
72 //Hack to eliminate the need to use the Makefile (which sucks ATM)
73 array_splice( $_SERVER['argv'], 1, 0,
74 array( '--configuration', $IP . '/tests/phpunit/suite.xml' ) );
75 }
76
77 # --with-phpunitdir let us override the default PHPUnit version
78 if ( $this->hasOption( 'with-phpunitdir' ) ) {
79 $phpunitDir = $this->getOption( 'with-phpunitdir' );
80 # Sanity checks
81 if ( !is_dir( $phpunitDir ) ) {
82 $this->error( "--with-phpunitdir should be set to an existing directory", 1 );
83 }
84 if ( !is_readable( $phpunitDir . "/PHPUnit/Runner/Version.php" ) ) {
85 $this->error( "No usable PHPUnit installation in $phpunitDir.\nAborting.\n", 1 );
86 }
87
88 # Now prepends provided PHPUnit directory
89 $this->output( "Will attempt loading PHPUnit from `$phpunitDir`\n" );
90 set_include_path( $phpunitDir . PATH_SEPARATOR . get_include_path() );
91
92 # Cleanup $args array so the option and its value do not
93 # pollute PHPUnit
94 $key = array_search( '--with-phpunitdir', $_SERVER['argv'] );
95 unset( $_SERVER['argv'][$key] ); // the option
96 unset( $_SERVER['argv'][$key + 1] ); // its value
97 $_SERVER['argv'] = array_values( $_SERVER['argv'] );
98 }
99
100 if ( !wfIsWindows() ) {
101 # If we are not running on windows then we can enable phpunit colors
102 # Windows does not come anymore with ANSI.SYS loaded by default
103 # PHPUnit uses the suite.xml parameters to enable/disable colors
104 # which can be then forced to be enabled with --colors.
105 # The below code injects a parameter just like if the user called
106 # Probably fix bug 29226
107 $key = array_search( '--colors', $_SERVER['argv'] );
108 if ( $key === false ) {
109 array_splice( $_SERVER['argv'], 1, 0, '--colors' );
110 }
111 }
112
113 # Makes MediaWiki PHPUnit directory includable so the PHPUnit will
114 # be able to resolve relative files inclusion such as suites/*
115 # PHPUnit uses stream_resolve_include_path() internally
116 # See bug 32022
117 $key = array_search( '--include-path', $_SERVER['argv'] );
118 if ( $key === false ) {
119 array_splice( $_SERVER['argv'], 1, 0,
120 __DIR__
121 . PATH_SEPARATOR
122 . get_include_path()
123 );
124 array_splice( $_SERVER['argv'], 1, 0, '--include-path' );
125 }
126 }
127
128 public function getDbType() {
129 return Maintenance::DB_ADMIN;
130 }
131 }
132
133 $maintClass = 'PHPUnitMaintClass';
134 require RUN_MAINTENANCE_IF_MAIN;
135
136 if ( !class_exists( 'PHPUnit_Runner_Version' ) ) {
137 require_once 'PHPUnit/Runner/Version.php';
138 }
139
140 if ( PHPUnit_Runner_Version::id() !== '@package_version@'
141 && version_compare( PHPUnit_Runner_Version::id(), '3.7.0', '<' )
142 ) {
143 die( 'PHPUnit 3.7.0 or later required, you have ' . PHPUnit_Runner_Version::id() . ".\n" );
144 }
145
146 if ( !class_exists( 'PHPUnit_TextUI_Command' ) ) {
147 require_once 'PHPUnit/Autoload.php';
148 }
149
150 // Prevent segfault when we have lots of unit tests (bug 62623)
151 if ( version_compare( PHP_VERSION, '5.4.0', '<' )
152 && version_compare( PHP_VERSION, '5.3.0', '>=' )
153 ) {
154 register_shutdown_function( function () {
155 gc_collect_cycles();
156 gc_disable();
157 } );
158 }
159
160 MediaWikiPHPUnitCommand::main();