| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
use MediaWiki\Config\ConfigException; |
| 23 |
use MediaWiki\Config\HashConfig; |
| 24 |
use MediaWiki\Deferred\DeferredUpdates; |
| 25 |
use MediaWiki\Deferred\DeferredUpdatesScopeMediaWikiStack; |
| 26 |
use MediaWiki\Deferred\DeferredUpdatesScopeStack; |
| 27 |
use MediaWiki\Logger\LoggerFactory; |
| 28 |
use MediaWiki\Logger\NullSpi; |
| 29 |
use MediaWiki\MediaWikiServices; |
| 30 |
use MediaWiki\Registration\ExtensionRegistry; |
| 31 |
use MediaWiki\Settings\SettingsBuilder; |
| 32 |
use PHPUnit\Framework\Exception; |
| 33 |
use PHPUnit\Framework\TestCase; |
| 34 |
use Wikimedia\ObjectFactory\ObjectFactory; |
| 35 |
use Wikimedia\Services\NoSuchServiceException; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
abstract class MediaWikiUnitTestCase extends TestCase { |
| 47 |
use MediaWikiCoversValidator; |
| 48 |
use MediaWikiTestCaseTrait; |
| 49 |
|
| 50 |
|
| 51 |
private static $originalGlobals; |
| 52 |
|
| 53 |
private static $unitGlobals; |
| 54 |
|
| 55 |
private ?MediaWikiServices $serviceContainer = null; |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
private array $services = []; |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
private static function getAllowedGlobalsList() { |
| 70 |
return [ |
| 71 |
|
| 72 |
|
| 73 |
'wgAutoloadClasses', |
| 74 |
'wgAutoloadLocalClasses', |
| 75 |
|
| 76 |
'wgMWLoggerDefaultSpi', |
| 77 |
'wgLegalTitleChars', |
| 78 |
'wgDevelopmentWarnings', |
| 79 |
|
| 80 |
'wgUrlProtocols', |
| 81 |
|
| 82 |
'wgDebugLogFile', |
| 83 |
'wgDebugLogGroups', |
| 84 |
]; |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
final public static function mediaWikiSetUpBeforeClass(): void { |
| 92 |
$reflection = new ReflectionClass( static::class ); |
| 93 |
$dirSeparator = DIRECTORY_SEPARATOR; |
| 94 |
if ( stripos( $reflection->getFileName(), "{$dirSeparator}unit{$dirSeparator}" ) === false ) { |
| 95 |
self::fail( 'This unit test needs to be in "tests/phpunit/unit"!' ); |
| 96 |
} |
| 97 |
|
| 98 |
self::$unitGlobals =& TestSetup::$bootstrapGlobals; |
| 99 |
|
| 100 |
foreach ( self::getAllowedGlobalsList() as $global ) { |
| 101 |
self::$unitGlobals[ $global ] =& $GLOBALS[ $global ]; |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
self::$originalGlobals = []; |
| 108 |
foreach ( $GLOBALS as $key => $_ ) { |
| 109 |
|
| 110 |
self::$originalGlobals[$key] =& $GLOBALS[$key]; |
| 111 |
|
| 112 |
|
| 113 |
if ( $key !== 'GLOBALS' && !array_key_exists( $key, self::$unitGlobals ) ) { |
| 114 |
unset( $GLOBALS[$key] ); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
foreach ( self::$unitGlobals as $key => $value ) { |
| 120 |
$GLOBALS[ $key ] = $value; |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
DeferredUpdates::setScopeStack( new DeferredUpdatesScopeStack() ); |
| 125 |
MediaWikiServices::disallowGlobalInstanceInUnitTests(); |
| 126 |
ExtensionRegistry::disableForTest(); |
| 127 |
SettingsBuilder::disableAccessForUnitTests(); |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
protected function runTest() { |
| 134 |
try { |
| 135 |
|
| 136 |
LoggerFactory::registerProvider( ObjectFactory::getObjectFromSpec( [ |
| 137 |
'class' => NullSpi::class |
| 138 |
] ) ); |
| 139 |
return parent::runTest(); |
| 140 |
} catch ( ConfigException $exception ) { |
| 141 |
throw new Exception( |
| 142 |
'Config variables must be mocked, they cannot be accessed directly in tests which extend ' |
| 143 |
. self::class, |
| 144 |
$exception->getCode(), |
| 145 |
$exception |
| 146 |
); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
final protected function mediaWikiTearDown(): void { |
| 155 |
|
| 156 |
foreach ( $GLOBALS as $key => $_ ) { |
| 157 |
if ( $key !== 'GLOBALS' && !array_key_exists( $key, self::$unitGlobals ) ) { |
| 158 |
unset( $GLOBALS[$key] ); |
| 159 |
} |
| 160 |
} |
| 161 |
foreach ( self::$unitGlobals as $key => $value ) { |
| 162 |
$GLOBALS[ $key ] = $value; |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
final public static function mediaWikiTearDownAfterClass(): void { |
| 171 |
|
| 172 |
foreach ( $GLOBALS as $key => $_ ) { |
| 173 |
if ( $key !== 'GLOBALS' && !array_key_exists( $key, self::$originalGlobals ) ) { |
| 174 |
unset( $GLOBALS[$key] ); |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
foreach ( self::$originalGlobals as $key => &$value ) { |
| 179 |
$GLOBALS[ $key ] =& $value; |
| 180 |
} |
| 181 |
unset( $value ); |
| 182 |
|
| 183 |
MediaWikiServices::allowGlobalInstanceAfterUnitTests(); |
| 184 |
DeferredUpdates::setScopeStack( new DeferredUpdatesScopeMediaWikiStack() ); |
| 185 |
ExtensionRegistry::enableForTest(); |
| 186 |
SettingsBuilder::enableAccessAfterUnitTests(); |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
protected function getServiceContainer(): MediaWikiServices { |
| 196 |
if ( !$this->serviceContainer ) { |
| 197 |
$this->serviceContainer = $this->getMockBuilder( MediaWikiServices::class ) |
| 198 |
->setConstructorArgs( [ new HashConfig() ] ) |
| 199 |
->onlyMethods( [ |
| 200 |
'getService', |
| 201 |
'disableStorage', |
| 202 |
'isStorageDisabled', |
| 203 |
'redefineService', |
| 204 |
'resetServiceForTesting', |
| 205 |
'resetChildProcessServices', |
| 206 |
'peekService' |
| 207 |
] ) |
| 208 |
->getMock(); |
| 209 |
|
| 210 |
$this->serviceContainer |
| 211 |
->method( 'getService' ) |
| 212 |
->willReturnCallback( function ( $name ) { |
| 213 |
return $this->getService( $name ); |
| 214 |
} ); |
| 215 |
} |
| 216 |
|
| 217 |
return $this->serviceContainer; |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
protected function getService( string $name ) { |
| 228 |
if ( !isset( $this->services[$name] ) ) { |
| 229 |
throw new NoSuchServiceException( $name ); |
| 230 |
} |
| 231 |
|
| 232 |
if ( is_callable( $this->services[$name] ) ) { |
| 233 |
$func = $this->services[$name]; |
| 234 |
$this->services[$name] = $func( $this->serviceContainer ); |
| 235 |
} |
| 236 |
|
| 237 |
return $this->services[$name]; |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
protected function setService( string $name, $service ) { |
| 250 |
$this->services[$name] = $service; |
| 251 |
} |
| 252 |
|
| 253 |
} |