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 * @dataProvider provideTextUseOutputPage
20 * @covers MWException::useOutputPage
22 public function testUseOutputPage( $expected, $wgLang, $wgFullyInitialised, $wgOut ) {
23 $this->setMwGlobals( array(
25 'wgFullyInitialised' => $wgFullyInitialised,
29 $e = new MWException();
30 $this->assertEquals( $expected, $e->useOutputPage() );
33 public function provideTextUseOutputPage() {
35 // expected, wgLang, wgFullyInitialised, wgOut
36 array( false, null, null, null ),
37 array( false, $this->getMockLanguage(), null, null ),
38 array( false, $this->getMockLanguage(), true, null ),
39 array( false, null, true, null ),
40 array( false, null, null, true ),
41 array( true, $this->getMockLanguage(), true, true ),
45 private function getMockLanguage() {
46 return $this->getMockBuilder( 'Language' )
47 ->disableOriginalConstructor()
52 * @dataProvider provideUseMessageCache
53 * @covers MWException::useMessageCache
55 public function testUseMessageCache( $expected, $wgLang ) {
56 $this->setMwGlobals( array(
59 $e = new MWException();
60 $this->assertEquals( $expected, $e->useMessageCache() );
63 public function provideUseMessageCache() {
66 array( true, $this->getMockLanguage() ),
71 * @covers MWException::isLoggable
73 public function testIsLogable() {
74 $e = new MWException();
75 $this->assertTrue( $e->isLoggable() );
79 * @dataProvider provideRunHooks
80 * @covers MWException::runHooks
82 public function testRunHooks( $wgExceptionHooks, $name, $args, $expectedReturn ) {
83 $this->setMwGlobals( array(
84 'wgExceptionHooks' => $wgExceptionHooks,
86 $e = new MWException();
87 $this->assertEquals( $expectedReturn, $e->runHooks( $name, $args ) );
90 public static function provideRunHooks() {
92 array( null, null, null, null ),
93 array( array(), 'name', array(), null ),
94 array( array( 'name' => false ), 'name', array(), null ),
96 array( 'mockHook' => array( 'MWExceptionTest::mockHook' ) ),
97 'mockHook', array(), 'YAY.[]'
100 array( 'mockHook' => array( 'MWExceptionTest::mockHook' ) ),
101 'mockHook', array( 'a' ), 'YAY.{"1":"a"}'
104 array( 'mockHook' => array( 'MWExceptionTest::mockHook' ) ),
105 'mockHook', array( null ), null
111 * Used in conjunction with provideRunHooks and testRunHooks as a mock callback for a hook
113 public static function mockHook() {
114 $args = func_get_args();
115 if ( !$args[0] instanceof MWException
) {
116 return '$caller not instance of MWException';
119 if ( array_key_exists( 1, $args ) && $args[1] === null ) {
122 return 'YAY.' . json_encode( $args );
126 * @dataProvider provideIsCommandLine
127 * @covers MWException::isCommandLine
129 public function testisCommandLine( $expected, $wgCommandLineMode ) {
130 $this->setMwGlobals( array(
131 'wgCommandLineMode' => $wgCommandLineMode,
133 $e = new MWException();
134 $this->assertEquals( $expected, $e->isCommandLine() );
137 public static function provideIsCommandLine() {
139 array( false, null ),
145 * Verify the exception classes are JSON serializabe.
147 * @covers MWExceptionHandler::jsonSerializeException
148 * @dataProvider provideExceptionClasses
150 public function testJsonSerializeExceptions( $exception_class ) {
151 $json = MWExceptionHandler
::jsonSerializeException(
152 new $exception_class()
154 $this->assertNotEquals( false, $json,
155 "The $exception_class exception should be JSON serializable, got false." );
158 public static function provideExceptionClasses() {
160 array( 'Exception' ),
161 array( 'MWException' ),
166 * Lame JSON schema validation.
168 * @covers MWExceptionHandler::jsonSerializeException
170 * @param string $expectedKeyType Type expected as returned by gettype()
171 * @param string $exClass An exception class (ie: Exception, MWException)
172 * @param string $key Name of the key to validate in the serialized JSON
173 * @dataProvider provideJsonSerializedKeys
175 public function testJsonserializeexceptionKeys( $expectedKeyType, $exClass, $key ) {
177 # Make sure we log a backtrace:
178 $this->setMwGlobals( array( 'wgLogExceptionBacktrace' => true ) );
181 MWExceptionHandler
::jsonSerializeException( new $exClass() )
183 $this->assertObjectHasAttribute( $key, $json,
184 "JSON serialized exception is missing key '$key'"
186 $this->assertInternalType( $expectedKeyType, $json->$key,
187 "JSON serialized key '$key' has type " . gettype( $json->$key )
188 . " (expected: $expectedKeyType)."
193 * Returns test cases: exception class, key name, gettype()
195 public static function provideJsonSerializedKeys() {
196 $testCases = array();
197 foreach ( array( 'Exception', 'MWException' ) as $exClass ) {
199 array( 'string', $exClass, 'id' ),
200 array( 'string', $exClass, 'file' ),
201 array( 'integer', $exClass, 'line' ),
202 array( 'string', $exClass, 'message' ),
203 array( 'null', $exClass, 'url' ),
204 # Backtrace only enabled with wgLogExceptionBacktrace = true
205 array( 'array', $exClass, 'backtrace' ),
207 $testCases = array_merge( $testCases, $exTests );
213 * Given wgLogExceptionBacktrace is true
214 * then serialized exception SHOULD have a backtrace
216 * @covers MWExceptionHandler::jsonSerializeException
218 public function testJsonserializeexceptionBacktracingEnabled() {
219 $this->setMwGlobals( array( 'wgLogExceptionBacktrace' => true ) );
221 MWExceptionHandler
::jsonSerializeException( new Exception() )
223 $this->assertObjectHasAttribute( 'backtrace', $json );
227 * Given wgLogExceptionBacktrace is false
228 * then serialized exception SHOULD NOT have a backtrace
230 * @covers MWExceptionHandler::jsonSerializeException
232 public function testJsonserializeexceptionBacktracingDisabled() {
233 $this->setMwGlobals( array( 'wgLogExceptionBacktrace' => false ) );
235 MWExceptionHandler
::jsonSerializeException( new Exception() )
237 $this->assertObjectNotHasAttribute( 'backtrace', $json );