fda986ccc85dae53c8982d7543ba43374e15e998
[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 use PHPUnit\Framework\Exception;
24
25 /**
26 * Base class for unit tests.
27 *
28 * Extend this class if you are testing classes which use dependency injection and do not access
29 * global functions, variables, services or a storage backend.
30 *
31 * @since 1.34
32 */
33 abstract class MediaWikiUnitTestCase extends TestCase {
34 use PHPUnit4And6Compat;
35 use MediaWikiCoversValidator;
36 use MediaWikiTestCaseTrait;
37
38 private static $originalGlobals;
39 private static $unitGlobals;
40
41 /**
42 * Whitelist of globals to allow in MediaWikiUnitTestCase.
43 *
44 * Please, keep this list to the bare minimum.
45 *
46 * @return string[]
47 */
48 private static function getGlobalsWhitelist() {
49 return [
50 // The autoloader may change between bootstrap and the first test,
51 // so (lazily) capture these here instead.
52 'wgAutoloadClasses',
53 'wgAutoloadLocalClasses',
54 // Need for LoggerFactory. Default is NullSpi.
55 'wgMWLoggerDefaultSpi',
56 'wgAutoloadAttemptLowercase'
57 ];
58 }
59
60 public static function setUpBeforeClass() {
61 parent::setUpBeforeClass();
62
63 $reflection = new ReflectionClass( static::class );
64 $dirSeparator = DIRECTORY_SEPARATOR;
65 if ( stripos( $reflection->getFilename(), "${dirSeparator}unit${dirSeparator}" ) === false ) {
66 self::fail( 'This unit test needs to be in "tests/phpunit/unit"!' );
67 }
68
69 if ( defined( 'HHVM_VERSION' ) ) {
70 // There are a number of issues we encountered in trying to make this
71 // work on HHVM. Specifically, once an MediaWikiIntegrationTestCase executes
72 // before us, the original globals go missing. This might have to do with
73 // one of the non-unit tests passing GLOBALS somewhere and causing HHVM
74 // to get confused somehow.
75 return;
76 }
77
78 self::$unitGlobals =& TestSetup::$bootstrapGlobals;
79
80 foreach ( self::getGlobalsWhitelist() as $global ) {
81 self::$unitGlobals[ $global ] =& $GLOBALS[ $global ];
82 }
83
84 // Would be nice if we coud simply replace $GLOBALS as a whole,
85 // but unsetting or re-assigning that breaks the reference of this magic
86 // variable. Thus we have to modify it in place.
87 self::$originalGlobals = [];
88 foreach ( $GLOBALS as $key => $_ ) {
89 // Stash current values
90 self::$originalGlobals[$key] =& $GLOBALS[$key];
91
92 // Remove globals not part of the snapshot (see bootstrap.php, phpunit.php).
93 // Support: HHVM (avoid self-ref)
94 if ( $key !== 'GLOBALS' && !array_key_exists( $key, self::$unitGlobals ) ) {
95 unset( $GLOBALS[$key] );
96 }
97 }
98 // Restore values from the early snapshot
99 // Not by ref because tests must not be able to modify the snapshot.
100 foreach ( self::$unitGlobals as $key => $value ) {
101 $GLOBALS[ $key ] = $value;
102 }
103 }
104
105 /**
106 * @inheritDoc
107 */
108 protected function runTest() {
109 try {
110 return parent::runTest();
111 } catch ( ConfigException $exception ) {
112 throw new Exception(
113 'Config variables must be mocked, they cannot be accessed directly in tests which extend '
114 . self::class,
115 $exception->getCode(),
116 $exception
117 );
118 }
119 }
120
121 protected function tearDown() {
122 if ( !defined( 'HHVM_VERSION' ) ) {
123 // Quick reset between tests
124 foreach ( $GLOBALS as $key => $_ ) {
125 if ( $key !== 'GLOBALS' && !array_key_exists( $key, self::$unitGlobals ) ) {
126 unset( $GLOBALS[$key] );
127 }
128 }
129 foreach ( self::$unitGlobals as $key => $value ) {
130 $GLOBALS[ $key ] = $value;
131 }
132 }
133
134 parent::tearDown();
135 }
136
137 public static function tearDownAfterClass() {
138 if ( !defined( 'HHVM_VERSION' ) ) {
139 // Remove globals created by the test
140 foreach ( $GLOBALS as $key => $_ ) {
141 if ( $key !== 'GLOBALS' && !array_key_exists( $key, self::$originalGlobals ) ) {
142 unset( $GLOBALS[$key] );
143 }
144 }
145 // Restore values (including reference!)
146 foreach ( self::$originalGlobals as $key => &$value ) {
147 $GLOBALS[ $key ] =& $value;
148 }
149 }
150
151 parent::tearDownAfterClass();
152 }
153
154 /**
155 * Create a temporary hook handler which will be reset by tearDown.
156 * This replaces other handlers for the same hook.
157 * @param string $hookName Hook name
158 * @param mixed $handler Value suitable for a hook handler
159 * @since 1.34
160 */
161 protected function setTemporaryHook( $hookName, $handler ) {
162 // This will be reset by tearDown() when it restores globals. We don't want to use
163 // Hooks::register()/clear() because they won't replace other handlers for the same hook,
164 // which doesn't match behavior of MediaWikiIntegrationTestCase.
165 global $wgHooks;
166 $wgHooks[$hookName] = [ $handler ];
167 }
168
169 protected function getMockMessage( $text, ...$params ) {
170 if ( isset( $params[0] ) && is_array( $params[0] ) ) {
171 $params = $params[0];
172 }
173
174 $msg = $this->getMockBuilder( Message::class )
175 ->disableOriginalConstructor()
176 ->setMethods( [] )
177 ->getMock();
178
179 $msg->method( 'toString' )->willReturn( $text );
180 $msg->method( '__toString' )->willReturn( $text );
181 $msg->method( 'text' )->willReturn( $text );
182 $msg->method( 'parse' )->willReturn( $text );
183 $msg->method( 'plain' )->willReturn( $text );
184 $msg->method( 'parseAsBlock' )->willReturn( $text );
185 $msg->method( 'escaped' )->willReturn( $text );
186
187 $msg->method( 'title' )->willReturn( $msg );
188 $msg->method( 'inLanguage' )->willReturn( $msg );
189 $msg->method( 'inContentLanguage' )->willReturn( $msg );
190 $msg->method( 'useDatabase' )->willReturn( $msg );
191 $msg->method( 'setContext' )->willReturn( $msg );
192
193 $msg->method( 'exists' )->willReturn( true );
194 $msg->method( 'content' )->willReturn( new MessageContent( $msg ) );
195
196 return $msg;
197 }
198 }