Unset all globals unneeded for unit tests, assert correct directory
[lhc/web/wiklou.git] / tests / phpunit / MediaWikiUnitTestCase.php
index 1065c2f..3f0fc7a 100644 (file)
@@ -31,4 +31,26 @@ abstract class MediaWikiUnitTestCase extends TestCase {
        use PHPUnit4And6Compat;
        use MediaWikiCoversValidator;
 
+       private $unitGlobals = [];
+
+       protected function setUp() {
+               parent::setUp();
+               $reflection = new ReflectionClass( $this );
+               if ( strpos( $reflection->getFilename(), '/unit/' ) === false ) {
+                       $this->fail( 'This unit test needs to be in "tests/phpunit/unit" !' );
+               }
+               $this->unitGlobals = $GLOBALS;
+               unset( $GLOBALS );
+               $GLOBALS = [];
+               // Add back the minimal set of globals needed for unit tests to run for core +
+               // extensions/skins.
+               foreach ( [ 'wgAutoloadClasses', 'wgAutoloadLocalClasses', 'IP' ] as $requiredGlobal ) {
+                       $GLOBALS[$requiredGlobal] = $this->unitGlobals[ $requiredGlobal ];
+               }
+       }
+
+       protected function tearDown() {
+               $GLOBALS = $this->unitGlobals;
+               parent::tearDown();
+       }
 }