2 use Liuggio\StatsdClient\Factory\StatsdDataFactory;
27 $testConfig->set(
'ServiceWiringFiles', $globalConfig->get(
'ServiceWiringFiles' ) );
28 $testConfig->set(
'ConfigRegistry', $globalConfig->get(
'ConfigRegistry' ) );
37 if ( $config ===
null ) {
44 $wiringFiles = $config->get(
'ServiceWiringFiles' );
45 $instance->loadWiringFiles( $wiringFiles );
51 $services = MediaWikiServices::getInstance();
52 $this->assertInstanceOf(
'MediaWiki\\MediaWikiServices',
$services );
57 $oldServices = MediaWikiServices::forceGlobalInstance( $newServices );
59 $this->assertInstanceOf(
'MediaWiki\\MediaWikiServices', $oldServices );
60 $this->assertNotSame( $oldServices, $newServices );
62 $theServices = MediaWikiServices::getInstance();
63 $this->assertSame( $theServices, $newServices );
65 MediaWikiServices::forceGlobalInstance( $oldServices );
67 $theServices = MediaWikiServices::getInstance();
68 $this->assertSame( $theServices, $oldServices );
73 $oldServices = MediaWikiServices::forceGlobalInstance( $newServices );
76 $service1->expects( $this->never() )
77 ->method(
'salvage' );
79 $newServices->defineService(
81 function ()
use ( $service1 ) {
87 $newServices->getService(
'Test' );
89 MediaWikiServices::resetGlobalInstance( $this->
newTestConfig() );
90 $theServices = MediaWikiServices::getInstance();
94 $theServices->getService(
'Test' ),
95 'service definition should survive reset'
98 $this->assertNotSame( $theServices, $newServices );
99 $this->assertNotSame( $theServices, $oldServices );
101 MediaWikiServices::forceGlobalInstance( $oldServices );
106 $oldServices = MediaWikiServices::forceGlobalInstance( $newServices );
109 $service1->expects( $this->never() )
110 ->method(
'salvage' );
113 $service2->expects( $this->once() )
114 ->method(
'salvage' )
118 $instantiatorReturnValues = [
123 $newServices->defineService(
125 function ()
use ( &$instantiatorReturnValues ) {
126 return array_shift( $instantiatorReturnValues );
131 $newServices->getService(
'Test' );
133 MediaWikiServices::resetGlobalInstance( $this->
newTestConfig(),
'quick' );
134 $theServices = MediaWikiServices::getInstance();
136 $this->assertSame( $service2, $theServices->getService(
'Test' ) );
138 $this->assertNotSame( $theServices, $newServices );
139 $this->assertNotSame( $theServices, $oldServices );
141 MediaWikiServices::forceGlobalInstance( $oldServices );
146 $oldServices = MediaWikiServices::forceGlobalInstance( $newServices );
148 $lbFactory = $this->getMockBuilder(
'LBFactorySimple' )
149 ->disableOriginalConstructor()
152 $newServices->redefineService(
153 'DBLoadBalancerFactory',
154 function ()
use ( $lbFactory ) {
160 $newServices->getService(
'DBLoadBalancerFactory' );
162 MediaWikiServices::disableStorageBackend();
165 MediaWikiServices::getInstance()->getService(
'DBLoadBalancerFactory' );
166 $this->fail(
'DBLoadBalancerFactory should have been disabled' );
170 }
catch ( Throwable $ex ) {
171 $this->fail(
'ServiceDisabledException expected, caught ' . get_class( $ex ) );
174 MediaWikiServices::forceGlobalInstance( $oldServices );
175 $newServices->destroy();
180 $oldServices = MediaWikiServices::forceGlobalInstance( $newServices );
183 $service1->expects( $this->once() )
184 ->method(
'destroy' );
187 $service2->expects( $this->never() )
188 ->method(
'destroy' );
191 $instantiatorReturnValues = [
196 $newServices->defineService(
198 function ()
use ( &$instantiatorReturnValues ) {
199 return array_shift( $instantiatorReturnValues );
204 $oldTestService = $newServices->getService(
'Test' );
206 MediaWikiServices::resetChildProcessServices();
207 $finalServices = MediaWikiServices::getInstance();
209 $newTestService = $finalServices->getService(
'Test' );
210 $this->assertNotSame( $oldTestService, $newTestService );
212 MediaWikiServices::forceGlobalInstance( $oldServices );
221 function ()
use ( &$serviceCounter ) {
223 $service = $this->createMock(
'MediaWiki\Services\DestructibleService' );
224 $service->expects( $this->once() )->method(
'destroy' );
230 $services->resetServiceForTesting(
'Test' );
231 $this->assertEquals( 0, $serviceCounter,
'No service instance should be created yet.' );
233 $oldInstance =
$services->getService(
'Test' );
234 $this->assertEquals( 1, $serviceCounter,
'A service instance should exit now.' );
237 $services->resetServiceForTesting(
'Test' );
238 $newInstance =
$services->getService(
'Test' );
240 $this->assertNotSame( $oldInstance, $newInstance );
243 $newInstance->destroy();
252 $service = $this->createMock(
'MediaWiki\Services\DestructibleService' );
253 $service->expects( $this->never() )->method(
'destroy' );
258 $oldInstance =
$services->getService(
'Test' );
261 $services->resetServiceForTesting(
'Test',
false );
262 $newInstance =
$services->getService(
'Test' );
264 $this->assertNotSame( $oldInstance, $newInstance );
272 foreach ( $getServiceCases
as $name => $case ) {
273 if (
$name[0] ===
'_' ) {
277 list( $service, $class ) = $case;
278 $getterCases[
$name] = [
292 $services = MediaWikiServices::getInstance();
294 $this->assertInstanceOf(
$type, $service );
314 'DBLoadBalancer' => [
'DBLoadBalancer',
'LoadBalancer' ],
344 $services = MediaWikiServices::getInstance();
347 $this->assertInstanceOf(
$type, $service );
354 $services = MediaWikiServices::getInstance();
360 $this->assertInternalType(
'object', $service );