Merge "Allow to optionally set language in Message constructor"
[lhc/web/wiklou.git] / tests / phpunit / includes / exception / MWExceptionTest.php
index 5f001c7..24741a3 100644 (file)
@@ -11,17 +11,143 @@ class MWExceptionTest extends MediaWikiTestCase {
        /**
         * @expectedException MWException
         */
-       function testMwexceptionThrowing() {
+       public function testMwexceptionThrowing() {
                throw new MWException();
        }
 
+       /**
+        * @dataProvider provideTextUseOutputPage
+        * @covers MWException::useOutputPage
+        */
+       public function testUseOutputPage( $expected, $wgLang, $wgFullyInitialised, $wgOut ) {
+               $this->setMwGlobals( array(
+                       'wgLang' => $wgLang,
+                       'wgFullyInitialised' => $wgFullyInitialised,
+                       'wgOut' => $wgOut,
+               ) );
+
+               $e = new MWException();
+               $this->assertEquals( $expected, $e->useOutputPage() );
+       }
+
+       public function provideTextUseOutputPage() {
+               return array(
+                       // expected, wgLang, wgFullyInitialised, wgOut
+                       array( false, null, null, null ),
+                       array( false, $this->getMockLanguage(), null, null ),
+                       array( false, $this->getMockLanguage(), true, null ),
+                       array( false, null, true, null ),
+                       array( false, null, null, true ),
+                       array( true, $this->getMockLanguage(), true, true ),
+               );
+       }
+
+       private function getMockLanguage() {
+               return $this->getMockBuilder( 'Language' )
+                       ->disableOriginalConstructor()
+                       ->getMock();
+       }
+
+       /**
+        * @dataProvider provideUseMessageCache
+        * @covers MWException::useMessageCache
+        */
+       public function testUseMessageCache( $expected, $wgLang ) {
+               $this->setMwGlobals( array(
+                       'wgLang' => $wgLang,
+               ) );
+               $e = new MWException();
+               $this->assertEquals( $expected, $e->useMessageCache() );
+       }
+
+       public function provideUseMessageCache() {
+               return array(
+                       array( false, null ),
+                       array( true, $this->getMockLanguage() ),
+               );
+       }
+
+       /**
+        * @covers MWException::isLoggable
+        */
+       public function testIsLogable() {
+               $e = new MWException();
+               $this->assertTrue( $e->isLoggable() );
+       }
+
+       /**
+        * @dataProvider provideRunHooks
+        * @covers MWException::runHooks
+        */
+       public function testRunHooks( $wgExceptionHooks, $name, $args, $expectedReturn ) {
+               $this->setMwGlobals( array(
+                       'wgExceptionHooks' => $wgExceptionHooks,
+               ) );
+               $e = new MWException();
+               $this->assertEquals( $expectedReturn, $e->runHooks( $name, $args ) );
+       }
+
+       public function provideRunHooks() {
+               return array(
+                       array( null, null, null, null ),
+                       array( array(), 'name', array(), null ),
+                       array( array( 'name' => false ), 'name', array(), null ),
+                       array(
+                               array( 'mockHook' => array( 'MWExceptionTest::mockHook' ) ),
+                               'mockHook', array(), 'YAY.[]'
+                       ),
+                       array(
+                               array( 'mockHook' => array( 'MWExceptionTest::mockHook' ) ),
+                               'mockHook', array( 'a' ), 'YAY.{"1":"a"}'
+                       ),
+                       array(
+                               array( 'mockHook' => array( 'MWExceptionTest::mockHook' ) ),
+                               'mockHook', array( null ), null
+                       ),
+               );
+       }
+
+       /**
+        * Used in conjunction with provideRunHooks and testRunHooks as a mock callback for a hook
+        */
+       public static function mockHook() {
+               $args = func_get_args();
+               if ( !$args[0] instanceof MWException ) {
+                       return '$caller not instance of MWException';
+               }
+               unset( $args[0] );
+               if ( array_key_exists( 1, $args ) && $args[1] === null ) {
+                       return null;
+               }
+               return 'YAY.' . json_encode( $args );
+       }
+
+       /**
+        * @dataProvider provideIsCommandLine
+        * @covers MWException::isCommandLine
+        */
+       public function testisCommandLine( $expected, $wgCommandLineMode ) {
+               $this->setMwGlobals( array(
+                       'wgCommandLineMode' => $wgCommandLineMode,
+               ) );
+               $e = new MWException();
+               $this->assertEquals( $expected, $e->isCommandLine() );
+       }
+
+       public function provideIsCommandLine() {
+               return array(
+                       array( false, null ),
+                       array( true, true ),
+               );
+       }
+
        /**
         * Verify the exception classes are JSON serializabe.
         *
         * @covers MWExceptionHandler::jsonSerializeException
         * @dataProvider provideExceptionClasses
         */
-       function testJsonSerializeExceptions( $exception_class ) {
+       public function testJsonSerializeExceptions( $exception_class ) {
                $json = MWExceptionHandler::jsonSerializeException(
                        new $exception_class()
                );
@@ -29,7 +155,7 @@ class MWExceptionTest extends MediaWikiTestCase {
                        "The $exception_class exception should be JSON serializable, got false." );
        }
 
-       function provideExceptionClasses() {
+       public function provideExceptionClasses() {
                return array(
                        array( 'Exception' ),
                        array( 'MWException' ),
@@ -46,7 +172,7 @@ class MWExceptionTest extends MediaWikiTestCase {
         * @param $key String Name of the key to validate in the serialized JSON
         * @dataProvider provideJsonSerializedKeys
         */
-       function testJsonserializeexceptionKeys( $expectedKeyType, $exClass, $key ) {
+       public function testJsonserializeexceptionKeys( $expectedKeyType, $exClass, $key ) {
 
                # Make sure we log a backtrace:
                $this->setMwGlobals( array( 'wgLogExceptionBacktrace' => true ) );
@@ -66,7 +192,7 @@ class MWExceptionTest extends MediaWikiTestCase {
        /**
         * Returns test cases: exception class, key name, gettype()
         */
-       function provideJsonSerializedKeys() {
+       public function provideJsonSerializedKeys() {
                $testCases = array();
                foreach ( array( 'Exception', 'MWException' ) as $exClass ) {
                        $exTests = array(
@@ -89,7 +215,7 @@ class MWExceptionTest extends MediaWikiTestCase {
         *
         * @covers MWExceptionHandler::jsonSerializeException
         */
-       function testJsonserializeexceptionBacktracingEnabled() {
+       public function testJsonserializeexceptionBacktracingEnabled() {
                $this->setMwGlobals( array( 'wgLogExceptionBacktrace' => true ) );
                $json = json_decode(
                        MWExceptionHandler::jsonSerializeException( new Exception() )
@@ -103,7 +229,7 @@ class MWExceptionTest extends MediaWikiTestCase {
         *
         * @covers MWExceptionHandler::jsonSerializeException
         */
-       function testJsonserializeexceptionBacktracingDisabled() {
+       public function testJsonserializeexceptionBacktracingDisabled() {
                $this->setMwGlobals( array( 'wgLogExceptionBacktrace' => false ) );
                $json = json_decode(
                        MWExceptionHandler::jsonSerializeException( new Exception() )