2013-10-28 16:24:50 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* @author Antoine Musso
|
|
|
|
|
* @copyright Copyright © 2013, Antoine Musso
|
|
|
|
|
* @copyright Copyright © 2013, Wikimedia Foundation Inc.
|
|
|
|
|
* @file
|
|
|
|
|
*/
|
|
|
|
|
|
2014-02-24 20:13:49 +00:00
|
|
|
class MWExceptionTest extends MediaWikiTestCase {
|
2013-10-28 16:24:50 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @expectedException MWException
|
|
|
|
|
*/
|
2014-02-24 20:21:09 +00:00
|
|
|
public function testMwexceptionThrowing() {
|
2013-10-28 16:24:50 +00:00
|
|
|
throw new MWException();
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-26 13:01:47 +00:00
|
|
|
/**
|
|
|
|
|
* @dataProvider provideTextUseOutputPage
|
|
|
|
|
* @covers MWException::useOutputPage
|
|
|
|
|
*/
|
|
|
|
|
public function testUseOutputPage( $expected, $wgLang, $wgFullyInitialised, $wgOut ) {
|
|
|
|
|
$this->setMwGlobals( array(
|
|
|
|
|
'wgLang' => $wgLang,
|
|
|
|
|
'wgFullyInitialised' => $wgFullyInitialised,
|
|
|
|
|
'wgOut' => $wgOut,
|
|
|
|
|
) );
|
|
|
|
|
|
|
|
|
|
$e = new MWException();
|
|
|
|
|
$this->assertEquals( $expected, $e->useOutputPage() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function provideTextUseOutputPage() {
|
|
|
|
|
return array(
|
|
|
|
|
// expected, wgLang, wgFullyInitialised, wgOut
|
|
|
|
|
array( false, null, null, null ),
|
|
|
|
|
array( false, $this->getMockLanguage(), null, null ),
|
|
|
|
|
array( false, $this->getMockLanguage(), true, null ),
|
|
|
|
|
array( false, null, true, null ),
|
|
|
|
|
array( false, null, null, true ),
|
|
|
|
|
array( true, $this->getMockLanguage(), true, true ),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function getMockLanguage() {
|
|
|
|
|
return $this->getMockBuilder( 'Language' )
|
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
|
->getMock();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideUseMessageCache
|
|
|
|
|
* @covers MWException::useMessageCache
|
|
|
|
|
*/
|
|
|
|
|
public function testUseMessageCache( $expected, $wgLang ) {
|
|
|
|
|
$this->setMwGlobals( array(
|
|
|
|
|
'wgLang' => $wgLang,
|
|
|
|
|
) );
|
|
|
|
|
$e = new MWException();
|
|
|
|
|
$this->assertEquals( $expected, $e->useMessageCache() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function provideUseMessageCache() {
|
|
|
|
|
return array(
|
|
|
|
|
array( false, null ),
|
|
|
|
|
array( true, $this->getMockLanguage() ),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers MWException::isLoggable
|
|
|
|
|
*/
|
|
|
|
|
public function testIsLogable() {
|
|
|
|
|
$e = new MWException();
|
|
|
|
|
$this->assertTrue( $e->isLoggable() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideRunHooks
|
|
|
|
|
* @covers MWException::runHooks
|
|
|
|
|
*/
|
|
|
|
|
public function testRunHooks( $wgExceptionHooks, $name, $args, $expectedReturn ) {
|
|
|
|
|
$this->setMwGlobals( array(
|
|
|
|
|
'wgExceptionHooks' => $wgExceptionHooks,
|
|
|
|
|
) );
|
|
|
|
|
$e = new MWException();
|
|
|
|
|
$this->assertEquals( $expectedReturn, $e->runHooks( $name, $args ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function provideRunHooks() {
|
|
|
|
|
return array(
|
|
|
|
|
array( null, null, null, null ),
|
|
|
|
|
array( array(), 'name', array(), null ),
|
|
|
|
|
array( array( 'name' => false ), 'name', array(), null ),
|
|
|
|
|
array(
|
|
|
|
|
array( 'mockHook' => array( 'MWExceptionTest::mockHook' ) ),
|
|
|
|
|
'mockHook', array(), 'YAY.[]'
|
|
|
|
|
),
|
|
|
|
|
array(
|
|
|
|
|
array( 'mockHook' => array( 'MWExceptionTest::mockHook' ) ),
|
|
|
|
|
'mockHook', array( 'a' ), 'YAY.{"1":"a"}'
|
|
|
|
|
),
|
|
|
|
|
array(
|
|
|
|
|
array( 'mockHook' => array( 'MWExceptionTest::mockHook' ) ),
|
|
|
|
|
'mockHook', array( null ), null
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Used in conjunction with provideRunHooks and testRunHooks as a mock callback for a hook
|
|
|
|
|
*/
|
|
|
|
|
public static function mockHook() {
|
|
|
|
|
$args = func_get_args();
|
|
|
|
|
if( !$args[0] instanceof MWException ) {
|
|
|
|
|
return '$caller not instance of MWException';
|
|
|
|
|
}
|
|
|
|
|
unset( $args[0] );
|
|
|
|
|
if( array_key_exists( 1, $args ) && $args[1] === null ) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return 'YAY.' . json_encode( $args );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideIsCommandLine
|
|
|
|
|
* @covers MWException::isCommandLine
|
|
|
|
|
*/
|
|
|
|
|
public function testisCommandLine( $expected, $wgCommandLineMode ) {
|
|
|
|
|
$this->setMwGlobals( array(
|
|
|
|
|
'wgCommandLineMode' => $wgCommandLineMode,
|
|
|
|
|
) );
|
|
|
|
|
$e = new MWException();
|
|
|
|
|
$this->assertEquals( $expected, $e->isCommandLine() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function provideIsCommandLine() {
|
|
|
|
|
return array(
|
|
|
|
|
array( false, null ),
|
|
|
|
|
array( true, true ),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-28 16:24:50 +00:00
|
|
|
/**
|
|
|
|
|
* Verify the exception classes are JSON serializabe.
|
|
|
|
|
*
|
|
|
|
|
* @covers MWExceptionHandler::jsonSerializeException
|
|
|
|
|
* @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()
|
|
|
|
|
);
|
|
|
|
|
$this->assertNotEquals( false, $json,
|
|
|
|
|
"The $exception_class exception should be JSON serializable, got false." );
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-24 20:21:09 +00:00
|
|
|
public function provideExceptionClasses() {
|
2013-10-28 16:24:50 +00:00
|
|
|
return array(
|
|
|
|
|
array( 'Exception' ),
|
|
|
|
|
array( 'MWException' ),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Lame JSON schema validation.
|
|
|
|
|
*
|
|
|
|
|
* @covers MWExceptionHandler::jsonSerializeException
|
|
|
|
|
*
|
|
|
|
|
* @param $expectedKeyType String Type expected as returned by gettype()
|
|
|
|
|
* @param $exClass String An exception class (ie: Exception, MWException)
|
|
|
|
|
* @param $key String Name of the key to validate in the serialized JSON
|
|
|
|
|
* @dataProvider provideJsonSerializedKeys
|
|
|
|
|
*/
|
2014-02-24 20:21:09 +00:00
|
|
|
public function testJsonserializeexceptionKeys( $expectedKeyType, $exClass, $key ) {
|
2013-10-28 16:24:50 +00:00
|
|
|
|
|
|
|
|
# Make sure we log a backtrace:
|
|
|
|
|
$this->setMwGlobals( array( 'wgLogExceptionBacktrace' => true ) );
|
|
|
|
|
|
|
|
|
|
$json = json_decode(
|
|
|
|
|
MWExceptionHandler::jsonSerializeException( new $exClass())
|
|
|
|
|
);
|
|
|
|
|
$this->assertObjectHasAttribute( $key, $json,
|
|
|
|
|
"JSON serialized exception is missing key '$key'"
|
|
|
|
|
);
|
|
|
|
|
$this->assertInternalType( $expectedKeyType, $json->$key,
|
2013-11-19 18:03:54 +00:00
|
|
|
"JSON serialized key '$key' has type " . gettype( $json->$key )
|
2013-10-28 16:24:50 +00:00
|
|
|
. " (expected: $expectedKeyType)."
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns test cases: exception class, key name, gettype()
|
|
|
|
|
*/
|
2014-02-24 20:21:09 +00:00
|
|
|
public function provideJsonSerializedKeys() {
|
2013-10-28 16:24:50 +00:00
|
|
|
$testCases = array();
|
2013-11-19 18:03:54 +00:00
|
|
|
foreach ( array( 'Exception', 'MWException' ) as $exClass ) {
|
2013-10-28 16:24:50 +00:00
|
|
|
$exTests = array(
|
2013-12-08 19:31:47 +00:00
|
|
|
array( 'string', $exClass, 'id' ),
|
|
|
|
|
array( 'string', $exClass, 'file' ),
|
|
|
|
|
array( 'integer', $exClass, 'line' ),
|
|
|
|
|
array( 'string', $exClass, 'message' ),
|
|
|
|
|
array( 'null', $exClass, 'url' ),
|
2013-10-28 16:24:50 +00:00
|
|
|
# Backtrace only enabled with wgLogExceptionBacktrace = true
|
2013-12-08 19:31:47 +00:00
|
|
|
array( 'array', $exClass, 'backtrace' ),
|
2013-10-28 16:24:50 +00:00
|
|
|
);
|
2013-11-19 18:03:54 +00:00
|
|
|
$testCases = array_merge( $testCases, $exTests );
|
2013-10-28 16:24:50 +00:00
|
|
|
}
|
|
|
|
|
return $testCases;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Given wgLogExceptionBacktrace is true
|
|
|
|
|
* then serialized exception SHOULD have a backtrace
|
|
|
|
|
*
|
|
|
|
|
* @covers MWExceptionHandler::jsonSerializeException
|
|
|
|
|
*/
|
2014-02-24 20:21:09 +00:00
|
|
|
public function testJsonserializeexceptionBacktracingEnabled() {
|
2013-10-28 16:24:50 +00:00
|
|
|
$this->setMwGlobals( array( 'wgLogExceptionBacktrace' => true ) );
|
|
|
|
|
$json = json_decode(
|
|
|
|
|
MWExceptionHandler::jsonSerializeException( new Exception() )
|
|
|
|
|
);
|
|
|
|
|
$this->assertObjectHasAttribute( 'backtrace', $json );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Given wgLogExceptionBacktrace is false
|
|
|
|
|
* then serialized exception SHOULD NOT have a backtrace
|
|
|
|
|
*
|
|
|
|
|
* @covers MWExceptionHandler::jsonSerializeException
|
|
|
|
|
*/
|
2014-02-24 20:21:09 +00:00
|
|
|
public function testJsonserializeexceptionBacktracingDisabled() {
|
2013-10-28 16:24:50 +00:00
|
|
|
$this->setMwGlobals( array( 'wgLogExceptionBacktrace' => false ) );
|
|
|
|
|
$json = json_decode(
|
|
|
|
|
MWExceptionHandler::jsonSerializeException( new Exception() )
|
|
|
|
|
);
|
|
|
|
|
$this->assertObjectNotHasAttribute( 'backtrace', $json );
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|