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 ];
76 protected function getMockMessage( $text, ...$params ) {
77 if ( isset( $params[0] ) && is_array( $params[0] ) ) {
81 $msg = $this->getMockBuilder( Message
::class )
82 ->disableOriginalConstructor()
86 $msg->method( 'toString' )->willReturn( $text );
87 $msg->method( '__toString' )->willReturn( $text );
88 $msg->method( 'text' )->willReturn( $text );
89 $msg->method( 'parse' )->willReturn( $text );
90 $msg->method( 'plain' )->willReturn( $text );
91 $msg->method( 'parseAsBlock' )->willReturn( $text );
92 $msg->method( 'escaped' )->willReturn( $text );
94 $msg->method( 'title' )->willReturn( $msg );
95 $msg->method( 'inLanguage' )->willReturn( $msg );
96 $msg->method( 'inContentLanguage' )->willReturn( $msg );
97 $msg->method( 'useDatabase' )->willReturn( $msg );
98 $msg->method( 'setContext' )->willReturn( $msg );
100 $msg->method( 'exists' )->willReturn( true );
101 $msg->method( 'content' )->willReturn( new MessageContent( $msg ) );