2 use Liuggio\StatsdClient\Factory\StatsdDataFactory
;
3 use MediaWiki\Interwiki\InterwikiLookup
;
4 use MediaWiki\Linker\LinkRenderer
;
5 use MediaWiki\Linker\LinkRendererFactory
;
6 use MediaWiki\MediaWikiServices
;
7 use MediaWiki\Services\DestructibleService
;
8 use MediaWiki\Services\SalvageableService
;
9 use MediaWiki\Services\ServiceDisabledException
;
12 * @covers MediaWiki\MediaWikiServices
16 class MediaWikiServicesTest
extends MediaWikiTestCase
{
21 private function newTestConfig() {
22 $globalConfig = new GlobalVarConfig();
24 $testConfig = new HashConfig();
25 $testConfig->set( 'ServiceWiringFiles', $globalConfig->get( 'ServiceWiringFiles' ) );
26 $testConfig->set( 'ConfigRegistry', $globalConfig->get( 'ConfigRegistry' ) );
32 * @return MediaWikiServices
34 private function newMediaWikiServices( Config
$config = null ) {
35 if ( $config === null ) {
36 $config = $this->newTestConfig();
39 $instance = new MediaWikiServices( $config );
41 // Load the default wiring from the specified files.
42 $wiringFiles = $config->get( 'ServiceWiringFiles' );
43 $instance->loadWiringFiles( $wiringFiles );
48 public function testGetInstance() {
49 $services = MediaWikiServices
::getInstance();
50 $this->assertInstanceOf( 'MediaWiki\\MediaWikiServices', $services );
53 public function testForceGlobalInstance() {
54 $newServices = $this->newMediaWikiServices();
55 $oldServices = MediaWikiServices
::forceGlobalInstance( $newServices );
57 $this->assertInstanceOf( 'MediaWiki\\MediaWikiServices', $oldServices );
58 $this->assertNotSame( $oldServices, $newServices );
60 $theServices = MediaWikiServices
::getInstance();
61 $this->assertSame( $theServices, $newServices );
63 MediaWikiServices
::forceGlobalInstance( $oldServices );
65 $theServices = MediaWikiServices
::getInstance();
66 $this->assertSame( $theServices, $oldServices );
69 public function testResetGlobalInstance() {
70 $newServices = $this->newMediaWikiServices();
71 $oldServices = MediaWikiServices
::forceGlobalInstance( $newServices );
73 $service1 = $this->getMock( SalvageableService
::class );
74 $service1->expects( $this->never() )
75 ->method( 'salvage' );
77 $newServices->defineService(
79 function() use ( $service1 ) {
84 // force instantiation
85 $newServices->getService( 'Test' );
87 MediaWikiServices
::resetGlobalInstance( $this->newTestConfig() );
88 $theServices = MediaWikiServices
::getInstance();
92 $theServices->getService( 'Test' ),
93 'service definition should survive reset'
96 $this->assertNotSame( $theServices, $newServices );
97 $this->assertNotSame( $theServices, $oldServices );
99 MediaWikiServices
::forceGlobalInstance( $oldServices );
102 public function testResetGlobalInstance_quick() {
103 $newServices = $this->newMediaWikiServices();
104 $oldServices = MediaWikiServices
::forceGlobalInstance( $newServices );
106 $service1 = $this->getMock( SalvageableService
::class );
107 $service1->expects( $this->never() )
108 ->method( 'salvage' );
110 $service2 = $this->getMock( SalvageableService
::class );
111 $service2->expects( $this->once() )
112 ->method( 'salvage' )
115 // sequence of values the instantiator will return
116 $instantiatorReturnValues = [
121 $newServices->defineService(
123 function() use ( &$instantiatorReturnValues ) {
124 return array_shift( $instantiatorReturnValues );
128 // force instantiation
129 $newServices->getService( 'Test' );
131 MediaWikiServices
::resetGlobalInstance( $this->newTestConfig(), 'quick' );
132 $theServices = MediaWikiServices
::getInstance();
134 $this->assertSame( $service2, $theServices->getService( 'Test' ) );
136 $this->assertNotSame( $theServices, $newServices );
137 $this->assertNotSame( $theServices, $oldServices );
139 MediaWikiServices
::forceGlobalInstance( $oldServices );
142 public function testDisableStorageBackend() {
143 $newServices = $this->newMediaWikiServices();
144 $oldServices = MediaWikiServices
::forceGlobalInstance( $newServices );
146 $lbFactory = $this->getMockBuilder( 'LBFactorySimple' )
147 ->disableOriginalConstructor()
150 $newServices->redefineService(
151 'DBLoadBalancerFactory',
152 function() use ( $lbFactory ) {
157 // force the service to become active, so we can check that it does get destroyed
158 $newServices->getService( 'DBLoadBalancerFactory' );
160 MediaWikiServices
::disableStorageBackend(); // should destroy DBLoadBalancerFactory
163 MediaWikiServices
::getInstance()->getService( 'DBLoadBalancerFactory' );
164 $this->fail( 'DBLoadBalancerFactory should have been disabled' );
166 catch ( ServiceDisabledException
$ex ) {
168 } catch ( Throwable
$ex ) {
169 $this->fail( 'ServiceDisabledException expected, caught ' . get_class( $ex ) );
172 MediaWikiServices
::forceGlobalInstance( $oldServices );
173 $newServices->destroy();
176 public function testResetChildProcessServices() {
177 $newServices = $this->newMediaWikiServices();
178 $oldServices = MediaWikiServices
::forceGlobalInstance( $newServices );
180 $service1 = $this->getMock( DestructibleService
::class );
181 $service1->expects( $this->once() )
182 ->method( 'destroy' );
184 $service2 = $this->getMock( DestructibleService
::class );
185 $service2->expects( $this->never() )
186 ->method( 'destroy' );
188 // sequence of values the instantiator will return
189 $instantiatorReturnValues = [
194 $newServices->defineService(
196 function() use ( &$instantiatorReturnValues ) {
197 return array_shift( $instantiatorReturnValues );
201 // force the service to become active, so we can check that it does get destroyed
202 $oldTestService = $newServices->getService( 'Test' );
204 MediaWikiServices
::resetChildProcessServices();
205 $finalServices = MediaWikiServices
::getInstance();
207 $newTestService = $finalServices->getService( 'Test' );
208 $this->assertNotSame( $oldTestService, $newTestService );
210 MediaWikiServices
::forceGlobalInstance( $oldServices );
213 public function testResetServiceForTesting() {
214 $services = $this->newMediaWikiServices();
217 $services->defineService(
219 function() use ( &$serviceCounter ) {
221 $service = $this->getMock( 'MediaWiki\Services\DestructibleService' );
222 $service->expects( $this->once() )->method( 'destroy' );
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.' );
231 $oldInstance = $services->getService( 'Test' );
232 $this->assertEquals( 1, $serviceCounter, 'A service instance should exit now.' );
234 // The old instance should be detached, and destroy() called.
235 $services->resetServiceForTesting( 'Test' );
236 $newInstance = $services->getService( 'Test' );
238 $this->assertNotSame( $oldInstance, $newInstance );
240 // Satisfy the expectation that destroy() is called also for the second service instance.
241 $newInstance->destroy();
244 public function testResetServiceForTesting_noDestroy() {
245 $services = $this->newMediaWikiServices();
247 $services->defineService(
250 $service = $this->getMock( 'MediaWiki\Services\DestructibleService' );
251 $service->expects( $this->never() )->method( 'destroy' );
256 $oldInstance = $services->getService( 'Test' );
258 // The old instance should be detached, but destroy() not called.
259 $services->resetServiceForTesting( 'Test', false );
260 $newInstance = $services->getService( 'Test' );
262 $this->assertNotSame( $oldInstance, $newInstance );
265 public function provideGetters() {
266 $getServiceCases = $this->provideGetService();
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
275 list( $service, $class ) = $case;
276 $getterCases[$name] = [
286 * @dataProvider provideGetters
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 );
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().
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 'Parser' => [ 'Parser', Parser
::class ],
319 'GenderCache' => [ 'GenderCache', GenderCache
::class ],
320 'LinkCache' => [ 'LinkCache', LinkCache
::class ],
321 'LinkRenderer' => [ 'LinkRenderer', LinkRenderer
::class ],
322 'LinkRendererFactory' => [ 'LinkRendererFactory', LinkRendererFactory
::class ],
323 '_MediaWikiTitleCodec' => [ '_MediaWikiTitleCodec', MediaWikiTitleCodec
::class ],
324 'MimeAnalyzer' => [ 'MimeAnalyzer', MimeAnalyzer
::class ],
325 'TitleFormatter' => [ 'TitleFormatter', TitleFormatter
::class ],
326 'TitleParser' => [ 'TitleParser', TitleParser
::class ],
327 'ProxyLookup' => [ 'ProxyLookup', ProxyLookup
::class ],
328 'MainObjectStash' => [ 'MainObjectStash', BagOStuff
::class ],
329 'MainWANObjectCache' => [ 'MainWANObjectCache', WANObjectCache
::class ],
330 'LocalServerObjectCache' => [ 'LocalServerObjectCache', BagOStuff
::class ],
331 'VirtualRESTServiceClient' => [ 'VirtualRESTServiceClient', VirtualRESTServiceClient
::class ]
336 * @dataProvider provideGetService
338 public function testGetService( $name, $type ) {
339 // Test against the default instance, since the dummy will not know the default services.
340 $services = MediaWikiServices
::getInstance();
342 $service = $services->getService( $name );
343 $this->assertInstanceOf( $type, $service );
346 public function testDefaultServiceInstantiation() {
347 // Check all services in the default instance, not a dummy instance!
348 // Note that we instantiate all services here, including any that
349 // were registered by extensions.
350 $services = MediaWikiServices
::getInstance();
351 $names = $services->getServiceNames();
353 foreach ( $names as $name ) {
354 $this->assertTrue( $services->hasService( $name ) );
355 $service = $services->getService( $name );
356 $this->assertInternalType( 'object', $service );