From 859aa193597e0ef955cbabddf379a62f97e25d14 Mon Sep 17 00:00:00 2001 From: Max Semenik Date: Fri, 27 Jun 2014 10:59:47 -0700 Subject: [PATCH] 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 --- includes/context/RequestContext.php | 22 ++++++++++++++++++---- tests/phpunit/MediaWikiTestCase.php | 1 + 2 files changed, 19 insertions(+), 4 deletions(-) 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' ) ); -- 2.20.1