diff --git a/tests/common/TestsAutoLoader.php b/tests/common/TestsAutoLoader.php index 2f9990499c2..38bcc1edbad 100644 --- a/tests/common/TestsAutoLoader.php +++ b/tests/common/TestsAutoLoader.php @@ -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", diff --git a/tests/phpunit/data/exception/TestThrowerDummy.php b/tests/phpunit/data/exception/TestThrowerDummy.php new file mode 100644 index 00000000000..f1b2e87a6ff --- /dev/null +++ b/tests/phpunit/data/exception/TestThrowerDummy.php @@ -0,0 +1,23 @@ +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__; + } +} diff --git a/tests/phpunit/unit/includes/exception/MWExceptionHandlerTest.php b/tests/phpunit/unit/includes/exception/MWExceptionHandlerTest.php index e199a7dcd7f..57ffa60f196 100644 --- a/tests/phpunit/unit/includes/exception/MWExceptionHandlerTest.php +++ b/tests/phpunit/unit/includes/exception/MWExceptionHandlerTest.php @@ -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 = <<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 */