Throw exception when trying to stash unset globals
authoraddshore <addshorewiki@gmail.com>
Wed, 5 Mar 2014 20:25:49 +0000 (21:25 +0100)
committeraddshore <addshorewiki@gmail.com>
Sun, 9 Mar 2014 13:52:40 +0000 (14:52 +0100)
If we are trying to stash an unset global then
throw an exception!

Also adds a test

Change-Id: I25f493a0a535201c08ca9624c2c650f61d9e256d

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 );
+       }
+
 }