3 * @author Antoine Musso
4 * @copyright Copyright © 2013, Antoine Musso
5 * @copyright Copyright © 2013, Wikimedia Foundation Inc.
9 class MWExceptionTest
extends MediaWikiTestCase
{
12 * @expectedException MWException
14 public function testMwexceptionThrowing() {
15 throw new MWException();
19 * Verify the exception classes are JSON serializabe.
21 * @covers MWExceptionHandler::jsonSerializeException
22 * @dataProvider provideExceptionClasses
24 public function testJsonSerializeExceptions( $exception_class ) {
25 $json = MWExceptionHandler
::jsonSerializeException(
26 new $exception_class()
28 $this->assertNotEquals( false, $json,
29 "The $exception_class exception should be JSON serializable, got false." );
32 public function provideExceptionClasses() {
35 array( 'MWException' ),
40 * Lame JSON schema validation.
42 * @covers MWExceptionHandler::jsonSerializeException
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
49 public function testJsonserializeexceptionKeys( $expectedKeyType, $exClass, $key ) {
51 # Make sure we log a backtrace:
52 $this->setMwGlobals( array( 'wgLogExceptionBacktrace' => true ) );
55 MWExceptionHandler
::jsonSerializeException( new $exClass())
57 $this->assertObjectHasAttribute( $key, $json,
58 "JSON serialized exception is missing key '$key'"
60 $this->assertInternalType( $expectedKeyType, $json->$key,
61 "JSON serialized key '$key' has type " . gettype( $json->$key )
62 . " (expected: $expectedKeyType)."
67 * Returns test cases: exception class, key name, gettype()
69 public function provideJsonSerializedKeys() {
71 foreach ( array( 'Exception', 'MWException' ) as $exClass ) {
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' ),
81 $testCases = array_merge( $testCases, $exTests );
87 * Given wgLogExceptionBacktrace is true
88 * then serialized exception SHOULD have a backtrace
90 * @covers MWExceptionHandler::jsonSerializeException
92 public function testJsonserializeexceptionBacktracingEnabled() {
93 $this->setMwGlobals( array( 'wgLogExceptionBacktrace' => true ) );
95 MWExceptionHandler
::jsonSerializeException( new Exception() )
97 $this->assertObjectHasAttribute( 'backtrace', $json );
101 * Given wgLogExceptionBacktrace is false
102 * then serialized exception SHOULD NOT have a backtrace
104 * @covers MWExceptionHandler::jsonSerializeException
106 public function testJsonserializeexceptionBacktracingDisabled() {
107 $this->setMwGlobals( array( 'wgLogExceptionBacktrace' => false ) );
109 MWExceptionHandler
::jsonSerializeException( new Exception() )
111 $this->assertObjectNotHasAttribute( 'backtrace', $json );