Merge "Move some more classes to comply with class per file"
[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 */
14 public function testMwexceptionThrowing() {
15 throw new MWException();
16 }
17
18 /**
19 * @dataProvider provideTextUseOutputPage
20 * @covers MWException::useOutputPage
21 */
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
51 /**
52 * @dataProvider provideUseMessageCache
53 * @covers MWException::useMessageCache
54 */
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
70 /**
71 * @covers MWException::isLoggable
72 */
73 public function testIsLogable() {
74 $e = new MWException();
75 $this->assertTrue( $e->isLoggable() );
76 }
77
78 /**
79 * @dataProvider provideIsCommandLine
80 * @covers MWException::isCommandLine
81 */
82 public function testisCommandLine( $expected, $wgCommandLineMode ) {
83 $this->setMwGlobals( [
84 'wgCommandLineMode' => $wgCommandLineMode,
85 ] );
86 $e = new MWException();
87 $this->assertEquals( $expected, $e->isCommandLine() );
88 }
89
90 public static function provideIsCommandLine() {
91 return [
92 [ false, null ],
93 [ true, true ],
94 ];
95 }
96
97 /**
98 * Verify the exception classes are JSON serializabe.
99 *
100 * @covers MWExceptionHandler::jsonSerializeException
101 * @dataProvider provideExceptionClasses
102 */
103 public function testJsonSerializeExceptions( $exception_class ) {
104 $json = MWExceptionHandler::jsonSerializeException(
105 new $exception_class()
106 );
107 $this->assertNotEquals( false, $json,
108 "The $exception_class exception should be JSON serializable, got false." );
109 }
110
111 public static function provideExceptionClasses() {
112 return [
113 [ 'Exception' ],
114 [ 'MWException' ],
115 ];
116 }
117
118 /**
119 * Lame JSON schema validation.
120 *
121 * @covers MWExceptionHandler::jsonSerializeException
122 *
123 * @param string $expectedKeyType Type expected as returned by gettype()
124 * @param string $exClass An exception class (ie: Exception, MWException)
125 * @param string $key Name of the key to validate in the serialized JSON
126 * @dataProvider provideJsonSerializedKeys
127 */
128 public function testJsonserializeexceptionKeys( $expectedKeyType, $exClass, $key ) {
129 # Make sure we log a backtrace:
130 $this->setMwGlobals( [ 'wgLogExceptionBacktrace' => true ] );
131
132 $json = json_decode(
133 MWExceptionHandler::jsonSerializeException( new $exClass() )
134 );
135 $this->assertObjectHasAttribute( $key, $json,
136 "JSON serialized exception is missing key '$key'"
137 );
138 $this->assertInternalType( $expectedKeyType, $json->$key,
139 "JSON serialized key '$key' has type " . gettype( $json->$key )
140 . " (expected: $expectedKeyType)."
141 );
142 }
143
144 /**
145 * Returns test cases: exception class, key name, gettype()
146 */
147 public static function provideJsonSerializedKeys() {
148 $testCases = [];
149 foreach ( [ 'Exception', 'MWException' ] as $exClass ) {
150 $exTests = [
151 [ 'string', $exClass, 'id' ],
152 [ 'string', $exClass, 'file' ],
153 [ 'integer', $exClass, 'line' ],
154 [ 'string', $exClass, 'message' ],
155 [ 'null', $exClass, 'url' ],
156 # Backtrace only enabled with wgLogExceptionBacktrace = true
157 [ 'array', $exClass, 'backtrace' ],
158 ];
159 $testCases = array_merge( $testCases, $exTests );
160 }
161 return $testCases;
162 }
163
164 /**
165 * Given wgLogExceptionBacktrace is true
166 * then serialized exception SHOULD have a backtrace
167 *
168 * @covers MWExceptionHandler::jsonSerializeException
169 */
170 public function testJsonserializeexceptionBacktracingEnabled() {
171 $this->setMwGlobals( [ 'wgLogExceptionBacktrace' => true ] );
172 $json = json_decode(
173 MWExceptionHandler::jsonSerializeException( new Exception() )
174 );
175 $this->assertObjectHasAttribute( 'backtrace', $json );
176 }
177
178 /**
179 * Given wgLogExceptionBacktrace is false
180 * then serialized exception SHOULD NOT have a backtrace
181 *
182 * @covers MWExceptionHandler::jsonSerializeException
183 */
184 public function testJsonserializeexceptionBacktracingDisabled() {
185 $this->setMwGlobals( [ 'wgLogExceptionBacktrace' => false ] );
186 $json = json_decode(
187 MWExceptionHandler::jsonSerializeException( new Exception() )
188 );
189 $this->assertObjectNotHasAttribute( 'backtrace', $json );
190 }
191
192 }