From: addshore Date: Wed, 5 Mar 2014 20:25:49 +0000 (+0100) Subject: Throw exception when trying to stash unset globals X-Git-Tag: 1.31.0-rc.0~16597^2~2 X-Git-Url: http://git.cyclocoop.org/%24action?a=commitdiff_plain;h=4b3b1000c0b5a3165a00d919299ccf14f33bbee8;p=lhc%2Fweb%2Fwiklou.git Throw exception when trying to stash unset globals If we are trying to stash an unset global then throw an exception! Also adds a test Change-Id: I25f493a0a535201c08ca9624c2c650f61d9e256d --- diff --git a/tests/phpunit/MediaWikiTestCase.php b/tests/phpunit/MediaWikiTestCase.php index 82bc0fb061..3444f31e50 100644 --- a/tests/phpunit/MediaWikiTestCase.php +++ b/tests/phpunit/MediaWikiTestCase.php @@ -324,6 +324,7 @@ 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 * @since 1.23 */ protected function stashMwGlobals( $globalKeys ) { @@ -336,6 +337,9 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase { // setMwGlobals() on the same global would override the original // value. if ( !array_key_exists( $globalKey, $this->mwGlobals ) ) { + if ( !array_key_exists( $globalKey, $GLOBALS ) ) { + throw new Exception( "Global with key {$globalKey} doesn't exist and cant be stashed" ); + } // 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 // and if is_object but this does account for objects within objects! diff --git a/tests/phpunit/tests/MediaWikiTestCaseTest.php b/tests/phpunit/tests/MediaWikiTestCaseTest.php index d6815a0142..2846fde000 100644 --- a/tests/phpunit/tests/MediaWikiTestCaseTest.php +++ b/tests/phpunit/tests/MediaWikiTestCaseTest.php @@ -7,6 +7,7 @@ class MediaWikiTestCaseTest extends MediaWikiTestCase { const GLOBAL_KEY_EXISTING = 'MediaWikiTestCaseTestGLOBAL-Existing'; + const GLOBAL_KEY_NONEXISTING = 'MediaWikiTestCaseTestGLOBAL-NONExisting'; public static function setUpBeforeClass() { parent::setUpBeforeClass(); @@ -61,4 +62,16 @@ class MediaWikiTestCaseTest extends MediaWikiTestCase { ); } + /** + * @covers MediaWikiTestCase::stashMwGlobals + */ + public function testExceptionThrownWhenStashingNonExistentGlobals() { + $this->setExpectedException( + 'Exception', + 'Global with key ' . self::GLOBAL_KEY_NONEXISTING . ' doesn\'t exist and cant be stashed' + ); + + $this->stashMwGlobals( self::GLOBAL_KEY_NONEXISTING ); + } + }