From 4ea70270ae96ef852afc6ba019560e8713e3e66b Mon Sep 17 00:00:00 2001 From: addshore Date: Wed, 26 Feb 2014 14:01:47 +0100 Subject: [PATCH] Expand MWException tests Change-Id: Ia6d579eff66c949f6d4318d09afb143ed78f3ac1 --- .../includes/exception/MWExceptionTest.php | 126 ++++++++++++++++++ 1 file changed, 126 insertions(+) diff --git a/tests/phpunit/includes/exception/MWExceptionTest.php b/tests/phpunit/includes/exception/MWExceptionTest.php index f1ddc17e4c..2dc4850c03 100644 --- a/tests/phpunit/includes/exception/MWExceptionTest.php +++ b/tests/phpunit/includes/exception/MWExceptionTest.php @@ -15,6 +15,132 @@ class MWExceptionTest extends MediaWikiTestCase { 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. * -- 2.20.1