Merge "Blacklist Google Glass web browser from JS"
[mediawiki.git] / tests / phpunit / includes / ExceptionTest.php
blobeaef1f7afb6f6f59a23dac36b657681091cfe748
1 <?php
2 /**
3 * Tests for includes/Exception.php.
5 * @author Antoine Musso
6 * @copyright Copyright © 2013, Antoine Musso
7 * @copyright Copyright © 2013, Wikimedia Foundation Inc.
8 * @file
9 */
11 class ExceptionTest extends MediaWikiTestCase {
13 /**
14 * @expectedException MWException
16 function testMwexceptionThrowing() {
17 throw new MWException();
20 /**
21 * Verify the exception classes are JSON serializabe.
23 * @covers MWExceptionHandler::jsonSerializeException
24 * @dataProvider provideExceptionClasses
26 function testJsonSerializeExceptions( $exception_class ) {
27 $json = MWExceptionHandler::jsonSerializeException(
28 new $exception_class()
30 $this->assertNotEquals( false, $json,
31 "The $exception_class exception should be JSON serializable, got false." );
34 function provideExceptionClasses() {
35 return array(
36 array( 'Exception' ),
37 array( 'MWException' ),
41 /**
42 * Lame JSON schema validation.
44 * @covers MWExceptionHandler::jsonSerializeException
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
51 function testJsonserializeexceptionKeys( $expectedKeyType, $exClass, $key ) {
53 # Make sure we log a backtrace:
54 $this->setMwGlobals( array( 'wgLogExceptionBacktrace' => true ) );
56 $json = json_decode(
57 MWExceptionHandler::jsonSerializeException( new $exClass())
59 $this->assertObjectHasAttribute( $key, $json,
60 "JSON serialized exception is missing key '$key'"
62 $this->assertInternalType( $expectedKeyType, $json->$key,
63 "JSON serialized key '$key' has type " . gettype( $json->$key )
64 . " (expected: $expectedKeyType)."
68 /**
69 * Returns test cases: exception class, key name, gettype()
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' ),
83 $testCases = array_merge( $testCases, $exTests );
85 return $testCases;
88 /**
89 * Given wgLogExceptionBacktrace is true
90 * then serialized exception SHOULD have a backtrace
92 * @covers MWExceptionHandler::jsonSerializeException
94 function testJsonserializeexceptionBacktracingEnabled() {
95 $this->setMwGlobals( array( 'wgLogExceptionBacktrace' => true ) );
96 $json = json_decode(
97 MWExceptionHandler::jsonSerializeException( new Exception() )
99 $this->assertObjectHasAttribute( 'backtrace', $json );
103 * Given wgLogExceptionBacktrace is false
104 * then serialized exception SHOULD NOT have a backtrace
106 * @covers MWExceptionHandler::jsonSerializeException
108 function testJsonserializeexceptionBacktracingDisabled() {
109 $this->setMwGlobals( array( 'wgLogExceptionBacktrace' => false ) );
110 $json = json_decode(
111 MWExceptionHandler::jsonSerializeException( new Exception() )
113 $this->assertObjectNotHasAttribute( 'backtrace', $json );