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\SqlBlobStore
;
20 * @covers MediaWiki\MediaWikiServices
24 class MediaWikiServicesTest
extends MediaWikiTestCase
{
29 private function newTestConfig() {
30 $globalConfig = new GlobalVarConfig();
32 $testConfig = new HashConfig();
33 $testConfig->set( 'ServiceWiringFiles', $globalConfig->get( 'ServiceWiringFiles' ) );
34 $testConfig->set( 'ConfigRegistry', $globalConfig->get( 'ConfigRegistry' ) );
40 * @return MediaWikiServices
42 private function newMediaWikiServices( Config
$config = null ) {
43 if ( $config === null ) {
44 $config = $this->newTestConfig();
47 $instance = new MediaWikiServices( $config );
49 // Load the default wiring from the specified files.
50 $wiringFiles = $config->get( 'ServiceWiringFiles' );
51 $instance->loadWiringFiles( $wiringFiles );
56 public function testGetInstance() {
57 $services = MediaWikiServices
::getInstance();
58 $this->assertInstanceOf( MediaWikiServices
::class, $services );
61 public function testForceGlobalInstance() {
62 $newServices = $this->newMediaWikiServices();
63 $oldServices = MediaWikiServices
::forceGlobalInstance( $newServices );
65 $this->assertInstanceOf( MediaWikiServices
::class, $oldServices );
66 $this->assertNotSame( $oldServices, $newServices );
68 $theServices = MediaWikiServices
::getInstance();
69 $this->assertSame( $theServices, $newServices );
71 MediaWikiServices
::forceGlobalInstance( $oldServices );
73 $theServices = MediaWikiServices
::getInstance();
74 $this->assertSame( $theServices, $oldServices );
77 public function testResetGlobalInstance() {
78 $newServices = $this->newMediaWikiServices();
79 $oldServices = MediaWikiServices
::forceGlobalInstance( $newServices );
81 $service1 = $this->createMock( SalvageableService
::class );
82 $service1->expects( $this->never() )
83 ->method( 'salvage' );
85 $newServices->defineService(
87 function () use ( $service1 ) {
92 // force instantiation
93 $newServices->getService( 'Test' );
95 MediaWikiServices
::resetGlobalInstance( $this->newTestConfig() );
96 $theServices = MediaWikiServices
::getInstance();
100 $theServices->getService( 'Test' ),
101 'service definition should survive reset'
104 $this->assertNotSame( $theServices, $newServices );
105 $this->assertNotSame( $theServices, $oldServices );
107 MediaWikiServices
::forceGlobalInstance( $oldServices );
110 public function testResetGlobalInstance_quick() {
111 $newServices = $this->newMediaWikiServices();
112 $oldServices = MediaWikiServices
::forceGlobalInstance( $newServices );
114 $service1 = $this->createMock( SalvageableService
::class );
115 $service1->expects( $this->never() )
116 ->method( 'salvage' );
118 $service2 = $this->createMock( SalvageableService
::class );
119 $service2->expects( $this->once() )
120 ->method( 'salvage' )
123 // sequence of values the instantiator will return
124 $instantiatorReturnValues = [
129 $newServices->defineService(
131 function () use ( &$instantiatorReturnValues ) {
132 return array_shift( $instantiatorReturnValues );
136 // force instantiation
137 $newServices->getService( 'Test' );
139 MediaWikiServices
::resetGlobalInstance( $this->newTestConfig(), 'quick' );
140 $theServices = MediaWikiServices
::getInstance();
142 $this->assertSame( $service2, $theServices->getService( 'Test' ) );
144 $this->assertNotSame( $theServices, $newServices );
145 $this->assertNotSame( $theServices, $oldServices );
147 MediaWikiServices
::forceGlobalInstance( $oldServices );
150 public function testDisableStorageBackend() {
151 $newServices = $this->newMediaWikiServices();
152 $oldServices = MediaWikiServices
::forceGlobalInstance( $newServices );
154 $lbFactory = $this->getMockBuilder( \Wikimedia\Rdbms\LBFactorySimple
::class )
155 ->disableOriginalConstructor()
158 $newServices->redefineService(
159 'DBLoadBalancerFactory',
160 function () use ( $lbFactory ) {
165 // force the service to become active, so we can check that it does get destroyed
166 $newServices->getService( 'DBLoadBalancerFactory' );
168 MediaWikiServices
::disableStorageBackend(); // should destroy DBLoadBalancerFactory
171 MediaWikiServices
::getInstance()->getService( 'DBLoadBalancerFactory' );
172 $this->fail( 'DBLoadBalancerFactory should have been disabled' );
174 catch ( ServiceDisabledException
$ex ) {
176 } catch ( Throwable
$ex ) {
177 $this->fail( 'ServiceDisabledException expected, caught ' . get_class( $ex ) );
180 MediaWikiServices
::forceGlobalInstance( $oldServices );
181 $newServices->destroy();
183 // No exception was thrown, avoid being risky
184 $this->assertTrue( true );
187 public function testResetChildProcessServices() {
188 $newServices = $this->newMediaWikiServices();
189 $oldServices = MediaWikiServices
::forceGlobalInstance( $newServices );
191 $service1 = $this->createMock( DestructibleService
::class );
192 $service1->expects( $this->once() )
193 ->method( 'destroy' );
195 $service2 = $this->createMock( DestructibleService
::class );
196 $service2->expects( $this->never() )
197 ->method( 'destroy' );
199 // sequence of values the instantiator will return
200 $instantiatorReturnValues = [
205 $newServices->defineService(
207 function () use ( &$instantiatorReturnValues ) {
208 return array_shift( $instantiatorReturnValues );
212 // force the service to become active, so we can check that it does get destroyed
213 $oldTestService = $newServices->getService( 'Test' );
215 MediaWikiServices
::resetChildProcessServices();
216 $finalServices = MediaWikiServices
::getInstance();
218 $newTestService = $finalServices->getService( 'Test' );
219 $this->assertNotSame( $oldTestService, $newTestService );
221 MediaWikiServices
::forceGlobalInstance( $oldServices );
224 public function testResetServiceForTesting() {
225 $services = $this->newMediaWikiServices();
228 $services->defineService(
230 function () use ( &$serviceCounter ) {
232 $service = $this->createMock( MediaWiki\Services\DestructibleService
::class );
233 $service->expects( $this->once() )->method( 'destroy' );
238 // This should do nothing. In particular, it should not create a service instance.
239 $services->resetServiceForTesting( 'Test' );
240 $this->assertEquals( 0, $serviceCounter, 'No service instance should be created yet.' );
242 $oldInstance = $services->getService( 'Test' );
243 $this->assertEquals( 1, $serviceCounter, 'A service instance should exit now.' );
245 // The old instance should be detached, and destroy() called.
246 $services->resetServiceForTesting( 'Test' );
247 $newInstance = $services->getService( 'Test' );
249 $this->assertNotSame( $oldInstance, $newInstance );
251 // Satisfy the expectation that destroy() is called also for the second service instance.
252 $newInstance->destroy();
255 public function testResetServiceForTesting_noDestroy() {
256 $services = $this->newMediaWikiServices();
258 $services->defineService(
261 $service = $this->createMock( MediaWiki\Services\DestructibleService
::class );
262 $service->expects( $this->never() )->method( 'destroy' );
267 $oldInstance = $services->getService( 'Test' );
269 // The old instance should be detached, but destroy() not called.
270 $services->resetServiceForTesting( 'Test', false );
271 $newInstance = $services->getService( 'Test' );
273 $this->assertNotSame( $oldInstance, $newInstance );
276 public function provideGetters() {
277 $getServiceCases = $this->provideGetService();
280 // All getters should be named just like the service, with "get" added.
281 foreach ( $getServiceCases as $name => $case ) {
282 if ( $name[0] === '_' ) {
283 // Internal service, no getter
286 list( $service, $class ) = $case;
287 $getterCases[$name] = [
297 * @dataProvider provideGetters
299 public function testGetters( $getter, $type ) {
300 // Test against the default instance, since the dummy will not know the default services.
301 $services = MediaWikiServices
::getInstance();
302 $service = $services->$getter();
303 $this->assertInstanceOf( $type, $service );
306 public function provideGetService() {
307 // NOTE: This should list all service getters defined in ServiceWiring.php.
308 // NOTE: For every test case defined here there should be a corresponding
309 // test case defined in provideGetters().
311 'BootstrapConfig' => [ 'BootstrapConfig', Config
::class ],
312 'ConfigFactory' => [ 'ConfigFactory', ConfigFactory
::class ],
313 'MainConfig' => [ 'MainConfig', Config
::class ],
314 'SiteStore' => [ 'SiteStore', SiteStore
::class ],
315 'SiteLookup' => [ 'SiteLookup', SiteLookup
::class ],
316 'StatsdDataFactory' => [ 'StatsdDataFactory', IBufferingStatsdDataFactory
::class ],
317 'InterwikiLookup' => [ 'InterwikiLookup', InterwikiLookup
::class ],
318 'EventRelayerGroup' => [ 'EventRelayerGroup', EventRelayerGroup
::class ],
319 'SearchEngineFactory' => [ 'SearchEngineFactory', SearchEngineFactory
::class ],
320 'SearchEngineConfig' => [ 'SearchEngineConfig', SearchEngineConfig
::class ],
321 'SkinFactory' => [ 'SkinFactory', SkinFactory
::class ],
322 'DBLoadBalancerFactory' => [ 'DBLoadBalancerFactory', Wikimedia\Rdbms\LBFactory
::class ],
323 'DBLoadBalancer' => [ 'DBLoadBalancer', Wikimedia\Rdbms\LoadBalancer
::class ],
324 'WatchedItemStore' => [ 'WatchedItemStore', WatchedItemStore
::class ],
325 'WatchedItemQueryService' => [ 'WatchedItemQueryService', WatchedItemQueryService
::class ],
326 'CryptRand' => [ 'CryptRand', CryptRand
::class ],
327 'CryptHKDF' => [ 'CryptHKDF', CryptHKDF
::class ],
328 'MediaHandlerFactory' => [ 'MediaHandlerFactory', MediaHandlerFactory
::class ],
329 'Parser' => [ 'Parser', Parser
::class ],
330 'ParserCache' => [ 'ParserCache', ParserCache
::class ],
331 'GenderCache' => [ 'GenderCache', GenderCache
::class ],
332 'LinkCache' => [ 'LinkCache', LinkCache
::class ],
333 'LinkRenderer' => [ 'LinkRenderer', LinkRenderer
::class ],
334 'LinkRendererFactory' => [ 'LinkRendererFactory', LinkRendererFactory
::class ],
335 '_MediaWikiTitleCodec' => [ '_MediaWikiTitleCodec', MediaWikiTitleCodec
::class ],
336 'MimeAnalyzer' => [ 'MimeAnalyzer', MimeAnalyzer
::class ],
337 'TitleFormatter' => [ 'TitleFormatter', TitleFormatter
::class ],
338 'TitleParser' => [ 'TitleParser', TitleParser
::class ],
339 'ProxyLookup' => [ 'ProxyLookup', ProxyLookup
::class ],
340 'MainObjectStash' => [ 'MainObjectStash', BagOStuff
::class ],
341 'MainWANObjectCache' => [ 'MainWANObjectCache', WANObjectCache
::class ],
342 'LocalServerObjectCache' => [ 'LocalServerObjectCache', BagOStuff
::class ],
343 'VirtualRESTServiceClient' => [ 'VirtualRESTServiceClient', VirtualRESTServiceClient
::class ],
344 'ShellCommandFactory' => [ 'ShellCommandFactory', CommandFactory
::class ],
345 'BlobStoreFactory' => [ 'BlobStoreFactory', BlobStoreFactory
::class ],
346 'BlobStore' => [ 'BlobStore', BlobStore
::class ],
347 '_SqlBlobStore' => [ '_SqlBlobStore', SqlBlobStore
::class ],
348 'RevisionStore' => [ 'RevisionStore', RevisionStore
::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 );