3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
22 use PHPUnit\Framework\TestCase
;
25 * Base class for unit tests.
27 * Extend this class if you are testing classes which use dependency injection and do not access
28 * global functions, variables, services or a storage backend.
32 abstract class MediaWikiUnitTestCase
extends TestCase
{
33 use PHPUnit4And6Compat
;
34 use MediaWikiCoversValidator
;
35 use MediaWikiTestCaseTrait
;
37 private $unitGlobals = [];
39 protected function setUp() {
41 $reflection = new ReflectionClass( $this );
42 $dirSeparator = DIRECTORY_SEPARATOR
;
43 if ( strpos( $reflection->getFilename(), "${dirSeparator}unit${dirSeparator}" ) === false ) {
44 $this->fail( 'This unit test needs to be in "tests/phpunit/unit"!' );
46 $this->unitGlobals
= $GLOBALS;
49 // Add back the minimal set of globals needed for unit tests to run for core +
51 foreach ( $this->unitGlobals
['wgPhpUnitBootstrapGlobals'] ??
[] as $key => $value ) {
52 $GLOBALS[ $key ] = $this->unitGlobals
[ $key ];
56 protected function tearDown() {
57 $GLOBALS = $this->unitGlobals
;
62 * Create a temporary hook handler which will be reset by tearDown.
63 * This replaces other handlers for the same hook.
64 * @param string $hookName Hook name
65 * @param mixed $handler Value suitable for a hook handler
68 protected function setTemporaryHook( $hookName, $handler ) {
69 // This will be reset by tearDown() when it restores globals. We don't want to use
70 // Hooks::register()/clear() because they won't replace other handlers for the same hook,
71 // which doesn't match behavior of MediaWikiIntegrationTestCase.
73 $wgHooks[$hookName] = [ $handler ];