Reset RequestContext between tests
authorMax Semenik <maxsem.wiki@gmail.com>
Fri, 27 Jun 2014 17:59:47 +0000 (10:59 -0700)
committerMax Semenik <maxsem.wiki@gmail.com>
Fri, 27 Jun 2014 17:59:47 +0000 (10:59 -0700)
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
tests/phpunit/MediaWikiTestCase.php

index 93602a0..d4bf0b4 100644 (file)
@@ -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;
        }
 
        /**
index 53b944d..c9184e8 100644 (file)
@@ -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' ) );