Restore (some) globals after each test
authordaniel <daniel.kinzler@wikimedia.de>
Wed, 5 Sep 2012 13:04:07 +0000 (15:04 +0200)
committerdaniel <daniel.kinzler@wikimedia.de>
Wed, 5 Sep 2012 13:04:07 +0000 (15:04 +0200)
Change-Id: I5fbf777c2bd4b1f88ba76d5cc42ad6df2eee4a9c

tests/phpunit/MediaWikiTestCase.php

index 3debb35..202bd8f 100644 (file)
@@ -14,6 +14,16 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
        protected $reuseDB = false;
        protected $tablesUsed = array(); // tables with data
 
+       protected $restoreGlobals = array( // global variables to restore for each test
+               'wgLang',
+               'wgContLang',
+               'wgLanguageCode',
+               'wgUser',
+               'wgTitle',
+       );
+
+       private $savedGlobals = array();
+
        private static $dbSetup = false;
 
        /**
@@ -114,7 +124,21 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
                return $fname;
        }
 
-       protected function tearDown() {
+       protected function setup() {
+               parent::setup();
+
+               foreach ( $this->restoreGlobals as $var ) {
+                       $v = $GLOBALS[ $var ];
+
+                       if ( is_object( $v ) || is_array( $v ) ) {
+                               $v = clone $v;
+                       }
+
+                       $this->savedGlobals[ $var ] = $v;
+               }
+       }
+
+       protected function teardown() {
                // Cleaning up temporary files
                foreach ( $this->tmpfiles as $fname ) {
                        if ( is_file( $fname ) || ( is_link( $fname ) ) ) {
@@ -131,7 +155,12 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
                        }
                }
 
-               parent::tearDown();
+               // restore saved globals
+               foreach ( $this->savedGlobals as $k => $v ) {
+                       $GLOBALS[ $k ] = $v;
+               }
+
+               parent::teardown();
        }
 
        function dbPrefix() {