3 use Mediawiki\Http\HttpRequestFactory
;
4 use MediaWiki\Interwiki\InterwikiLookup
;
5 use MediaWiki\Linker\LinkRenderer
;
6 use MediaWiki\Linker\LinkRendererFactory
;
7 use MediaWiki\MediaWikiServices
;
8 use MediaWiki\Services\DestructibleService
;
9 use MediaWiki\Services\SalvageableService
;
10 use MediaWiki\Services\ServiceDisabledException
;
11 use MediaWiki\Shell\CommandFactory
;
12 use MediaWiki\Storage\BlobStore
;
13 use MediaWiki\Storage\BlobStoreFactory
;
14 use MediaWiki\Storage\NameTableStore
;
15 use MediaWiki\Storage\RevisionLookup
;
16 use MediaWiki\Storage\RevisionStore
;
17 use MediaWiki\Storage\RevisionStoreFactory
;
18 use MediaWiki\Storage\SqlBlobStore
;
21 * @covers MediaWiki\MediaWikiServices
25 class MediaWikiServicesTest
extends MediaWikiTestCase
{
30 private function newTestConfig() {
31 $globalConfig = new GlobalVarConfig();
33 $testConfig = new HashConfig();
34 $testConfig->set( 'ServiceWiringFiles', $globalConfig->get( 'ServiceWiringFiles' ) );
35 $testConfig->set( 'ConfigRegistry', $globalConfig->get( 'ConfigRegistry' ) );
41 * @return MediaWikiServices
43 private function newMediaWikiServices( Config
$config = null ) {
44 if ( $config === null ) {
45 $config = $this->newTestConfig();
48 $instance = new MediaWikiServices( $config );
50 // Load the default wiring from the specified files.
51 $wiringFiles = $config->get( 'ServiceWiringFiles' );
52 $instance->loadWiringFiles( $wiringFiles );
57 public function testGetInstance() {
58 $services = MediaWikiServices
::getInstance();
59 $this->assertInstanceOf( MediaWikiServices
::class, $services );
62 public function testForceGlobalInstance() {
63 $newServices = $this->newMediaWikiServices();
64 $oldServices = MediaWikiServices
::forceGlobalInstance( $newServices );
66 $this->assertInstanceOf( MediaWikiServices
::class, $oldServices );
67 $this->assertNotSame( $oldServices, $newServices );
69 $theServices = MediaWikiServices
::getInstance();
70 $this->assertSame( $theServices, $newServices );
72 MediaWikiServices
::forceGlobalInstance( $oldServices );
74 $theServices = MediaWikiServices
::getInstance();
75 $this->assertSame( $theServices, $oldServices );
78 public function testResetGlobalInstance() {
79 $newServices = $this->newMediaWikiServices();
80 $oldServices = MediaWikiServices
::forceGlobalInstance( $newServices );
82 $service1 = $this->createMock( SalvageableService
::class );
83 $service1->expects( $this->never() )
84 ->method( 'salvage' );
86 $newServices->defineService(
88 function () use ( $service1 ) {
93 // force instantiation
94 $newServices->getService( 'Test' );
96 MediaWikiServices
::resetGlobalInstance( $this->newTestConfig() );
97 $theServices = MediaWikiServices
::getInstance();
101 $theServices->getService( 'Test' ),
102 'service definition should survive reset'
105 $this->assertNotSame( $theServices, $newServices );
106 $this->assertNotSame( $theServices, $oldServices );
108 MediaWikiServices
::forceGlobalInstance( $oldServices );
111 public function testResetGlobalInstance_quick() {
112 $newServices = $this->newMediaWikiServices();
113 $oldServices = MediaWikiServices
::forceGlobalInstance( $newServices );
115 $service1 = $this->createMock( SalvageableService
::class );
116 $service1->expects( $this->never() )
117 ->method( 'salvage' );
119 $service2 = $this->createMock( SalvageableService
::class );
120 $service2->expects( $this->once() )
121 ->method( 'salvage' )
124 // sequence of values the instantiator will return
125 $instantiatorReturnValues = [
130 $newServices->defineService(
132 function () use ( &$instantiatorReturnValues ) {
133 return array_shift( $instantiatorReturnValues );
137 // force instantiation
138 $newServices->getService( 'Test' );
140 MediaWikiServices
::resetGlobalInstance( $this->newTestConfig(), 'quick' );
141 $theServices = MediaWikiServices
::getInstance();
143 $this->assertSame( $service2, $theServices->getService( 'Test' ) );
145 $this->assertNotSame( $theServices, $newServices );
146 $this->assertNotSame( $theServices, $oldServices );
148 MediaWikiServices
::forceGlobalInstance( $oldServices );
151 public function testDisableStorageBackend() {
152 $newServices = $this->newMediaWikiServices();
153 $oldServices = MediaWikiServices
::forceGlobalInstance( $newServices );
155 $lbFactory = $this->getMockBuilder( \Wikimedia\Rdbms\LBFactorySimple
::class )
156 ->disableOriginalConstructor()
159 $newServices->redefineService(
160 'DBLoadBalancerFactory',
161 function () use ( $lbFactory ) {
166 // force the service to become active, so we can check that it does get destroyed
167 $newServices->getService( 'DBLoadBalancerFactory' );
169 MediaWikiServices
::disableStorageBackend(); // should destroy DBLoadBalancerFactory
172 MediaWikiServices
::getInstance()->getService( 'DBLoadBalancerFactory' );
173 $this->fail( 'DBLoadBalancerFactory should have been disabled' );
175 catch ( ServiceDisabledException
$ex ) {
177 } catch ( Throwable
$ex ) {
178 $this->fail( 'ServiceDisabledException expected, caught ' . get_class( $ex ) );
181 MediaWikiServices
::forceGlobalInstance( $oldServices );
182 $newServices->destroy();
184 // No exception was thrown, avoid being risky
185 $this->assertTrue( true );
188 public function testResetChildProcessServices() {
189 $newServices = $this->newMediaWikiServices();
190 $oldServices = MediaWikiServices
::forceGlobalInstance( $newServices );
192 $service1 = $this->createMock( DestructibleService
::class );
193 $service1->expects( $this->once() )
194 ->method( 'destroy' );
196 $service2 = $this->createMock( DestructibleService
::class );
197 $service2->expects( $this->never() )
198 ->method( 'destroy' );
200 // sequence of values the instantiator will return
201 $instantiatorReturnValues = [
206 $newServices->defineService(
208 function () use ( &$instantiatorReturnValues ) {
209 return array_shift( $instantiatorReturnValues );
213 // force the service to become active, so we can check that it does get destroyed
214 $oldTestService = $newServices->getService( 'Test' );
216 MediaWikiServices
::resetChildProcessServices();
217 $finalServices = MediaWikiServices
::getInstance();
219 $newTestService = $finalServices->getService( 'Test' );
220 $this->assertNotSame( $oldTestService, $newTestService );
222 MediaWikiServices
::forceGlobalInstance( $oldServices );
225 public function testResetServiceForTesting() {
226 $services = $this->newMediaWikiServices();
229 $services->defineService(
231 function () use ( &$serviceCounter ) {
233 $service = $this->createMock( MediaWiki\Services\DestructibleService
::class );
234 $service->expects( $this->once() )->method( 'destroy' );
239 // This should do nothing. In particular, it should not create a service instance.
240 $services->resetServiceForTesting( 'Test' );
241 $this->assertEquals( 0, $serviceCounter, 'No service instance should be created yet.' );
243 $oldInstance = $services->getService( 'Test' );
244 $this->assertEquals( 1, $serviceCounter, 'A service instance should exit now.' );
246 // The old instance should be detached, and destroy() called.
247 $services->resetServiceForTesting( 'Test' );
248 $newInstance = $services->getService( 'Test' );
250 $this->assertNotSame( $oldInstance, $newInstance );
252 // Satisfy the expectation that destroy() is called also for the second service instance.
253 $newInstance->destroy();
256 public function testResetServiceForTesting_noDestroy() {
257 $services = $this->newMediaWikiServices();
259 $services->defineService(
262 $service = $this->createMock( MediaWiki\Services\DestructibleService
::class );
263 $service->expects( $this->never() )->method( 'destroy' );
268 $oldInstance = $services->getService( 'Test' );
270 // The old instance should be detached, but destroy() not called.
271 $services->resetServiceForTesting( 'Test', false );
272 $newInstance = $services->getService( 'Test' );
274 $this->assertNotSame( $oldInstance, $newInstance );
277 public function provideGetters() {
278 $getServiceCases = $this->provideGetService();
281 // All getters should be named just like the service, with "get" added.
282 foreach ( $getServiceCases as $name => $case ) {
283 if ( $name[0] === '_' ) {
284 // Internal service, no getter
287 list( $service, $class ) = $case;
288 $getterCases[$name] = [
298 * @dataProvider provideGetters
300 public function testGetters( $getter, $type ) {
301 // Test against the default instance, since the dummy will not know the default services.
302 $services = MediaWikiServices
::getInstance();
303 $service = $services->$getter();
304 $this->assertInstanceOf( $type, $service );
307 public function provideGetService() {
308 // NOTE: This should list all service getters defined in ServiceWiring.php.
310 'BootstrapConfig' => [ 'BootstrapConfig', Config
::class ],
311 'ConfigFactory' => [ 'ConfigFactory', ConfigFactory
::class ],
312 'MainConfig' => [ 'MainConfig', Config
::class ],
313 'SiteStore' => [ 'SiteStore', SiteStore
::class ],
314 'SiteLookup' => [ 'SiteLookup', SiteLookup
::class ],
315 'StatsdDataFactory' => [ 'StatsdDataFactory', IBufferingStatsdDataFactory
::class ],
316 'InterwikiLookup' => [ 'InterwikiLookup', InterwikiLookup
::class ],
317 'EventRelayerGroup' => [ 'EventRelayerGroup', EventRelayerGroup
::class ],
318 'SearchEngineFactory' => [ 'SearchEngineFactory', SearchEngineFactory
::class ],
319 'SearchEngineConfig' => [ 'SearchEngineConfig', SearchEngineConfig
::class ],
320 'SkinFactory' => [ 'SkinFactory', SkinFactory
::class ],
321 'DBLoadBalancerFactory' => [ 'DBLoadBalancerFactory', Wikimedia\Rdbms\LBFactory
::class ],
322 'DBLoadBalancer' => [ 'DBLoadBalancer', Wikimedia\Rdbms\LoadBalancer
::class ],
323 'WatchedItemStore' => [ 'WatchedItemStore', WatchedItemStore
::class ],
324 'WatchedItemQueryService' => [ 'WatchedItemQueryService', WatchedItemQueryService
::class ],
325 'CryptRand' => [ 'CryptRand', CryptRand
::class ],
326 'CryptHKDF' => [ 'CryptHKDF', CryptHKDF
::class ],
327 'MediaHandlerFactory' => [ 'MediaHandlerFactory', MediaHandlerFactory
::class ],
328 'Parser' => [ 'Parser', Parser
::class ],
329 'ParserCache' => [ 'ParserCache', ParserCache
::class ],
330 'GenderCache' => [ 'GenderCache', GenderCache
::class ],
331 'LinkCache' => [ 'LinkCache', LinkCache
::class ],
332 'LinkRenderer' => [ 'LinkRenderer', LinkRenderer
::class ],
333 'LinkRendererFactory' => [ 'LinkRendererFactory', LinkRendererFactory
::class ],
334 '_MediaWikiTitleCodec' => [ '_MediaWikiTitleCodec', MediaWikiTitleCodec
::class ],
335 'MimeAnalyzer' => [ 'MimeAnalyzer', MimeAnalyzer
::class ],
336 'TitleFormatter' => [ 'TitleFormatter', TitleFormatter
::class ],
337 'TitleParser' => [ 'TitleParser', TitleParser
::class ],
338 'ProxyLookup' => [ 'ProxyLookup', ProxyLookup
::class ],
339 'MainObjectStash' => [ 'MainObjectStash', BagOStuff
::class ],
340 'MainWANObjectCache' => [ 'MainWANObjectCache', WANObjectCache
::class ],
341 'LocalServerObjectCache' => [ 'LocalServerObjectCache', BagOStuff
::class ],
342 'VirtualRESTServiceClient' => [ 'VirtualRESTServiceClient', VirtualRESTServiceClient
::class ],
343 'ShellCommandFactory' => [ 'ShellCommandFactory', CommandFactory
::class ],
344 'BlobStoreFactory' => [ 'BlobStoreFactory', BlobStoreFactory
::class ],
345 'BlobStore' => [ 'BlobStore', BlobStore
::class ],
346 '_SqlBlobStore' => [ '_SqlBlobStore', SqlBlobStore
::class ],
347 'RevisionStore' => [ 'RevisionStore', RevisionStore
::class ],
348 'RevisionStoreFactory' => [ 'RevisionStoreFactory', RevisionStoreFactory
::class ],
349 'RevisionLookup' => [ 'RevisionLookup', RevisionLookup
::class ],
350 'HttpRequestFactory' => [ 'HttpRequestFactory', HttpRequestFactory
::class ],
351 'CommentStore' => [ 'CommentStore', CommentStore
::class ],
352 'ChangeTagDefStore' => [ 'ChangeTagDefStore', NameTableStore
::class ],
357 * @dataProvider provideGetService
359 public function testGetService( $name, $type ) {
360 // Test against the default instance, since the dummy will not know the default services.
361 $services = MediaWikiServices
::getInstance();
363 $service = $services->getService( $name );
364 $this->assertInstanceOf( $type, $service );
367 public function testDefaultServiceInstantiation() {
368 // Check all services in the default instance, not a dummy instance!
369 // Note that we instantiate all services here, including any that
370 // were registered by extensions.
371 $services = MediaWikiServices
::getInstance();
372 $names = $services->getServiceNames();
374 foreach ( $names as $name ) {
375 $this->assertTrue( $services->hasService( $name ) );
376 $service = $services->getService( $name );
377 $this->assertInternalType( 'object', $service );