MediaWiki  1.33.0
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 ) {
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(
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(
175  );
176  $this->assertObjectHasAttribute( 'backtrace', $json );
177  }
178 
186  $this->setMwGlobals( [ 'wgLogExceptionBacktrace' => false ] );
187  $json = json_decode(
189  );
190  $this->assertObjectNotHasAttribute( 'backtrace', $json );
191  }
192 
193 }
MWExceptionTest\testJsonserializeexceptionBacktracingDisabled
testJsonserializeexceptionBacktracingDisabled()
Given wgLogExceptionBacktrace is false then serialized exception SHOULD NOT have a backtrace.
Definition: MWExceptionTest.php:185
MWExceptionTest
Definition: MWExceptionTest.php:9
MWExceptionTest\testJsonserializeexceptionBacktracingEnabled
testJsonserializeexceptionBacktracingEnabled()
Given wgLogExceptionBacktrace is true then serialized exception SHOULD have a backtrace.
Definition: MWExceptionTest.php:171
MWExceptionTest\testIsLogable
testIsLogable()
MWException::isLoggable.
Definition: MWExceptionTest.php:74
MWExceptionTest\testMwexceptionThrowing
testMwexceptionThrowing()
MWException MWException.
Definition: MWExceptionTest.php:15
MWExceptionTest\getMockLanguage
getMockLanguage()
Definition: MWExceptionTest.php:46
php
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:35
MWExceptionTest\provideUseMessageCache
provideUseMessageCache()
Definition: MWExceptionTest.php:64
MWException
MediaWiki exception.
Definition: MWException.php:26
MWExceptionTest\provideExceptionClasses
static provideExceptionClasses()
Definition: MWExceptionTest.php:112
MediaWikiTestCase\setMwGlobals
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
Definition: MediaWikiTestCase.php:709
$wgCommandLineMode
global $wgCommandLineMode
Definition: DevelopmentSettings.php:27
MediaWikiTestCase
Definition: MediaWikiTestCase.php:17
MWExceptionTest\provideIsCommandLine
static provideIsCommandLine()
Definition: MWExceptionTest.php:91
MWExceptionTest\testisCommandLine
testisCommandLine( $expected, $wgCommandLineMode)
provideIsCommandLine MWException::isCommandLine
Definition: MWExceptionTest.php:83
$wgFullyInitialised
foreach( $wgExtensionFunctions as $func) if(!defined( 'MW_NO_SESSION') &&! $wgCommandLineMode) if(! $wgCommandLineMode) $wgFullyInitialised
Definition: Setup.php:929
null
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition: hooks.txt:780
MWExceptionTest\testUseMessageCache
testUseMessageCache( $expected, $langObj)
provideUseMessageCache MWException::useMessageCache
Definition: MWExceptionTest.php:56
$e
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException' returning false will NOT prevent logging $e
Definition: hooks.txt:2162
MWExceptionTest\provideJsonSerializedKeys
static provideJsonSerializedKeys()
Returns test cases: exception class, key name, gettype()
Definition: MWExceptionTest.php:148
MWExceptionTest\testJsonserializeexceptionKeys
testJsonserializeexceptionKeys( $expectedKeyType, $exClass, $key)
Lame JSON schema validation.
Definition: MWExceptionTest.php:129
MWExceptionTest\testUseOutputPage
testUseOutputPage( $expected, $langObj, $wgFullyInitialised, $wgOut)
provideTextUseOutputPage MWException::useOutputPage
Definition: MWExceptionTest.php:23
as
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
Definition: distributors.txt:9
true
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:1985
MWExceptionTest\testJsonSerializeExceptions
testJsonSerializeExceptions( $exception_class)
Verify the exception classes are JSON serializabe.
Definition: MWExceptionTest.php:104
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
MWExceptionHandler\jsonSerializeException
static jsonSerializeException( $e, $pretty=false, $escaping=0, $catcher=self::CAUGHT_BY_OTHER)
Serialize an Exception object to JSON.
Definition: MWExceptionHandler.php:647
$wgOut
$wgOut
Definition: Setup.php:880
MWExceptionTest\provideTextUseOutputPage
provideTextUseOutputPage()
Definition: MWExceptionTest.php:34