Tests for MWExceptionHandler::jsonSerializeException
[lhc/web/wiklou.git] / tests / phpunit / includes / ExceptionTest.php
1 <?php
2 /**
3 * Tests for includes/Exception.php.
4 *
5 * @author Antoine Musso
6 * @copyright Copyright © 2013, Antoine Musso
7 * @copyright Copyright © 2013, Wikimedia Foundation Inc.
8 * @file
9 */
10
11 class ExceptionTest extends MediaWikiTestCase {
12
13 /**
14 * @expectedException MWException
15 */
16 function testMwexceptionThrowing() {
17 throw new MWException();
18 }
19
20 /**
21 * Verify the exception classes are JSON serializabe.
22 *
23 * @covers MWExceptionHandler::jsonSerializeException
24 * @dataProvider provideExceptionClasses
25 */
26 function testJsonSerializeExceptions( $exception_class ) {
27 $json = MWExceptionHandler::jsonSerializeException(
28 new $exception_class()
29 );
30 $this->assertNotEquals( false, $json,
31 "The $exception_class exception should be JSON serializable, got false." );
32 }
33
34 function provideExceptionClasses() {
35 return array(
36 array( 'Exception' ),
37 array( 'MWException' ),
38 );
39 }
40
41
42 /**
43 * Lame JSON schema validation.
44 *
45 * @covers MWExceptionHandler::jsonSerializeException
46 *
47 * @param $expectedKeyType String Type expected as returned by gettype()
48 * @param $exClass String An exception class (ie: Exception, MWException)
49 * @param $key String Name of the key to validate in the serialized JSON
50 * @dataProvider provideJsonSerializedKeys
51 */
52 function testJsonserializeexceptionKeys($expectedKeyType, $exClass, $key) {
53
54 # Make sure we log a backtrace:
55 $this->setMwGlobals( array( 'wgLogExceptionBacktrace' => true ) );
56
57 $json = json_decode(
58 MWExceptionHandler::jsonSerializeException( new $exClass())
59 );
60 $this->assertObjectHasAttribute( $key, $json,
61 "JSON serialized exception is missing key '$key'"
62 );
63 $this->assertInternalType( $expectedKeyType, $json->$key,
64 "JSON serialized key '$key' has type " . gettype($json->$key)
65 . " (expected: $expectedKeyType)."
66 );
67 }
68
69 /**
70 * Returns test cases: exception class, key name, gettype()
71 */
72 function provideJsonSerializedKeys() {
73 $testCases = array();
74 foreach( array( 'Exception', 'MWException' ) as $exClass ) {
75 $exTests = array(
76 array( 'string', $exClass, 'id' ),
77 array( 'string', $exClass, 'file' ),
78 array( 'integer', $exClass, 'line' ),
79 array( 'string', $exClass, 'message' ),
80 array( 'null', $exClass, 'url' ),
81 # Backtrace only enabled with wgLogExceptionBacktrace = true
82 array( 'array', $exClass, 'backtrace' ),
83 );
84 $testCases = array_merge($testCases, $exTests);
85 }
86 return $testCases;
87 }
88
89 /**
90 * Given wgLogExceptionBacktrace is true
91 * then serialized exception SHOULD have a backtrace
92 *
93 * @covers MWExceptionHandler::jsonSerializeException
94 */
95 function testJsonserializeexceptionBacktracingEnabled() {
96 $this->setMwGlobals( array( 'wgLogExceptionBacktrace' => true ) );
97 $json = json_decode(
98 MWExceptionHandler::jsonSerializeException( new Exception() )
99 );
100 $this->assertObjectHasAttribute( 'backtrace', $json );
101 }
102
103 /**
104 * Given wgLogExceptionBacktrace is false
105 * then serialized exception SHOULD NOT have a backtrace
106 *
107 * @covers MWExceptionHandler::jsonSerializeException
108 */
109 function testJsonserializeexceptionBacktracingDisabled() {
110 $this->setMwGlobals( array( 'wgLogExceptionBacktrace' => false ) );
111 $json = json_decode(
112 MWExceptionHandler::jsonSerializeException( new Exception() )
113 );
114 $this->assertObjectNotHasAttribute( 'backtrace', $json );
115
116 }
117
118 }