From: daniel Date: Thu, 25 Oct 2012 15:29:10 +0000 (+0200) Subject: Stash global only once per test case. X-Git-Tag: 1.31.0-rc.0~21672^2~1 X-Git-Url: https://git.heureux-cyclage.org/index.php?a=commitdiff_plain;h=7385890cfee85f3a33e9c6448fe74fb7f9766cc4;p=lhc%2Fweb%2Fwiklou.git Stash global only once per test case. Make sure we only stash a given global only once per test case since we do not want to override the original value. This allows the same test to modify the same global multiple times, while still preserving the original value to be restored after the test case finished. Change-Id: I9056d6d6879fb976a192960f661904287f9760a8 --- diff --git a/tests/phpunit/MediaWikiTestCase.php b/tests/phpunit/MediaWikiTestCase.php index 5bc36edea6..25ae32d5d7 100644 --- a/tests/phpunit/MediaWikiTestCase.php +++ b/tests/phpunit/MediaWikiTestCase.php @@ -220,15 +220,22 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase { * if an array is given as first argument). */ protected function setMwGlobals( $pairs, $value = null ) { - if ( !is_array( $pairs ) ) { - $key = $pairs; - $this->mwGlobals[$key] = $GLOBALS[$key]; - $GLOBALS[$key] = $value; - } else { - foreach ( $pairs as $key => $value ) { + + // Normalize (string, value) to an array + if( is_string( $pairs ) ) { + $pairs = array( $pairs => $value ); + } + + foreach ( $pairs as $key => $value ) { + // 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( $key, $this->mwGlobals ) ) { $this->mwGlobals[$key] = $GLOBALS[$key]; - $GLOBALS[$key] = $value; } + + // Override the global + $GLOBALS[$key] = $value; } }