<?php
/**
- * Base class for MediaWiki unit tests.
- *
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
use PHPUnit\Framework\TestCase;
+/**
+ * Base class for unit tests.
+ *
+ * Extend this class if you are testing classes which use dependency injection and do not access
+ * global functions, variables, services or a storage backend.
+ */
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 ( $this->unitGlobals['wgPhpUnitBootstrapGlobals'] ?? [] as $key => $value ) {
+ $GLOBALS[ $key ] = $this->unitGlobals[ $key ];
+ }
+ }
+
+ protected function tearDown() {
+ $GLOBALS = $this->unitGlobals;
+ parent::tearDown();
+ }
}