2013-10-28 16:24:50 +00:00
|
|
|
<?php
|
2022-01-28 19:33:21 +00:00
|
|
|
|
2024-07-09 15:36:08 +00:00
|
|
|
use MediaWiki\MainConfigNames;
|
|
|
|
|
|
2013-10-28 16:24:50 +00:00
|
|
|
/**
|
2024-02-16 18:04:47 +00:00
|
|
|
* @covers \MWException
|
2013-10-28 16:24:50 +00:00
|
|
|
* @author Antoine Musso
|
|
|
|
|
*/
|
2020-06-30 15:09:24 +00:00
|
|
|
class MWExceptionTest extends MediaWikiIntegrationTestCase {
|
2013-10-28 16:24:50 +00:00
|
|
|
|
2014-02-24 20:21:09 +00:00
|
|
|
public function testMwexceptionThrowing() {
|
2019-10-11 22:22:26 +00:00
|
|
|
$this->expectException( MWException::class );
|
2013-10-28 16:24:50 +00:00
|
|
|
throw new MWException();
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-02 09:35:57 +00:00
|
|
|
public function testUseMessageCache() {
|
2014-02-26 13:01:47 +00:00
|
|
|
$e = new MWException();
|
2020-03-02 09:35:57 +00:00
|
|
|
$this->assertTrue( $e->useMessageCache() );
|
2014-02-26 13:01:47 +00:00
|
|
|
}
|
|
|
|
|
|
2023-12-14 17:29:35 +00:00
|
|
|
public function testIsLoggable() {
|
2014-02-26 13:01:47 +00:00
|
|
|
$e = new MWException();
|
|
|
|
|
$this->assertTrue( $e->isLoggable() );
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-28 16:24:50 +00:00
|
|
|
/**
|
|
|
|
|
* Verify the exception classes are JSON serializabe.
|
|
|
|
|
*
|
|
|
|
|
* @dataProvider provideExceptionClasses
|
|
|
|
|
*/
|
2014-02-24 20:21:09 +00:00
|
|
|
public function testJsonSerializeExceptions( $exception_class ) {
|
2013-10-28 16:24:50 +00:00
|
|
|
$json = MWExceptionHandler::jsonSerializeException(
|
|
|
|
|
new $exception_class()
|
|
|
|
|
);
|
2020-10-28 19:57:14 +00:00
|
|
|
$this->assertIsString( $json,
|
2013-10-28 16:24:50 +00:00
|
|
|
"The $exception_class exception should be JSON serializable, got false." );
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-18 01:28:26 +00:00
|
|
|
public static function provideExceptionClasses() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
2018-01-13 00:02:09 +00:00
|
|
|
[ Exception::class ],
|
|
|
|
|
[ MWException::class ],
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2013-10-28 16:24:50 +00:00
|
|
|
}
|
|
|
|
|
|
2023-12-14 16:28:48 +00:00
|
|
|
/**
|
2024-02-16 18:04:47 +00:00
|
|
|
* @covers \MWException::report
|
2023-12-14 16:28:48 +00:00
|
|
|
*/
|
|
|
|
|
public function testReport() {
|
|
|
|
|
// Turn off to keep mw-error.log file empty in CI (and thus avoid build failure)
|
2024-07-09 15:36:08 +00:00
|
|
|
$this->overrideConfigValue( MainConfigNames::DebugLogGroups, [] );
|
2023-12-14 16:28:48 +00:00
|
|
|
|
|
|
|
|
global $wgOut;
|
|
|
|
|
$wgOut->disable();
|
|
|
|
|
|
|
|
|
|
$e = new class( 'Uh oh!' ) extends MWException {
|
|
|
|
|
public function report() {
|
|
|
|
|
global $wgOut;
|
|
|
|
|
$wgOut->addHTML( 'Oh no!' );
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
MWExceptionHandler::handleException( $e );
|
|
|
|
|
|
|
|
|
|
$this->assertStringContainsString( 'Oh no!', $wgOut->getHTML() );
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-28 16:24:50 +00:00
|
|
|
}
|