wiki.techinc.nl/tests/phpunit/MediaWikiTestCaseTrait.php
Aryeh Gregor b0795a2550 Hard-deprecate LockManagerGroup::getDefault/getAny
These appear to be unused. Moreover, their behavior doesn't match their
documentation. getDefault() claims to return NullLockManager if no lock
manager could be found, but really returns it if there's no lock manager
named 'default'. getAny() claims to throw if no lock manager can be
found, but actually throws if no lock manager is named 'default' or
'fsLockManager'. The behavior can be easily replicated by just using
get() yourself and catching any exception.

Bug: T234227
Change-Id: Iad083847f45d6e017a3f7dbfece1f9c155c5efd6
2019-10-29 14:48:44 +02:00

71 lines
1.9 KiB
PHP

<?php
/**
* For code common to both MediaWikiUnitTestCase and MediaWikiIntegrationTestCase.
*/
trait MediaWikiTestCaseTrait {
/**
* Returns a PHPUnit constraint that matches anything other than a fixed set of values. This can
* be used to whitelist values, e.g.
* $mock->expects( $this->never() )->method( $this->anythingBut( 'foo', 'bar' ) );
* which will throw if any unexpected method is called.
*
* @param mixed ...$values Values that are not matched
*/
protected function anythingBut( ...$values ) {
return $this->logicalNot( $this->logicalOr(
...array_map( [ $this, 'matches' ], $values )
) );
}
/**
* Return a PHPUnit mock that is expected to never have any methods called on it.
*
* @param string $type
* @return object
*/
protected function createNoOpMock( $type ) {
$mock = $this->createMock( $type );
$mock->expects( $this->never() )->method( $this->anythingBut( '__destruct' ) );
return $mock;
}
/**
* Don't throw a warning if $function is deprecated and called later
*
* @since 1.19
*
* @param string $function
*/
public function hideDeprecated( $function ) {
Wikimedia\suppressWarnings();
wfDeprecated( $function );
Wikimedia\restoreWarnings();
}
/**
* Check whether file contains given data.
* @param string $fileName
* @param string $actualData
* @param bool $createIfMissing If true, and file does not exist, create it with given data
* and skip the test.
* @param string $msg
* @since 1.30
*/
protected function assertFileContains(
$fileName,
$actualData,
$createIfMissing = false,
$msg = ''
) {
if ( $createIfMissing ) {
if ( !file_exists( $fileName ) ) {
file_put_contents( $fileName, $actualData );
$this->markTestSkipped( "Data file $fileName does not exist" );
}
} else {
$this->assertFileExists( $fileName );
}
$this->assertEquals( file_get_contents( $fileName ), $actualData, $msg );
}
}