Merge "Add SPARQL client to core"
[lhc/web/wiklou.git] / tests / phpunit / includes / exception / MWExceptionTest.php
1 <?php
2 /**
3 * @author Antoine Musso
4 * @copyright Copyright © 2013, Antoine Musso
5 * @copyright Copyright © 2013, Wikimedia Foundation Inc.
6 * @file
7 */
8
9 class MWExceptionTest extends MediaWikiTestCase {
10
11 /**
12 * @expectedException MWException
13 * @covers MWException
14 */
15 public function testMwexceptionThrowing() {
16 throw new MWException();
17 }
18
19 /**
20 * @dataProvider provideTextUseOutputPage
21 * @covers MWException::useOutputPage
22 */
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
52 /**
53 * @dataProvider provideUseMessageCache
54 * @covers MWException::useMessageCache
55 */
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
71 /**
72 * @covers MWException::isLoggable
73 */
74 public function testIsLogable() {
75 $e = new MWException();
76 $this->assertTrue( $e->isLoggable() );
77 }
78
79 /**
80 * @dataProvider provideIsCommandLine
81 * @covers MWException::isCommandLine
82 */
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
98 /**
99 * Verify the exception classes are JSON serializabe.
100 *
101 * @covers MWExceptionHandler::jsonSerializeException
102 * @dataProvider provideExceptionClasses
103 */
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
119 /**
120 * Lame JSON schema validation.
121 *
122 * @covers MWExceptionHandler::jsonSerializeException
123 *
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
128 */
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
145 /**
146 * Returns test cases: exception class, key name, gettype()
147 */
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
165 /**
166 * Given wgLogExceptionBacktrace is true
167 * then serialized exception SHOULD have a backtrace
168 *
169 * @covers MWExceptionHandler::jsonSerializeException
170 */
171 public function testJsonserializeexceptionBacktracingEnabled() {
172 $this->setMwGlobals( [ 'wgLogExceptionBacktrace' => true ] );
173 $json = json_decode(
174 MWExceptionHandler::jsonSerializeException( new Exception() )
175 );
176 $this->assertObjectHasAttribute( 'backtrace', $json );
177 }
178
179 /**
180 * Given wgLogExceptionBacktrace is false
181 * then serialized exception SHOULD NOT have a backtrace
182 *
183 * @covers MWExceptionHandler::jsonSerializeException
184 */
185 public function testJsonserializeexceptionBacktracingDisabled() {
186 $this->setMwGlobals( [ 'wgLogExceptionBacktrace' => false ] );
187 $json = json_decode(
188 MWExceptionHandler::jsonSerializeException( new Exception() )
189 );
190 $this->assertObjectNotHasAttribute( 'backtrace', $json );
191 }
192
193 }