wiki.techinc.nl/tests/phpunit/includes/debug/MWDebugTest.php
Thiemo Kreuz 40764d277c Replace PHPUnit ->returnValue() with ->willReturn() shortcut
It's the same and makes the test code much more readable, I
would like to argue.

Because of the was I split all the changes I made into smaller
patches this patch contains some other changes in the same
lines where I could not split them off. E.g. removal of
->any(), which is the default anyway and doesn't do anything.

Change-Id: Ib297b989d4aec33b31a4e33fe9d5032865b39be0
2021-04-22 10:37:45 +02:00

171 lines
4 KiB
PHP

<?php
class MWDebugTest extends MediaWikiIntegrationTestCase {
protected function setUp() : void {
parent::setUp();
/** Clear log before each test */
MWDebug::clearLog();
}
public static function setUpBeforeClass() : void {
parent::setUpBeforeClass();
MWDebug::init();
Wikimedia\suppressWarnings();
}
public static function tearDownAfterClass() : void {
MWDebug::deinit();
Wikimedia\restoreWarnings();
parent::tearDownAfterClass();
}
/**
* @covers MWDebug::log
*/
public function testAddLog() {
MWDebug::log( 'logging a string' );
$this->assertEquals(
[ [
'msg' => 'logging a string',
'type' => 'log',
'caller' => 'MWDebugTest->testAddLog',
] ],
MWDebug::getLog()
);
}
/**
* @covers MWDebug::warning
*/
public function testAddWarning() {
MWDebug::warning( 'Warning message' );
$this->assertEquals(
[ [
'msg' => 'Warning message',
'type' => 'warn',
'caller' => 'MWDebugTest::testAddWarning',
] ],
MWDebug::getLog()
);
}
/**
* @covers MWDebug::detectDeprecatedOverride
*/
public function testDetectDeprecatedOverride() {
$baseclassInstance = new TitleValue( NS_MAIN, 'Test' );
$this->assertFalse(
MWDebug::detectDeprecatedOverride(
$baseclassInstance,
TitleValue::class,
'getNamespace',
MW_VERSION
)
);
// create a dummy subclass that overrides a method
$subclassInstance = new class ( NS_MAIN, 'Test' ) extends TitleValue {
public function getNamespace() {
// never called
return -100;
}
};
$this->assertTrue(
MWDebug::detectDeprecatedOverride(
$subclassInstance,
TitleValue::class,
'getNamespace',
MW_VERSION
)
);
// A warning should have been logged
$this->assertCount( 1, MWDebug::getLog() );
}
/**
* @covers MWDebug::deprecated
*/
public function testAvoidDuplicateDeprecations() {
MWDebug::deprecated( 'wfOldFunction', '1.0', 'component' );
MWDebug::deprecated( 'wfOldFunction', '1.0', 'component' );
// assertCount() not available on WMF integration server
$this->assertCount( 1, MWDebug::getLog(),
"Only one deprecated warning per function should be kept"
);
}
/**
* @covers MWDebug::deprecated
*/
public function testAvoidNonConsecutivesDuplicateDeprecations() {
MWDebug::deprecated( 'wfOldFunction', '1.0', 'component' );
MWDebug::warning( 'some warning' );
MWDebug::log( 'we could have logged something too' );
// Another deprecation
MWDebug::deprecated( 'wfOldFunction', '1.0', 'component' );
// assertCount() not available on WMF integration server
$this->assertCount( 3, MWDebug::getLog(),
"Only one deprecated warning per function should be kept"
);
}
/**
* @covers MWDebug::appendDebugInfoToApiResult
*/
public function testAppendDebugInfoToApiResultXmlFormat() {
$request = $this->newApiRequest(
[ 'action' => 'help', 'format' => 'xml' ],
'/api.php?action=help&format=xml'
);
$context = new RequestContext();
$context->setRequest( $request );
$result = new ApiResult( false );
MWDebug::appendDebugInfoToApiResult( $context, $result );
$this->assertInstanceOf( ApiResult::class, $result );
$data = $result->getResultData();
$expectedKeys = [ 'mwVersion', 'phpEngine', 'phpVersion', 'gitRevision', 'gitBranch',
'gitViewUrl', 'time', 'log', 'debugLog', 'queries', 'request', 'memory',
'memoryPeak', 'includes', '_element' ];
foreach ( $expectedKeys as $expectedKey ) {
$this->assertArrayHasKey( $expectedKey, $data['debuginfo'], "debuginfo has $expectedKey" );
}
$xml = ApiFormatXml::recXmlPrint( 'help', $data, null );
// exception not thrown
$this->assertIsString( $xml );
}
/**
* @param string[] $params
* @param string $requestUrl
*
* @return FauxRequest
*/
private function newApiRequest( array $params, $requestUrl ) {
$request = $this->getMockBuilder( FauxRequest::class )
->onlyMethods( [ 'getRequestURL' ] )
->setConstructorArgs( [
$params
] )
->getMock();
$request->method( 'getRequestURL' )
->willReturn( $requestUrl );
return $request;
}
}