From d544acdbbb71f07debba996cb5d8ff4c01d1430d Mon Sep 17 00:00:00 2001 From: addshore Date: Fri, 18 Nov 2016 13:55:21 +0000 Subject: [PATCH] Allow stashing of unset globals in MWTestCase Change-Id: I6a1cf9a2e436978a6068cecdaf74aa58b31100ab --- tests/phpunit/MediaWikiTestCase.php | 21 +++++++++++++++---- tests/phpunit/tests/MediaWikiTestCaseTest.php | 18 +++++++++++----- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/tests/phpunit/MediaWikiTestCase.php b/tests/phpunit/MediaWikiTestCase.php index e0f44166d6..db1df5c04f 100644 --- a/tests/phpunit/MediaWikiTestCase.php +++ b/tests/phpunit/MediaWikiTestCase.php @@ -81,6 +81,13 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase { */ private $mwGlobals = []; + /** + * Holds list of MediaWiki configuration settings to be unset in tearDown(). + * See also setMwGlobals(). + * @var array + */ + private $mwGlobalsToUnset = []; + /** * Holds original loggers which have been replaced by setLogger() * @var LoggerInterface[] @@ -535,7 +542,11 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase { foreach ( $this->mwGlobals as $key => $value ) { $GLOBALS[$key] = $value; } + foreach ( $this->mwGlobalsToUnset as $value ) { + unset( $GLOBALS[$value] ); + } $this->mwGlobals = []; + $this->mwGlobalsToUnset = []; $this->restoreLoggers(); if ( self::$serviceLocator && MediaWikiServices::getInstance() !== self::$serviceLocator ) { @@ -684,8 +695,6 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase { * * @param array|string $globalKeys Key to the global variable, or an array of keys. * - * @throws Exception When trying to stash an unset global - * * @note To allow changes to global variables to take effect on global service instances, * call overrideMwServices(). * @@ -700,9 +709,13 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase { // NOTE: make sure we only save the global once or a second call to // setMwGlobals() on the same global would override the original // value. - if ( !array_key_exists( $globalKey, $this->mwGlobals ) ) { + if ( + !array_key_exists( $globalKey, $this->mwGlobals ) && + !array_key_exists( $globalKey, $this->mwGlobalsToUnset ) + ) { if ( !array_key_exists( $globalKey, $GLOBALS ) ) { - throw new Exception( "Global with key {$globalKey} doesn't exist and cant be stashed" ); + $this->mwGlobalsToUnset[$globalKey] = $globalKey; + continue; } // NOTE: we serialize then unserialize the value in case it is an object // this stops any objects being passed by reference. We could use clone diff --git a/tests/phpunit/tests/MediaWikiTestCaseTest.php b/tests/phpunit/tests/MediaWikiTestCaseTest.php index 5d2f37e6a6..fc075bbdc5 100644 --- a/tests/phpunit/tests/MediaWikiTestCaseTest.php +++ b/tests/phpunit/tests/MediaWikiTestCaseTest.php @@ -90,14 +90,22 @@ class MediaWikiTestCaseTest extends MediaWikiTestCase { /** * @covers MediaWikiTestCase::stashMwGlobals + * @covers MediaWikiTestCase::tearDown */ - public function testExceptionThrownWhenStashingNonExistentGlobals() { - $this->setExpectedException( - 'Exception', - 'Global with key ' . self::GLOBAL_KEY_NONEXISTING . ' doesn\'t exist and cant be stashed' + public function testSetNonExistentGlobalsAreUnsetOnTearDown() { + $globalKey = 'abcdefg1234567'; + $this->setMwGlobals( $globalKey, true ); + $this->assertTrue( + $GLOBALS[$globalKey], + 'Global failed to correctly set' ); - $this->stashMwGlobals( self::GLOBAL_KEY_NONEXISTING ); + $this->tearDown(); + + $this->assertFalse( + isset( $GLOBALS[$globalKey] ), + 'Global failed to be correctly unset' + ); } public function testOverrideMwServices() { -- 2.20.1