From: Max Semenik Date: Fri, 27 Jun 2014 17:59:47 +0000 (-0700) Subject: Reset RequestContext between tests X-Git-Tag: 1.31.0-rc.0~15200^2 X-Git-Url: http://git.cyclocoop.org/fichier?a=commitdiff_plain;h=859aa193597e0ef955cbabddf379a62f97e25d14;p=lhc%2Fweb%2Fwiklou.git Reset RequestContext between tests Its state can change when people do something with objects it returns or when they alter globals like $wgRequest. By resetting this singleton, we ensure that no such change will propagate outside of a test. Change-Id: I7e8598716d810a09c17f80a05deecab617b62346 --- diff --git a/includes/context/RequestContext.php b/includes/context/RequestContext.php index 93602a0d77..d4bf0b4fa8 100644 --- a/includes/context/RequestContext.php +++ b/includes/context/RequestContext.php @@ -68,6 +68,11 @@ class RequestContext implements IContextSource { */ private $config; + /** + * @var RequestContext + */ + private static $instance = null; + /** * Set the Config object * @@ -411,12 +416,21 @@ class RequestContext implements IContextSource { * @return RequestContext */ public static function getMain() { - static $instance = null; - if ( $instance === null ) { - $instance = new self; + if ( self::$instance === null ) { + self::$instance = new self; } - return $instance; + return self::$instance; + } + + /** + * Resets singleton returned by getMain(). Should be called only from unit tests. + */ + public static function resetMain() { + if ( !defined( 'MW_PHPUNIT_TEST' ) ) { + throw new MWException( __METHOD__ . '() should be called only from unit tests!' ); + } + self::$instance = null; } /** diff --git a/tests/phpunit/MediaWikiTestCase.php b/tests/phpunit/MediaWikiTestCase.php index 53b944d6a4..c9184e8534 100644 --- a/tests/phpunit/MediaWikiTestCase.php +++ b/tests/phpunit/MediaWikiTestCase.php @@ -245,6 +245,7 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase { $GLOBALS[$key] = $value; } $this->mwGlobals = array(); + RequestContext::resetMain(); $phpErrorLevel = intval( ini_get( 'error_reporting' ) );