MediaWiki REL1_31
MWExceptionTest.php
Go to the documentation of this file.
1<?php
10
15 public function testMwexceptionThrowing() {
16 throw new MWException();
17 }
18
23 public function testUseOutputPage( $expected, $langObj, $wgFullyInitialised, $wgOut ) {
24 $this->setMwGlobals( [
25 'wgLang' => $langObj,
26 'wgFullyInitialised' => $wgFullyInitialised,
27 'wgOut' => $wgOut,
28 ] );
29
30 $e = new MWException();
31 $this->assertEquals( $expected, $e->useOutputPage() );
32 }
33
34 public function provideTextUseOutputPage() {
35 return [
36 // expected, langObj, wgFullyInitialised, wgOut
37 [ false, null, null, null ],
38 [ false, $this->getMockLanguage(), null, null ],
39 [ false, $this->getMockLanguage(), true, null ],
40 [ false, null, true, null ],
41 [ false, null, null, true ],
42 [ true, $this->getMockLanguage(), true, true ],
43 ];
44 }
45
46 private function getMockLanguage() {
47 return $this->getMockBuilder( Language::class )
48 ->disableOriginalConstructor()
49 ->getMock();
50 }
51
56 public function testUseMessageCache( $expected, $langObj ) {
57 $this->setMwGlobals( [
58 'wgLang' => $langObj,
59 ] );
60 $e = new MWException();
61 $this->assertEquals( $expected, $e->useMessageCache() );
62 }
63
64 public function provideUseMessageCache() {
65 return [
66 [ false, null ],
67 [ true, $this->getMockLanguage() ],
68 ];
69 }
70
74 public function testIsLogable() {
75 $e = new MWException();
76 $this->assertTrue( $e->isLoggable() );
77 }
78
83 public function testisCommandLine( $expected, $wgCommandLineMode ) {
84 $this->setMwGlobals( [
85 'wgCommandLineMode' => $wgCommandLineMode,
86 ] );
87 $e = new MWException();
88 $this->assertEquals( $expected, $e->isCommandLine() );
89 }
90
91 public static function provideIsCommandLine() {
92 return [
93 [ false, null ],
94 [ true, true ],
95 ];
96 }
97
104 public function testJsonSerializeExceptions( $exception_class ) {
105 $json = MWExceptionHandler::jsonSerializeException(
106 new $exception_class()
107 );
108 $this->assertNotEquals( false, $json,
109 "The $exception_class exception should be JSON serializable, got false." );
110 }
111
112 public static function provideExceptionClasses() {
113 return [
114 [ Exception::class ],
115 [ MWException::class ],
116 ];
117 }
118
129 public function testJsonserializeexceptionKeys( $expectedKeyType, $exClass, $key ) {
130 # Make sure we log a backtrace:
131 $this->setMwGlobals( [ 'wgLogExceptionBacktrace' => true ] );
132
133 $json = json_decode(
134 MWExceptionHandler::jsonSerializeException( new $exClass() )
135 );
136 $this->assertObjectHasAttribute( $key, $json,
137 "JSON serialized exception is missing key '$key'"
138 );
139 $this->assertInternalType( $expectedKeyType, $json->$key,
140 "JSON serialized key '$key' has type " . gettype( $json->$key )
141 . " (expected: $expectedKeyType)."
142 );
143 }
144
148 public static function provideJsonSerializedKeys() {
149 $testCases = [];
150 foreach ( [ Exception::class, MWException::class ] as $exClass ) {
151 $exTests = [
152 [ 'string', $exClass, 'id' ],
153 [ 'string', $exClass, 'file' ],
154 [ 'integer', $exClass, 'line' ],
155 [ 'string', $exClass, 'message' ],
156 [ 'null', $exClass, 'url' ],
157 # Backtrace only enabled with wgLogExceptionBacktrace = true
158 [ 'array', $exClass, 'backtrace' ],
159 ];
160 $testCases = array_merge( $testCases, $exTests );
161 }
162 return $testCases;
163 }
164
172 $this->setMwGlobals( [ 'wgLogExceptionBacktrace' => true ] );
173 $json = json_decode(
174 MWExceptionHandler::jsonSerializeException( new Exception() )
175 );
176 $this->assertObjectHasAttribute( 'backtrace', $json );
177 }
178
186 $this->setMwGlobals( [ 'wgLogExceptionBacktrace' => false ] );
187 $json = json_decode(
188 MWExceptionHandler::jsonSerializeException( new Exception() )
189 );
190 $this->assertObjectNotHasAttribute( 'backtrace', $json );
191 }
192
193}
global $wgCommandLineMode
$wgOut
Definition Setup.php:912
foreach( $wgExtensionFunctions as $func) if(!defined('MW_NO_SESSION') &&! $wgCommandLineMode) if(! $wgCommandLineMode) $wgFullyInitialised
Definition Setup.php:977
testisCommandLine( $expected, $wgCommandLineMode)
provideIsCommandLine MWException::isCommandLine
testMwexceptionThrowing()
MWException MWException.
testUseMessageCache( $expected, $langObj)
provideUseMessageCache MWException::useMessageCache
static provideIsCommandLine()
testJsonserializeexceptionBacktracingEnabled()
Given wgLogExceptionBacktrace is true then serialized exception SHOULD have a backtrace.
testIsLogable()
MWException::isLoggable.
static provideJsonSerializedKeys()
Returns test cases: exception class, key name, gettype()
testJsonserializeexceptionBacktracingDisabled()
Given wgLogExceptionBacktrace is false then serialized exception SHOULD NOT have a backtrace.
testJsonserializeexceptionKeys( $expectedKeyType, $exClass, $key)
Lame JSON schema validation.
static provideExceptionClasses()
testUseOutputPage( $expected, $langObj, $wgFullyInitialised, $wgOut)
provideTextUseOutputPage MWException::useOutputPage
testJsonSerializeExceptions( $exception_class)
Verify the exception classes are JSON serializabe.
MediaWiki exception.
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return true
Definition hooks.txt:2006
returning false will NOT prevent logging $e
Definition hooks.txt:2176