MediaWiki REL1_28
MediaWikiServicesTest.php
Go to the documentation of this file.
1<?php
2use Liuggio\StatsdClient\Factory\StatsdDataFactory;
10
17
21 private function newTestConfig() {
22 $globalConfig = new GlobalVarConfig();
23
24 $testConfig = new HashConfig();
25 $testConfig->set( 'ServiceWiringFiles', $globalConfig->get( 'ServiceWiringFiles' ) );
26 $testConfig->set( 'ConfigRegistry', $globalConfig->get( 'ConfigRegistry' ) );
27
28 return $testConfig;
29 }
30
34 private function newMediaWikiServices( Config $config = null ) {
35 if ( $config === null ) {
36 $config = $this->newTestConfig();
37 }
38
39 $instance = new MediaWikiServices( $config );
40
41 // Load the default wiring from the specified files.
42 $wiringFiles = $config->get( 'ServiceWiringFiles' );
43 $instance->loadWiringFiles( $wiringFiles );
44
45 return $instance;
46 }
47
48 public function testGetInstance() {
49 $services = MediaWikiServices::getInstance();
50 $this->assertInstanceOf( 'MediaWiki\\MediaWikiServices', $services );
51 }
52
53 public function testForceGlobalInstance() {
54 $newServices = $this->newMediaWikiServices();
55 $oldServices = MediaWikiServices::forceGlobalInstance( $newServices );
56
57 $this->assertInstanceOf( 'MediaWiki\\MediaWikiServices', $oldServices );
58 $this->assertNotSame( $oldServices, $newServices );
59
60 $theServices = MediaWikiServices::getInstance();
61 $this->assertSame( $theServices, $newServices );
62
63 MediaWikiServices::forceGlobalInstance( $oldServices );
64
65 $theServices = MediaWikiServices::getInstance();
66 $this->assertSame( $theServices, $oldServices );
67 }
68
69 public function testResetGlobalInstance() {
70 $newServices = $this->newMediaWikiServices();
71 $oldServices = MediaWikiServices::forceGlobalInstance( $newServices );
72
73 $service1 = $this->getMock( SalvageableService::class );
74 $service1->expects( $this->never() )
75 ->method( 'salvage' );
76
77 $newServices->defineService(
78 'Test',
79 function() use ( $service1 ) {
80 return $service1;
81 }
82 );
83
84 // force instantiation
85 $newServices->getService( 'Test' );
86
87 MediaWikiServices::resetGlobalInstance( $this->newTestConfig() );
88 $theServices = MediaWikiServices::getInstance();
89
90 $this->assertSame(
91 $service1,
92 $theServices->getService( 'Test' ),
93 'service definition should survive reset'
94 );
95
96 $this->assertNotSame( $theServices, $newServices );
97 $this->assertNotSame( $theServices, $oldServices );
98
99 MediaWikiServices::forceGlobalInstance( $oldServices );
100 }
101
103 $newServices = $this->newMediaWikiServices();
104 $oldServices = MediaWikiServices::forceGlobalInstance( $newServices );
105
106 $service1 = $this->getMock( SalvageableService::class );
107 $service1->expects( $this->never() )
108 ->method( 'salvage' );
109
110 $service2 = $this->getMock( SalvageableService::class );
111 $service2->expects( $this->once() )
112 ->method( 'salvage' )
113 ->with( $service1 );
114
115 // sequence of values the instantiator will return
116 $instantiatorReturnValues = [
117 $service1,
118 $service2,
119 ];
120
121 $newServices->defineService(
122 'Test',
123 function() use ( &$instantiatorReturnValues ) {
124 return array_shift( $instantiatorReturnValues );
125 }
126 );
127
128 // force instantiation
129 $newServices->getService( 'Test' );
130
131 MediaWikiServices::resetGlobalInstance( $this->newTestConfig(), 'quick' );
132 $theServices = MediaWikiServices::getInstance();
133
134 $this->assertSame( $service2, $theServices->getService( 'Test' ) );
135
136 $this->assertNotSame( $theServices, $newServices );
137 $this->assertNotSame( $theServices, $oldServices );
138
139 MediaWikiServices::forceGlobalInstance( $oldServices );
140 }
141
142 public function testDisableStorageBackend() {
143 $newServices = $this->newMediaWikiServices();
144 $oldServices = MediaWikiServices::forceGlobalInstance( $newServices );
145
146 $lbFactory = $this->getMockBuilder( 'LBFactorySimple' )
147 ->disableOriginalConstructor()
148 ->getMock();
149
150 $newServices->redefineService(
151 'DBLoadBalancerFactory',
152 function() use ( $lbFactory ) {
153 return $lbFactory;
154 }
155 );
156
157 // force the service to become active, so we can check that it does get destroyed
158 $newServices->getService( 'DBLoadBalancerFactory' );
159
160 MediaWikiServices::disableStorageBackend(); // should destroy DBLoadBalancerFactory
161
162 try {
163 MediaWikiServices::getInstance()->getService( 'DBLoadBalancerFactory' );
164 $this->fail( 'DBLoadBalancerFactory should have been disabled' );
165 }
166 catch ( ServiceDisabledException $ex ) {
167 // ok, as expected
168 } catch ( Throwable $ex ) {
169 $this->fail( 'ServiceDisabledException expected, caught ' . get_class( $ex ) );
170 }
171
172 MediaWikiServices::forceGlobalInstance( $oldServices );
173 $newServices->destroy();
174 }
175
177 $newServices = $this->newMediaWikiServices();
178 $oldServices = MediaWikiServices::forceGlobalInstance( $newServices );
179
180 $service1 = $this->getMock( DestructibleService::class );
181 $service1->expects( $this->once() )
182 ->method( 'destroy' );
183
184 $service2 = $this->getMock( DestructibleService::class );
185 $service2->expects( $this->never() )
186 ->method( 'destroy' );
187
188 // sequence of values the instantiator will return
189 $instantiatorReturnValues = [
190 $service1,
191 $service2,
192 ];
193
194 $newServices->defineService(
195 'Test',
196 function() use ( &$instantiatorReturnValues ) {
197 return array_shift( $instantiatorReturnValues );
198 }
199 );
200
201 // force the service to become active, so we can check that it does get destroyed
202 $oldTestService = $newServices->getService( 'Test' );
203
204 MediaWikiServices::resetChildProcessServices();
205 $finalServices = MediaWikiServices::getInstance();
206
207 $newTestService = $finalServices->getService( 'Test' );
208 $this->assertNotSame( $oldTestService, $newTestService );
209
210 MediaWikiServices::forceGlobalInstance( $oldServices );
211 }
212
213 public function testResetServiceForTesting() {
215 $serviceCounter = 0;
216
217 $services->defineService(
218 'Test',
219 function() use ( &$serviceCounter ) {
220 $serviceCounter++;
221 $service = $this->getMock( 'MediaWiki\Services\DestructibleService' );
222 $service->expects( $this->once() )->method( 'destroy' );
223 return $service;
224 }
225 );
226
227 // This should do nothing. In particular, it should not create a service instance.
228 $services->resetServiceForTesting( 'Test' );
229 $this->assertEquals( 0, $serviceCounter, 'No service instance should be created yet.' );
230
231 $oldInstance = $services->getService( 'Test' );
232 $this->assertEquals( 1, $serviceCounter, 'A service instance should exit now.' );
233
234 // The old instance should be detached, and destroy() called.
235 $services->resetServiceForTesting( 'Test' );
236 $newInstance = $services->getService( 'Test' );
237
238 $this->assertNotSame( $oldInstance, $newInstance );
239
240 // Satisfy the expectation that destroy() is called also for the second service instance.
241 $newInstance->destroy();
242 }
243
246
247 $services->defineService(
248 'Test',
249 function() {
250 $service = $this->getMock( 'MediaWiki\Services\DestructibleService' );
251 $service->expects( $this->never() )->method( 'destroy' );
252 return $service;
253 }
254 );
255
256 $oldInstance = $services->getService( 'Test' );
257
258 // The old instance should be detached, but destroy() not called.
259 $services->resetServiceForTesting( 'Test', false );
260 $newInstance = $services->getService( 'Test' );
261
262 $this->assertNotSame( $oldInstance, $newInstance );
263 }
264
265 public function provideGetters() {
266 $getServiceCases = $this->provideGetService();
267 $getterCases = [];
268
269 // All getters should be named just like the service, with "get" added.
270 foreach ( $getServiceCases as $name => $case ) {
271 if ( $name[0] === '_' ) {
272 // Internal service, no getter
273 continue;
274 }
275 list( $service, $class ) = $case;
276 $getterCases[$name] = [
277 'get' . $service,
278 $class,
279 ];
280 }
281
282 return $getterCases;
283 }
284
288 public function testGetters( $getter, $type ) {
289 // Test against the default instance, since the dummy will not know the default services.
290 $services = MediaWikiServices::getInstance();
291 $service = $services->$getter();
292 $this->assertInstanceOf( $type, $service );
293 }
294
295 public function provideGetService() {
296 // NOTE: This should list all service getters defined in ServiceWiring.php.
297 // NOTE: For every test case defined here there should be a corresponding
298 // test case defined in provideGetters().
299 return [
300 'BootstrapConfig' => [ 'BootstrapConfig', Config::class ],
301 'ConfigFactory' => [ 'ConfigFactory', ConfigFactory::class ],
302 'MainConfig' => [ 'MainConfig', Config::class ],
303 'SiteStore' => [ 'SiteStore', SiteStore::class ],
304 'SiteLookup' => [ 'SiteLookup', SiteLookup::class ],
305 'StatsdDataFactory' => [ 'StatsdDataFactory', StatsdDataFactory::class ],
306 'InterwikiLookup' => [ 'InterwikiLookup', InterwikiLookup::class ],
307 'EventRelayerGroup' => [ 'EventRelayerGroup', EventRelayerGroup::class ],
308 'SearchEngineFactory' => [ 'SearchEngineFactory', SearchEngineFactory::class ],
309 'SearchEngineConfig' => [ 'SearchEngineConfig', SearchEngineConfig::class ],
310 'SkinFactory' => [ 'SkinFactory', SkinFactory::class ],
311 'DBLoadBalancerFactory' => [ 'DBLoadBalancerFactory', 'LBFactory' ],
312 'DBLoadBalancer' => [ 'DBLoadBalancer', 'LoadBalancer' ],
313 'WatchedItemStore' => [ 'WatchedItemStore', WatchedItemStore::class ],
314 'WatchedItemQueryService' => [ 'WatchedItemQueryService', WatchedItemQueryService::class ],
315 'CryptRand' => [ 'CryptRand', CryptRand::class ],
316 'CryptHKDF' => [ 'CryptHKDF', CryptHKDF::class ],
317 'MediaHandlerFactory' => [ 'MediaHandlerFactory', MediaHandlerFactory::class ],
318 'GenderCache' => [ 'GenderCache', GenderCache::class ],
319 'LinkCache' => [ 'LinkCache', LinkCache::class ],
320 'LinkRenderer' => [ 'LinkRenderer', LinkRenderer::class ],
321 'LinkRendererFactory' => [ 'LinkRendererFactory', LinkRendererFactory::class ],
322 '_MediaWikiTitleCodec' => [ '_MediaWikiTitleCodec', MediaWikiTitleCodec::class ],
323 'MimeAnalyzer' => [ 'MimeAnalyzer', MimeAnalyzer::class ],
324 'TitleFormatter' => [ 'TitleFormatter', TitleFormatter::class ],
325 'TitleParser' => [ 'TitleParser', TitleParser::class ],
326 'ProxyLookup' => [ 'ProxyLookup', ProxyLookup::class ],
327 'MainObjectStash' => [ 'MainObjectStash', BagOStuff::class ],
328 'MainWANObjectCache' => [ 'MainWANObjectCache', WANObjectCache::class ],
329 'LocalServerObjectCache' => [ 'LocalServerObjectCache', BagOStuff::class ],
330 'VirtualRESTServiceClient' => [ 'VirtualRESTServiceClient', VirtualRESTServiceClient::class ]
331 ];
332 }
333
337 public function testGetService( $name, $type ) {
338 // Test against the default instance, since the dummy will not know the default services.
339 $services = MediaWikiServices::getInstance();
340
341 $service = $services->getService( $name );
342 $this->assertInstanceOf( $type, $service );
343 }
344
346 // Check all services in the default instance, not a dummy instance!
347 // Note that we instantiate all services here, including any that
348 // were registered by extensions.
349 $services = MediaWikiServices::getInstance();
350 $names = $services->getServiceNames();
351
352 foreach ( $names as $name ) {
353 $this->assertTrue( $services->hasService( $name ) );
354 $service = $services->getService( $name );
355 $this->assertInternalType( 'object', $service );
356 }
357 }
358
359}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
Accesses configuration settings from $GLOBALS.
A Config instance which stores all settings as a member variable.
MediaWiki\MediaWikiServices.
testGetters( $getter, $type)
provideGetters
testGetService( $name, $type)
provideGetService
newMediaWikiServices(Config $config=null)
Factory to create LinkRender objects.
Class that generates HTML links for pages.
MediaWikiServices is the service locator for the application scope of MediaWiki.
Exception thrown when trying to access a disabled service.
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition deferred.txt:11
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
$lbFactory
namespace are movable Hooks may change this value to override the return value of MWNamespace::isMovable(). 'NewDifferenceEngine' do that in ParserLimitReportFormat instead use this to modify the parameters of the image and a DIV can begin in one section and end in another Make sure your code can handle that case gracefully See the EditSectionClearerLink extension for an example zero but section is usually empty its values are the globals values before the output is cached one of or reset my talk my contributions etc etc otherwise the built in rate limiting checks are if enabled allows for interception of redirect as a string mapping parameter names to values & $type
Definition hooks.txt:2568
static configuration should be added through ResourceLoaderGetConfigVars instead can be used to get the real title after the basic globals have been set but before ordinary actions take place or wrap services the preferred way to define a new service is the $wgServiceWiringFiles array $services
Definition hooks.txt:2207
Allows to change the fields on the form that will be generated $name
Definition hooks.txt:304
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37
Interface for configuration instances.
Definition Config.php:28
Service interface for looking up Interwiki records.
DestructibleService defines a standard interface for shutting down a service instance.
SalvageableService defines an interface for services that are able to salvage state from a previous i...