Merge "Revert "DefaultSettings.php: Remove $wgPasswordSenderName""
[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 * Verify the exception classes are JSON serializabe.
20 *
21 * @covers MWExceptionHandler::jsonSerializeException
22 * @dataProvider provideExceptionClasses
23 */
24 public function testJsonSerializeExceptions( $exception_class ) {
25 $json = MWExceptionHandler::jsonSerializeException(
26 new $exception_class()
27 );
28 $this->assertNotEquals( false, $json,
29 "The $exception_class exception should be JSON serializable, got false." );
30 }
31
32 public function provideExceptionClasses() {
33 return array(
34 array( 'Exception' ),
35 array( 'MWException' ),
36 );
37 }
38
39 /**
40 * Lame JSON schema validation.
41 *
42 * @covers MWExceptionHandler::jsonSerializeException
43 *
44 * @param $expectedKeyType String Type expected as returned by gettype()
45 * @param $exClass String An exception class (ie: Exception, MWException)
46 * @param $key String Name of the key to validate in the serialized JSON
47 * @dataProvider provideJsonSerializedKeys
48 */
49 public function testJsonserializeexceptionKeys( $expectedKeyType, $exClass, $key ) {
50
51 # Make sure we log a backtrace:
52 $this->setMwGlobals( array( 'wgLogExceptionBacktrace' => true ) );
53
54 $json = json_decode(
55 MWExceptionHandler::jsonSerializeException( new $exClass())
56 );
57 $this->assertObjectHasAttribute( $key, $json,
58 "JSON serialized exception is missing key '$key'"
59 );
60 $this->assertInternalType( $expectedKeyType, $json->$key,
61 "JSON serialized key '$key' has type " . gettype( $json->$key )
62 . " (expected: $expectedKeyType)."
63 );
64 }
65
66 /**
67 * Returns test cases: exception class, key name, gettype()
68 */
69 public function provideJsonSerializedKeys() {
70 $testCases = array();
71 foreach ( array( 'Exception', 'MWException' ) as $exClass ) {
72 $exTests = array(
73 array( 'string', $exClass, 'id' ),
74 array( 'string', $exClass, 'file' ),
75 array( 'integer', $exClass, 'line' ),
76 array( 'string', $exClass, 'message' ),
77 array( 'null', $exClass, 'url' ),
78 # Backtrace only enabled with wgLogExceptionBacktrace = true
79 array( 'array', $exClass, 'backtrace' ),
80 );
81 $testCases = array_merge( $testCases, $exTests );
82 }
83 return $testCases;
84 }
85
86 /**
87 * Given wgLogExceptionBacktrace is true
88 * then serialized exception SHOULD have a backtrace
89 *
90 * @covers MWExceptionHandler::jsonSerializeException
91 */
92 public function testJsonserializeexceptionBacktracingEnabled() {
93 $this->setMwGlobals( array( 'wgLogExceptionBacktrace' => true ) );
94 $json = json_decode(
95 MWExceptionHandler::jsonSerializeException( new Exception() )
96 );
97 $this->assertObjectHasAttribute( 'backtrace', $json );
98 }
99
100 /**
101 * Given wgLogExceptionBacktrace is false
102 * then serialized exception SHOULD NOT have a backtrace
103 *
104 * @covers MWExceptionHandler::jsonSerializeException
105 */
106 public function testJsonserializeexceptionBacktracingDisabled() {
107 $this->setMwGlobals( array( 'wgLogExceptionBacktrace' => false ) );
108 $json = json_decode(
109 MWExceptionHandler::jsonSerializeException( new Exception() )
110 );
111 $this->assertObjectNotHasAttribute( 'backtrace', $json );
112
113 }
114
115 }