Merge "Throw exception when trying to stash unset globals"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Tue, 11 Mar 2014 09:00:17 +0000 (09:00 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Tue, 11 Mar 2014 09:00:18 +0000 (09:00 +0000)
tests/phpunit/MediaWikiTestCase.php
tests/phpunit/tests/MediaWikiTestCaseTest.php

index 82bc0fb..3444f31 100644 (file)
@@ -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!
index d6815a0..2846fde 100644 (file)
@@ -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 );
+       }
+
 }