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.
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
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
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37