| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\Debug\MWDebug; |
| 4 |
use MediaWiki\HookContainer\HookContainer; |
| 5 |
use MediaWiki\HookContainer\StaticHookRegistry; |
| 6 |
use MediaWiki\Message\Message; |
| 7 |
use MediaWiki\Tests\Unit\FakeQqxMessageLocalizer; |
| 8 |
use PHPUnit\Framework\Constraint\Constraint; |
| 9 |
use PHPUnit\Framework\ExpectationFailedException; |
| 10 |
use PHPUnit\Framework\MockObject\MockObject; |
| 11 |
use Psr\Container\ContainerInterface; |
| 12 |
use SebastianBergmann\Comparator\ComparisonFailure; |
| 13 |
use Wikimedia\ObjectFactory\ObjectFactory; |
| 14 |
use Wikimedia\Services\NoSuchServiceException; |
| 15 |
use Wikimedia\Timestamp\ConvertibleTimestamp; |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
trait MediaWikiTestCaseTrait { |
| 21 |
|
| 22 |
private $originalPhpErrorFilter; |
| 23 |
|
| 24 |
|
| 25 |
private $expectedDeprecations = []; |
| 26 |
|
| 27 |
|
| 28 |
private $actualDeprecations = []; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
protected function anythingBut( ...$values ) { |
| 40 |
if ( !in_array( '__destruct', $values, true ) ) { |
| 41 |
|
| 42 |
|
| 43 |
$values[] = '__destruct'; |
| 44 |
} |
| 45 |
return $this->logicalNot( $this->logicalOr( |
| 46 |
...array_map( [ $this, 'identicalTo' ], $values ) |
| 47 |
) ); |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
protected function createNoOpMock( $type, $allow = [] ) { |
| 64 |
$mock = $this->createMock( $type ); |
| 65 |
$mock->expects( $this->never() )->method( $this->anythingBut( '__debugInfo', '__destruct', ...$allow ) ); |
| 66 |
return $mock; |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
protected function createNoOpAbstractMock( $type, $allow = [] ) { |
| 83 |
$mock = $this->getMockBuilder( $type ) |
| 84 |
->disableOriginalConstructor() |
| 85 |
->disableOriginalClone() |
| 86 |
->disableArgumentCloning() |
| 87 |
->disallowMockingUnknownTypes() |
| 88 |
->getMockForAbstractClass(); |
| 89 |
$mock->expects( $this->never() )->method( $this->anythingBut( '__destruct', ...$allow ) ); |
| 90 |
return $mock; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
protected function createSimpleObjectFactory() { |
| 99 |
$serviceContainer = $this->createMock( ContainerInterface::class ); |
| 100 |
$serviceContainer->method( 'has' )->willReturn( false ); |
| 101 |
$serviceContainer->method( 'get' )->willReturnCallback( |
| 102 |
static function ( $serviceName ) { |
| 103 |
throw new NoSuchServiceException( $serviceName ); |
| 104 |
} |
| 105 |
); |
| 106 |
return new ObjectFactory( $serviceContainer ); |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
protected function createHookContainer( $hooks = [] ) { |
| 117 |
$hookContainer = new HookContainer( |
| 118 |
new StaticHookRegistry(), |
| 119 |
$this->createSimpleObjectFactory() |
| 120 |
); |
| 121 |
foreach ( $hooks as $name => $callback ) { |
| 122 |
$hookContainer->register( $name, $callback ); |
| 123 |
} |
| 124 |
return $hookContainer; |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
protected function markTestSkippedIfPhp( $op, $version ) { |
| 136 |
if ( version_compare( PHP_VERSION, $version, $op ) ) { |
| 137 |
$this->markTestSkipped( "PHP $version isn't supported for this test" ); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
public function hideDeprecated( $function ) { |
| 149 |
|
| 150 |
|
| 151 |
$this->filterDeprecated( '/Use of ' . preg_quote( $function, '/' ) . ' /' ); |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
public function filterDeprecated( $regex ) { |
| 162 |
MWDebug::filterDeprecationForTest( $regex ); |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
public function expectDeprecationAndContinue( string $regex ): void { |
| 174 |
$this->expectedDeprecations[] = $regex; |
| 175 |
MWDebug::filterDeprecationForTest( $regex, function () use ( $regex ): void { |
| 176 |
$this->actualDeprecations[] = $regex; |
| 177 |
} ); |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
public function checkExpectedDeprecationsOnTearDown(): void { |
| 184 |
if ( $this->expectedDeprecations ) { |
| 185 |
$this->assertSame( [], |
| 186 |
array_diff( $this->expectedDeprecations, $this->actualDeprecations ), |
| 187 |
'Expected deprecation warning(s) were not emitted' ); |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
protected function assertFileContains( |
| 201 |
$fileName, |
| 202 |
$actualData, |
| 203 |
$createIfMissing = false, |
| 204 |
$msg = '' |
| 205 |
) { |
| 206 |
if ( $createIfMissing ) { |
| 207 |
if ( !is_file( $fileName ) ) { |
| 208 |
file_put_contents( $fileName, $actualData ); |
| 209 |
$this->markTestSkipped( "Data file $fileName does not exist" ); |
| 210 |
} |
| 211 |
} else { |
| 212 |
$this->assertFileExists( $fileName ); |
| 213 |
} |
| 214 |
$this->assertEquals( file_get_contents( $fileName ), $actualData, $msg ); |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
protected function assertArrayContains( |
| 229 |
array $expected, |
| 230 |
array $actual, |
| 231 |
$message = '' |
| 232 |
) { |
| 233 |
$patched = array_replace_recursive( $actual, $expected ); |
| 234 |
|
| 235 |
ksort( $patched ); |
| 236 |
ksort( $actual ); |
| 237 |
$result = ( $actual === $patched ); |
| 238 |
|
| 239 |
if ( !$result ) { |
| 240 |
$comparisonFailure = new ComparisonFailure( |
| 241 |
$patched, |
| 242 |
$actual, |
| 243 |
var_export( $patched, true ), |
| 244 |
var_export( $actual, true ) |
| 245 |
); |
| 246 |
|
| 247 |
$failureDescription = 'Failed asserting that array contains the expected submap.'; |
| 248 |
if ( $message != '' ) { |
| 249 |
$failureDescription = $message . "\n" . $failureDescription; |
| 250 |
} |
| 251 |
|
| 252 |
throw new ExpectationFailedException( |
| 253 |
$failureDescription, |
| 254 |
$comparisonFailure |
| 255 |
); |
| 256 |
} else { |
| 257 |
$this->assertTrue( true, $message ); |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
public function assertArrayEquals( |
| 275 |
array $expected, array $actual, $ordered = false, $named = false, string $message = '' |
| 276 |
) { |
| 277 |
if ( !$ordered ) { |
| 278 |
$this->objectAssociativeSort( $expected ); |
| 279 |
$this->objectAssociativeSort( $actual ); |
| 280 |
} |
| 281 |
|
| 282 |
if ( !$named ) { |
| 283 |
$expected = array_values( $expected ); |
| 284 |
$actual = array_values( $actual ); |
| 285 |
} |
| 286 |
|
| 287 |
$this->assertEquals( $expected, $actual, $message ); |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
protected function objectAssociativeSort( array &$array ) { |
| 298 |
uasort( |
| 299 |
$array, |
| 300 |
static function ( $a, $b ) { |
| 301 |
return serialize( $a ) <=> serialize( $b ); |
| 302 |
} |
| 303 |
); |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
protected function phpErrorFilterSetUp() { |
| 310 |
$this->originalPhpErrorFilter = error_reporting(); |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
protected function phpErrorFilterTearDown() { |
| 317 |
$phpErrorFilter = error_reporting(); |
| 318 |
|
| 319 |
if ( $phpErrorFilter !== $this->originalPhpErrorFilter ) { |
| 320 |
error_reporting( $this->originalPhpErrorFilter ); |
| 321 |
$message = "PHP error_reporting setting found dirty." |
| 322 |
. " Did you forget AtEase::restoreWarnings?"; |
| 323 |
$this->fail( $message ); |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
protected function mwDebugTearDown() { |
| 334 |
MWDebug::clearLog(); |
| 335 |
MWDebug::clearDeprecationFilters(); |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
protected function fakeTimestampTearDown() { |
| 347 |
ConvertibleTimestamp::setFakeTime( null ); |
| 348 |
} |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
protected function fakeTimestampSetUp() { |
| 358 |
$realTime = time(); |
| 359 |
$maybeFakeTime = ConvertibleTimestamp::time(); |
| 360 |
if ( abs( $maybeFakeTime - $realTime ) > 1 ) { |
| 361 |
$this->assertTrue( false, "Someone set a fake timestamp ($maybeFakeTime) " . |
| 362 |
"and did not clean it up. This will cause confusing test failures." ); |
| 363 |
} |
| 364 |
} |
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
protected function getMockMessage( string $text = '', array $params = [] ) { |
| 373 |
|
| 374 |
$oneOf = fn ( string ...$methods ) => $this->logicalOr( |
| 375 |
...array_map( [ $this, 'identicalTo' ], $methods ) |
| 376 |
); |
| 377 |
|
| 378 |
$msg = $this->createMock( Message::class ); |
| 379 |
$msg->method( $oneOf( '__toString', 'escaped', 'getKey', 'parse', 'parseAsBlock', |
| 380 |
'plain', 'text', 'toString' ) )->willReturn( $text ); |
| 381 |
$msg->method( 'getParams' )->willReturn( $params ); |
| 382 |
$msg->method( $oneOf( 'inContentLanguage', 'inLanguage', 'numParams', 'params', |
| 383 |
'rawParams', 'setContext', 'title', 'useDatabase' ) )->willReturnSelf(); |
| 384 |
$msg->method( 'exists' )->willReturn( true ); |
| 385 |
return $msg; |
| 386 |
} |
| 387 |
|
| 388 |
private function failStatus( StatusValue $status, $reason, $message = '' ) { |
| 389 |
$reason = $message === '' ? $reason : "$message\n$reason"; |
| 390 |
$this->fail( "$reason\n$status" ); |
| 391 |
} |
| 392 |
|
| 393 |
protected function assertStatusOK( StatusValue $status, $message = '' ) { |
| 394 |
if ( !$status->isOK() ) { |
| 395 |
$errors = $status->splitByErrorType()[0]; |
| 396 |
$this->failStatus( $errors, 'Status should be OK', $message ); |
| 397 |
} else { |
| 398 |
$this->addToAssertionCount( 1 ); |
| 399 |
} |
| 400 |
} |
| 401 |
|
| 402 |
protected function assertStatusGood( StatusValue $status, $message = '' ) { |
| 403 |
if ( !$status->isGood() ) { |
| 404 |
$this->failStatus( $status, 'Status should be Good', $message ); |
| 405 |
} else { |
| 406 |
$this->addToAssertionCount( 1 ); |
| 407 |
} |
| 408 |
} |
| 409 |
|
| 410 |
protected function assertStatusNotOK( StatusValue $status, $message = '' ) { |
| 411 |
if ( $status->isOK() ) { |
| 412 |
$this->failStatus( $status, 'Status should not be OK', $message ); |
| 413 |
} else { |
| 414 |
$this->addToAssertionCount( 1 ); |
| 415 |
} |
| 416 |
} |
| 417 |
|
| 418 |
protected function assertStatusNotGood( StatusValue $status, $message = '' ) { |
| 419 |
if ( $status->isGood() ) { |
| 420 |
$this->failStatus( $status, 'Status should not be Good', $message ); |
| 421 |
} else { |
| 422 |
$this->addToAssertionCount( 1 ); |
| 423 |
} |
| 424 |
} |
| 425 |
|
| 426 |
protected function assertStatusMessage( string $messageKey, StatusValue $status, $message = '' ) { |
| 427 |
if ( !$status->hasMessage( $messageKey ) ) { |
| 428 |
$this->failStatus( $status, "Status should have message $messageKey", $message ); |
| 429 |
} else { |
| 430 |
$this->addToAssertionCount( 1 ); |
| 431 |
} |
| 432 |
} |
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
protected function assertStatusMessagesExactly( StatusValue $expected, StatusValue $actual, $message = '' ) { |
| 451 |
$localizer = new FakeQqxMessageLocalizer(); |
| 452 |
|
| 453 |
foreach ( [ 'error', 'warning' ] as $type ) { |
| 454 |
foreach ( |
| 455 |
array_map( null, $expected->getMessages( $type ), $actual->getMessages( $type ) ) |
| 456 |
as [ $expectedMsg, $actualMsg ] |
| 457 |
) { |
| 458 |
if ( |
| 459 |
$expectedMsg === null || $actualMsg === null || |
| 460 |
$localizer->msg( $expectedMsg )->text() !== $localizer->msg( $actualMsg )->text() |
| 461 |
) { |
| 462 |
$this->failStatus( $actual, "Status messages should be exactly like: $expected\nActual:", $message ); |
| 463 |
} |
| 464 |
} |
| 465 |
} |
| 466 |
|
| 467 |
$this->addToAssertionCount( 1 ); |
| 468 |
} |
| 469 |
|
| 470 |
protected function assertStatusValue( $expected, StatusValue $status, $message = 'Status value' ) { |
| 471 |
$this->assertEquals( $expected, $status->getValue(), $message ); |
| 472 |
} |
| 473 |
|
| 474 |
protected function assertStatusError( string $messageKey, StatusValue $status, $message = '' ) { |
| 475 |
$this->assertStatusNotOK( $status, $message ); |
| 476 |
$this->assertStatusMessage( $messageKey, $status, $message ); |
| 477 |
} |
| 478 |
|
| 479 |
protected function assertStatusWarning( string $messageKey, StatusValue $status, $message = '' ) { |
| 480 |
$this->assertStatusNotGood( $status, $message ); |
| 481 |
$this->assertStatusOK( $status, $message ); |
| 482 |
$this->assertStatusMessage( $messageKey, $status, $message ); |
| 483 |
} |
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
protected function assertHTMLEquals( $expected, $actual, $msg = '' ) { |
| 499 |
$expected = str_replace( '>', ">\n", $expected ); |
| 500 |
$actual = str_replace( '>', ">\n", $actual ); |
| 501 |
|
| 502 |
$this->assertEquals( $expected, $actual, $msg ); |
| 503 |
} |
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
protected function expectPHPError( |
| 515 |
int $errorLevel, |
| 516 |
callable $callback, |
| 517 |
?string $msg = null |
| 518 |
): void { |
| 519 |
try { |
| 520 |
$errorEmitted = false; |
| 521 |
set_error_handler( function ( $_, $actualMsg ) use ( $msg, &$errorEmitted ) { |
| 522 |
if ( $msg !== null ) { |
| 523 |
$this->assertStringContainsString( $msg, $actualMsg ); |
| 524 |
} |
| 525 |
$errorEmitted = true; |
| 526 |
}, $errorLevel ); |
| 527 |
$callback(); |
| 528 |
$this->assertTrue( $errorEmitted, "No PHP error was emitted." ); |
| 529 |
} finally { |
| 530 |
restore_error_handler(); |
| 531 |
} |
| 532 |
} |
| 533 |
} |