wiki.techinc.nl/tests/phpunit/includes/debug/MWDebugTest.php
Ori Livneh 66afa6ecd0 MWDebugTest: disable MWDebug on test teardown
MWDebug::init() is currently irreversible -- once MWDebug is enabled, it cannot
be disabled in that execution context. This means that the MWDebug test suite
(which enables MWDebug) has a nasty side-effect -- all the tests that run after
it run with MWDebug enabled. So add an MWDebug::deinit(), and call it on test
teardown.

Ostensibly this is a great use-case for services and dependency injection. The
reason I am not going that route is that it's not entirely clear to me what the
MWDebug class is supposed to represent. If I were going to spend any
substantial amount of time on this, I would be trying to move it out of core
and into an extension, not converting it into a service.

Change-Id: I52c511be049bc276d203d07283e3aa0944f22d34
2016-05-18 02:23:26 -07:00

138 lines
3.3 KiB
PHP

<?php
class MWDebugTest extends MediaWikiTestCase {
protected function setUp() {
parent::setUp();
/** Clear log before each test */
MWDebug::clearLog();
}
public static function setUpBeforeClass() {
MWDebug::init();
MediaWiki\suppressWarnings();
}
public static function tearDownAfterClass() {
MWDebug::deinit();
MediaWiki\restoreWarnings();
}
/**
* @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::deprecated
*/
public function testAvoidDuplicateDeprecations() {
MWDebug::deprecated( 'wfOldFunction', '1.0', 'component' );
MWDebug::deprecated( 'wfOldFunction', '1.0', 'component' );
// assertCount() not available on WMF integration server
$this->assertEquals( 1,
count( 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->assertEquals( 3,
count( 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 );
$apiMain = new ApiMain( $context );
$result = new ApiResult( $apiMain );
MWDebug::appendDebugInfoToApiResult( $context, $result );
$this->assertInstanceOf( 'ApiResult', $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 );
// exception not thrown
$this->assertInternalType( 'string', $xml );
}
/**
* @param string[] $params
* @param string $requestUrl
*
* @return FauxRequest
*/
private function newApiRequest( array $params, $requestUrl ) {
$request = $this->getMockBuilder( 'FauxRequest' )
->setMethods( [ 'getRequestURL' ] )
->setConstructorArgs( [
$params
] )
->getMock();
$request->expects( $this->any() )
->method( 'getRequestURL' )
->will( $this->returnValue( $requestUrl ) );
return $request;
}
}