MediaWiki
REL1_24
|
00001 <?php 00009 class MWExceptionTest extends MediaWikiTestCase { 00010 00014 public function testMwexceptionThrowing() { 00015 throw new MWException(); 00016 } 00017 00022 public function testUseOutputPage( $expected, $wgLang, $wgFullyInitialised, $wgOut ) { 00023 $this->setMwGlobals( array( 00024 'wgLang' => $wgLang, 00025 'wgFullyInitialised' => $wgFullyInitialised, 00026 'wgOut' => $wgOut, 00027 ) ); 00028 00029 $e = new MWException(); 00030 $this->assertEquals( $expected, $e->useOutputPage() ); 00031 } 00032 00033 public function provideTextUseOutputPage() { 00034 return array( 00035 // expected, wgLang, wgFullyInitialised, wgOut 00036 array( false, null, null, null ), 00037 array( false, $this->getMockLanguage(), null, null ), 00038 array( false, $this->getMockLanguage(), true, null ), 00039 array( false, null, true, null ), 00040 array( false, null, null, true ), 00041 array( true, $this->getMockLanguage(), true, true ), 00042 ); 00043 } 00044 00045 private function getMockLanguage() { 00046 return $this->getMockBuilder( 'Language' ) 00047 ->disableOriginalConstructor() 00048 ->getMock(); 00049 } 00050 00055 public function testUseMessageCache( $expected, $wgLang ) { 00056 $this->setMwGlobals( array( 00057 'wgLang' => $wgLang, 00058 ) ); 00059 $e = new MWException(); 00060 $this->assertEquals( $expected, $e->useMessageCache() ); 00061 } 00062 00063 public function provideUseMessageCache() { 00064 return array( 00065 array( false, null ), 00066 array( true, $this->getMockLanguage() ), 00067 ); 00068 } 00069 00073 public function testIsLogable() { 00074 $e = new MWException(); 00075 $this->assertTrue( $e->isLoggable() ); 00076 } 00077 00082 public function testRunHooks( $wgExceptionHooks, $name, $args, $expectedReturn ) { 00083 $this->setMwGlobals( array( 00084 'wgExceptionHooks' => $wgExceptionHooks, 00085 ) ); 00086 $e = new MWException(); 00087 $this->assertEquals( $expectedReturn, $e->runHooks( $name, $args ) ); 00088 } 00089 00090 public static function provideRunHooks() { 00091 return array( 00092 array( null, null, null, null ), 00093 array( array(), 'name', array(), null ), 00094 array( array( 'name' => false ), 'name', array(), null ), 00095 array( 00096 array( 'mockHook' => array( 'MWExceptionTest::mockHook' ) ), 00097 'mockHook', array(), 'YAY.[]' 00098 ), 00099 array( 00100 array( 'mockHook' => array( 'MWExceptionTest::mockHook' ) ), 00101 'mockHook', array( 'a' ), 'YAY.{"1":"a"}' 00102 ), 00103 array( 00104 array( 'mockHook' => array( 'MWExceptionTest::mockHook' ) ), 00105 'mockHook', array( null ), null 00106 ), 00107 ); 00108 } 00109 00113 public static function mockHook() { 00114 $args = func_get_args(); 00115 if ( !$args[0] instanceof MWException ) { 00116 return '$caller not instance of MWException'; 00117 } 00118 unset( $args[0] ); 00119 if ( array_key_exists( 1, $args ) && $args[1] === null ) { 00120 return null; 00121 } 00122 return 'YAY.' . json_encode( $args ); 00123 } 00124 00129 public function testisCommandLine( $expected, $wgCommandLineMode ) { 00130 $this->setMwGlobals( array( 00131 'wgCommandLineMode' => $wgCommandLineMode, 00132 ) ); 00133 $e = new MWException(); 00134 $this->assertEquals( $expected, $e->isCommandLine() ); 00135 } 00136 00137 public static function provideIsCommandLine() { 00138 return array( 00139 array( false, null ), 00140 array( true, true ), 00141 ); 00142 } 00143 00150 public function testJsonSerializeExceptions( $exception_class ) { 00151 $json = MWExceptionHandler::jsonSerializeException( 00152 new $exception_class() 00153 ); 00154 $this->assertNotEquals( false, $json, 00155 "The $exception_class exception should be JSON serializable, got false." ); 00156 } 00157 00158 public static function provideExceptionClasses() { 00159 return array( 00160 array( 'Exception' ), 00161 array( 'MWException' ), 00162 ); 00163 } 00164 00175 public function testJsonserializeexceptionKeys( $expectedKeyType, $exClass, $key ) { 00176 00177 # Make sure we log a backtrace: 00178 $this->setMwGlobals( array( 'wgLogExceptionBacktrace' => true ) ); 00179 00180 $json = json_decode( 00181 MWExceptionHandler::jsonSerializeException( new $exClass()) 00182 ); 00183 $this->assertObjectHasAttribute( $key, $json, 00184 "JSON serialized exception is missing key '$key'" 00185 ); 00186 $this->assertInternalType( $expectedKeyType, $json->$key, 00187 "JSON serialized key '$key' has type " . gettype( $json->$key ) 00188 . " (expected: $expectedKeyType)." 00189 ); 00190 } 00191 00195 public static function provideJsonSerializedKeys() { 00196 $testCases = array(); 00197 foreach ( array( 'Exception', 'MWException' ) as $exClass ) { 00198 $exTests = array( 00199 array( 'string', $exClass, 'id' ), 00200 array( 'string', $exClass, 'file' ), 00201 array( 'integer', $exClass, 'line' ), 00202 array( 'string', $exClass, 'message' ), 00203 array( 'null', $exClass, 'url' ), 00204 # Backtrace only enabled with wgLogExceptionBacktrace = true 00205 array( 'array', $exClass, 'backtrace' ), 00206 ); 00207 $testCases = array_merge( $testCases, $exTests ); 00208 } 00209 return $testCases; 00210 } 00211 00218 public function testJsonserializeexceptionBacktracingEnabled() { 00219 $this->setMwGlobals( array( 'wgLogExceptionBacktrace' => true ) ); 00220 $json = json_decode( 00221 MWExceptionHandler::jsonSerializeException( new Exception() ) 00222 ); 00223 $this->assertObjectHasAttribute( 'backtrace', $json ); 00224 } 00225 00232 public function testJsonserializeexceptionBacktracingDisabled() { 00233 $this->setMwGlobals( array( 'wgLogExceptionBacktrace' => false ) ); 00234 $json = json_decode( 00235 MWExceptionHandler::jsonSerializeException( new Exception() ) 00236 ); 00237 $this->assertObjectNotHasAttribute( 'backtrace', $json ); 00238 00239 } 00240 00241 }