3 * @author Antoine Musso
4 * @copyright Copyright © 2013, Antoine Musso
5 * @copyright Copyright © 2013, Wikimedia Foundation Inc.
9 class MWExceptionTest
extends MediaWikiTestCase
{
12 * @expectedException MWException
15 public function testMwexceptionThrowing() {
16 throw new MWException();
20 * @dataProvider provideTextUseOutputPage
21 * @covers MWException::useOutputPage
23 public function testUseOutputPage( $expected, $langObj, $wgFullyInitialised, $wgOut ) {
24 $this->setMwGlobals( [
26 'wgFullyInitialised' => $wgFullyInitialised,
30 $e = new MWException();
31 $this->assertEquals( $expected, $e->useOutputPage() );
34 public function provideTextUseOutputPage() {
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 ],
46 private function getMockLanguage() {
47 return $this->getMockBuilder( Language
::class )
48 ->disableOriginalConstructor()
53 * @dataProvider provideUseMessageCache
54 * @covers MWException::useMessageCache
56 public function testUseMessageCache( $expected, $langObj ) {
57 $this->setMwGlobals( [
60 $e = new MWException();
61 $this->assertEquals( $expected, $e->useMessageCache() );
64 public function provideUseMessageCache() {
67 [ true, $this->getMockLanguage() ],
72 * @covers MWException::isLoggable
74 public function testIsLogable() {
75 $e = new MWException();
76 $this->assertTrue( $e->isLoggable() );
80 * @dataProvider provideIsCommandLine
81 * @covers MWException::isCommandLine
83 public function testisCommandLine( $expected, $wgCommandLineMode ) {
84 $this->setMwGlobals( [
85 'wgCommandLineMode' => $wgCommandLineMode,
87 $e = new MWException();
88 $this->assertEquals( $expected, $e->isCommandLine() );
91 public static function provideIsCommandLine() {
99 * Verify the exception classes are JSON serializabe.
101 * @covers MWExceptionHandler::jsonSerializeException
102 * @dataProvider provideExceptionClasses
104 public function testJsonSerializeExceptions( $exception_class ) {
105 $json = MWExceptionHandler
::jsonSerializeException(
106 new $exception_class()
108 $this->assertNotEquals( false, $json,
109 "The $exception_class exception should be JSON serializable, got false." );
112 public static function provideExceptionClasses() {
114 [ Exception
::class ],
115 [ MWException
::class ],
120 * Lame JSON schema validation.
122 * @covers MWExceptionHandler::jsonSerializeException
124 * @param string $expectedKeyType Type expected as returned by gettype()
125 * @param string $exClass An exception class (ie: Exception, MWException)
126 * @param string $key Name of the key to validate in the serialized JSON
127 * @dataProvider provideJsonSerializedKeys
129 public function testJsonserializeexceptionKeys( $expectedKeyType, $exClass, $key ) {
130 # Make sure we log a backtrace:
131 $this->setMwGlobals( [ 'wgLogExceptionBacktrace' => true ] );
134 MWExceptionHandler
::jsonSerializeException( new $exClass() )
136 $this->assertObjectHasAttribute( $key, $json,
137 "JSON serialized exception is missing key '$key'"
139 $this->assertInternalType( $expectedKeyType, $json->$key,
140 "JSON serialized key '$key' has type " . gettype( $json->$key )
141 . " (expected: $expectedKeyType)."
146 * Returns test cases: exception class, key name, gettype()
148 public static function provideJsonSerializedKeys() {
150 foreach ( [ Exception
::class, MWException
::class ] as $exClass ) {
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' ],
160 $testCases = array_merge( $testCases, $exTests );
166 * Given wgLogExceptionBacktrace is true
167 * then serialized exception SHOULD have a backtrace
169 * @covers MWExceptionHandler::jsonSerializeException
171 public function testJsonserializeexceptionBacktracingEnabled() {
172 $this->setMwGlobals( [ 'wgLogExceptionBacktrace' => true ] );
174 MWExceptionHandler
::jsonSerializeException( new Exception() )
176 $this->assertObjectHasAttribute( 'backtrace', $json );
180 * Given wgLogExceptionBacktrace is false
181 * then serialized exception SHOULD NOT have a backtrace
183 * @covers MWExceptionHandler::jsonSerializeException
185 public function testJsonserializeexceptionBacktracingDisabled() {
186 $this->setMwGlobals( [ 'wgLogExceptionBacktrace' => false ] );
188 MWExceptionHandler
::jsonSerializeException( new Exception() )
190 $this->assertObjectNotHasAttribute( 'backtrace', $json );