Fix use of GenderCache in ApiPageSet::processTitlesArray
[lhc/web/wiklou.git] / tests / phpunit / MediaWikiUnitTestCase.php
1 <?php
2 /**
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.
7 *
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.
12 *
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
17 *
18 * @file
19 * @ingroup Testing
20 */
21
22 use PHPUnit\Framework\TestCase;
23
24 /**
25 * Base class for unit tests.
26 *
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.
29 *
30 * @since 1.34
31 */
32 abstract class MediaWikiUnitTestCase extends TestCase {
33 use PHPUnit4And6Compat;
34 use MediaWikiCoversValidator;
35 use MediaWikiTestCaseTrait;
36
37 private $unitGlobals = [];
38
39 protected function setUp() {
40 parent::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"!' );
45 }
46 $this->unitGlobals = $GLOBALS;
47 unset( $GLOBALS );
48 $GLOBALS = [];
49 // Add back the minimal set of globals needed for unit tests to run for core +
50 // extensions/skins.
51 foreach ( $this->unitGlobals['wgPhpUnitBootstrapGlobals'] ?? [] as $key => $value ) {
52 $GLOBALS[ $key ] = $this->unitGlobals[ $key ];
53 }
54 }
55
56 protected function tearDown() {
57 $GLOBALS = $this->unitGlobals;
58 parent::tearDown();
59 }
60
61 /**
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
66 * @since 1.34
67 */
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.
72 global $wgHooks;
73 $wgHooks[$hookName] = [ $handler ];
74 }
75
76 protected function getMockMessage( $text, ...$params ) {
77 if ( isset( $params[0] ) && is_array( $params[0] ) ) {
78 $params = $params[0];
79 }
80
81 $msg = $this->getMockBuilder( Message::class )
82 ->disableOriginalConstructor()
83 ->setMethods( [] )
84 ->getMock();
85
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 );
93
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 );
99
100 $msg->method( 'exists' )->willReturn( true );
101 $msg->method( 'content' )->willReturn( new MessageContent( $msg ) );
102
103 return $msg;
104 }
105 }