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