wiki.techinc.nl/tests/phpunit/includes/debug/MWDebugTest.php
Timo Tijhof 9cb8bb611b debug: Improve docs, fix ingroup tags, clean up tests
* Remove redundant file-level description and ensure the class desc
  and ingroup tag are on the class block instead.
  Ref https://gerrit.wikimedia.org/r/q/owner:Krinkle+message:ingroup

* Widen `@covers` annotations in unit tests.
  Ref https://gerrit.wikimedia.org/r/q/owner:krinkle+is:merged+message:covers

* Create "Debug" documentation group, covering the debug/ directory.
  This will show up on doc.wikimedia.org under "Modules", where
  each class is listed, and the class page will also link back to
  the group as part of its breadcrumb navigation.

  Test with `php maintenance/mwdocgen.php --file docs/,includes/debug/`
  and then view /w/docs/html/

* Improve docs of various classes and explain relationships better.
  In particular, reformat to ensure each class has a oneline
  description that captures its essential function.

Change-Id: I5d1143a9244b7fd888e1dc31f0fd7965272aa900
2023-10-05 11:54:30 +00:00

145 lines
3.5 KiB
PHP

<?php
use MediaWiki\Request\FauxRequest;
use MediaWiki\Title\TitleValue;
/**
* @covers MWDebug
*/
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();
}
public static function tearDownAfterClass(): void {
MWDebug::deinit();
parent::tearDownAfterClass();
}
public function testAddLog() {
@MWDebug::log( 'logging a string' );
$this->assertEquals(
[ [
'msg' => 'logging a string',
'type' => 'log',
'caller' => 'MWDebugTest->testAddLog',
] ],
MWDebug::getLog()
);
}
public function testAddWarning() {
@MWDebug::warning( 'Warning message' );
$this->assertEquals(
[ [
'msg' => 'Warning message',
'type' => 'warn',
'caller' => 'MWDebugTest::testAddWarning',
] ],
MWDebug::getLog()
);
}
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(): int {
// 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() );
}
public function testAvoidDuplicateDeprecations() {
@MWDebug::deprecated( 'wfOldFunction', '1.0', 'component' );
@MWDebug::deprecated( 'wfOldFunction', '1.0', 'component' );
$this->assertCount( 1, MWDebug::getLog(),
"Only one deprecated warning per function should be kept"
);
}
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' );
$this->assertCount( 3, MWDebug::getLog(),
"Only one deprecated warning per function should be kept"
);
}
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 ) {
$req = new FauxRequest( $params );
$req->setRequestURL( $requestUrl );
return $req;
}
}