exception: Add test for MWExceptionHandler trace formatting

Change-Id: I00b9607ba7e17c1f75ef065e83b83d2e1a82870c
This commit is contained in:
Timo Tijhof 2021-01-07 23:28:32 +00:00 committed by Krinkle
parent eae704846b
commit f7e8bc6a69
3 changed files with 57 additions and 0 deletions

View file

@ -33,6 +33,9 @@ $wgAutoloadClasses += [
# tests/integration
'MWHttpRequestTestCase' => "$testDir/integration/includes/http/MWHttpRequestTestCase.php",
# tests/exception
'TestThrowerDummy' => "$testDir/phpunit/data/exception/TestThrowerDummy.php",
# tests/parser
'DbTestPreviewer' => "$testDir/parser/DbTestPreviewer.php",
'DbTestRecorder' => "$testDir/parser/DbTestRecorder.php",

View file

@ -0,0 +1,23 @@
<?php
class TestThrowerDummy {
public function main() {
$this->doFoo();
}
private function doFoo() {
$this->getBar();
}
private function getBar() {
$this->getQuux();
}
private function getQuux() {
throw new Exception( 'Quux failed' );
}
public static function getFile() : string {
return __FILE__;
}
}

View file

@ -22,6 +22,37 @@ class MWExceptionHandlerTest extends \MediaWikiUnitTestCase {
parent::tearDown();
}
/**
* Test end-to-end formatting of an exception, such as used by LogstashFormatter.
*
* @covers MWExceptionHandler
* @see MWExceptionHandler::prettyPrintTrace
*/
public function testTraceFormatting() {
try {
$dummy = new TestThrowerDummy();
$startLine = __LINE__ + 1;
$dummy->main();
} catch ( Exception $e ) {
}
$startFile = __FILE__;
$dummyFile = TestThrowerDummy::getFile();
$dummyClass = TestThrowerDummy::class;
$expected = <<<TEXT
#0 ${dummyFile}(13): ${dummyClass}->getQuux()
#1 ${dummyFile}(9): ${dummyClass}->getBar()
#2 ${dummyFile}(5): ${dummyClass}->doFoo()
#3 ${startFile}($startLine): ${dummyClass}->main()
TEXT;
// Trim up until our call()
$trace = MWExceptionHandler::getRedactedTraceAsString( $e );
$actual = implode( "\n", array_slice( explode( "\n", trim( $trace ) ), 0, 4 ) );
$this->assertEquals( $expected, $actual );
}
/**
* @covers MWExceptionHandler::getRedactedTrace
*/