MediaWiki  1.29.2
MWExceptionTest.php
Go to the documentation of this file.
1 <?php
10 
14  public function testMwexceptionThrowing() {
15  throw new MWException();
16  }
17 
22  public function testUseOutputPage( $expected, $langObj, $wgFullyInitialised, $wgOut ) {
23  $this->setMwGlobals( [
24  'wgLang' => $langObj,
25  'wgFullyInitialised' => $wgFullyInitialised,
26  'wgOut' => $wgOut,
27  ] );
28 
29  $e = new MWException();
30  $this->assertEquals( $expected, $e->useOutputPage() );
31  }
32 
33  public function provideTextUseOutputPage() {
34  return [
35  // expected, langObj, wgFullyInitialised, wgOut
36  [ false, null, null, null ],
37  [ false, $this->getMockLanguage(), null, null ],
38  [ false, $this->getMockLanguage(), true, null ],
39  [ false, null, true, null ],
40  [ false, null, null, true ],
41  [ true, $this->getMockLanguage(), true, true ],
42  ];
43  }
44 
45  private function getMockLanguage() {
46  return $this->getMockBuilder( 'Language' )
47  ->disableOriginalConstructor()
48  ->getMock();
49  }
50 
55  public function testUseMessageCache( $expected, $langObj ) {
56  $this->setMwGlobals( [
57  'wgLang' => $langObj,
58  ] );
59  $e = new MWException();
60  $this->assertEquals( $expected, $e->useMessageCache() );
61  }
62 
63  public function provideUseMessageCache() {
64  return [
65  [ false, null ],
66  [ true, $this->getMockLanguage() ],
67  ];
68  }
69 
73  public function testIsLogable() {
74  $e = new MWException();
75  $this->assertTrue( $e->isLoggable() );
76  }
77 
82  public function testRunHooks( $wgExceptionHooks, $name, $args, $expectedReturn ) {
83  $this->setMwGlobals( [
84  'wgExceptionHooks' => $wgExceptionHooks,
85  ] );
86  $e = new MWException();
87  $this->assertEquals( $expectedReturn, $e->runHooks( $name, $args ) );
88  }
89 
90  public static function provideRunHooks() {
91  return [
92  [ null, null, null, null ],
93  [ [], 'name', [], null ],
94  [ [ 'name' => false ], 'name', [], null ],
95  [
96  [ 'mockHook' => [ 'MWExceptionTest::mockHook' ] ],
97  'mockHook', [], 'YAY.[]'
98  ],
99  [
100  [ 'mockHook' => [ 'MWExceptionTest::mockHook' ] ],
101  'mockHook', [ 'a' ], 'YAY.{"1":"a"}'
102  ],
103  [
104  [ 'mockHook' => [ 'MWExceptionTest::mockHook' ] ],
105  'mockHook', [ null ], null
106  ],
107  ];
108  }
109 
113  public static function mockHook() {
114  $args = func_get_args();
115  if ( !$args[0] instanceof MWException ) {
116  return '$caller not instance of MWException';
117  }
118  unset( $args[0] );
119  if ( array_key_exists( 1, $args ) && $args[1] === null ) {
120  return null;
121  }
122  return 'YAY.' . json_encode( $args );
123  }
124 
129  public function testisCommandLine( $expected, $wgCommandLineMode ) {
130  $this->setMwGlobals( [
131  'wgCommandLineMode' => $wgCommandLineMode,
132  ] );
133  $e = new MWException();
134  $this->assertEquals( $expected, $e->isCommandLine() );
135  }
136 
137  public static function provideIsCommandLine() {
138  return [
139  [ false, null ],
140  [ true, true ],
141  ];
142  }
143 
150  public function testJsonSerializeExceptions( $exception_class ) {
152  new $exception_class()
153  );
154  $this->assertNotEquals( false, $json,
155  "The $exception_class exception should be JSON serializable, got false." );
156  }
157 
158  public static function provideExceptionClasses() {
159  return [
160  [ 'Exception' ],
161  [ 'MWException' ],
162  ];
163  }
164 
175  public function testJsonserializeexceptionKeys( $expectedKeyType, $exClass, $key ) {
176  # Make sure we log a backtrace:
177  $this->setMwGlobals( [ 'wgLogExceptionBacktrace' => true ] );
178 
179  $json = json_decode(
181  );
182  $this->assertObjectHasAttribute( $key, $json,
183  "JSON serialized exception is missing key '$key'"
184  );
185  $this->assertInternalType( $expectedKeyType, $json->$key,
186  "JSON serialized key '$key' has type " . gettype( $json->$key )
187  . " (expected: $expectedKeyType)."
188  );
189  }
190 
194  public static function provideJsonSerializedKeys() {
195  $testCases = [];
196  foreach ( [ 'Exception', 'MWException' ] as $exClass ) {
197  $exTests = [
198  [ 'string', $exClass, 'id' ],
199  [ 'string', $exClass, 'file' ],
200  [ 'integer', $exClass, 'line' ],
201  [ 'string', $exClass, 'message' ],
202  [ 'null', $exClass, 'url' ],
203  # Backtrace only enabled with wgLogExceptionBacktrace = true
204  [ 'array', $exClass, 'backtrace' ],
205  ];
206  $testCases = array_merge( $testCases, $exTests );
207  }
208  return $testCases;
209  }
210 
218  $this->setMwGlobals( [ 'wgLogExceptionBacktrace' => true ] );
219  $json = json_decode(
221  );
222  $this->assertObjectHasAttribute( 'backtrace', $json );
223  }
224 
232  $this->setMwGlobals( [ 'wgLogExceptionBacktrace' => false ] );
233  $json = json_decode(
235  );
236  $this->assertObjectNotHasAttribute( 'backtrace', $json );
237  }
238 
239 }
MWExceptionTest\testJsonserializeexceptionBacktracingDisabled
testJsonserializeexceptionBacktracingDisabled()
Given wgLogExceptionBacktrace is false then serialized exception SHOULD NOT have a backtrace.
Definition: MWExceptionTest.php:231
MWExceptionTest\provideRunHooks
static provideRunHooks()
Definition: MWExceptionTest.php:90
MWExceptionTest
Definition: MWExceptionTest.php:9
false
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:189
MWExceptionTest\testJsonserializeexceptionBacktracingEnabled
testJsonserializeexceptionBacktracingEnabled()
Given wgLogExceptionBacktrace is true then serialized exception SHOULD have a backtrace.
Definition: MWExceptionTest.php:217
MWExceptionTest\testIsLogable
testIsLogable()
MWException::isLoggable.
Definition: MWExceptionTest.php:73
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:304
MWExceptionTest\testMwexceptionThrowing
testMwexceptionThrowing()
MWException.
Definition: MWExceptionTest.php:14
MWExceptionTest\getMockLanguage
getMockLanguage()
Definition: MWExceptionTest.php:45
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:63
MWException
MediaWiki exception.
Definition: MWException.php:26
MWExceptionTest\provideExceptionClasses
static provideExceptionClasses()
Definition: MWExceptionTest.php:158
$wgCommandLineMode
global $wgCommandLineMode
Definition: Setup.php:503
MediaWikiTestCase\setMwGlobals
setMwGlobals( $pairs, $value=null)
Definition: MediaWikiTestCase.php:658
MediaWikiTestCase
Definition: MediaWikiTestCase.php:13
MWExceptionTest\provideIsCommandLine
static provideIsCommandLine()
Definition: MWExceptionTest.php:137
MWExceptionTest\testisCommandLine
testisCommandLine( $expected, $wgCommandLineMode)
provideIsCommandLine MWException::isCommandLine
Definition: MWExceptionTest.php:129
$wgFullyInitialised
foreach( $wgExtensionFunctions as $func) if(!defined( 'MW_NO_SESSION') &&! $wgCommandLineMode) if(! $wgCommandLineMode) $wgFullyInitialised
Definition: Setup.php:856
MWExceptionTest\testUseMessageCache
testUseMessageCache( $expected, $langObj)
provideUseMessageCache MWException::useMessageCache
Definition: MWExceptionTest.php:55
$e
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException' returning false will NOT prevent logging $e
Definition: hooks.txt:2122
MWExceptionTest\provideJsonSerializedKeys
static provideJsonSerializedKeys()
Returns test cases: exception class, key name, gettype()
Definition: MWExceptionTest.php:194
MWExceptionTest\testJsonserializeexceptionKeys
testJsonserializeexceptionKeys( $expectedKeyType, $exClass, $key)
Lame JSON schema validation.
Definition: MWExceptionTest.php:175
MWExceptionTest\testUseOutputPage
testUseOutputPage( $expected, $langObj, $wgFullyInitialised, $wgOut)
provideTextUseOutputPage MWException::useOutputPage
Definition: MWExceptionTest.php:22
MWExceptionTest\mockHook
static mockHook()
Used in conjunction with provideRunHooks and testRunHooks as a mock callback for a hook.
Definition: MWExceptionTest.php:113
$args
if( $line===false) $args
Definition: cdb.php:63
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:1956
MWExceptionTest\testJsonSerializeExceptions
testJsonSerializeExceptions( $exception_class)
Verify the exception classes are JSON serializabe.
Definition: MWExceptionTest.php:150
MWExceptionHandler\jsonSerializeException
static jsonSerializeException( $e, $pretty=false, $escaping=0, $catcher=self::CAUGHT_BY_OTHER)
Serialize an Exception object to JSON.
Definition: MWExceptionHandler.php:576
$wgOut
$wgOut
Definition: Setup.php:791
MWExceptionTest\testRunHooks
testRunHooks( $wgExceptionHooks, $name, $args, $expectedReturn)
provideRunHooks MWException::runHooks
Definition: MWExceptionTest.php:82
MWExceptionTest\provideTextUseOutputPage
provideTextUseOutputPage()
Definition: MWExceptionTest.php:33