23 $testConfig->set(
'ServiceWiringFiles', $globalConfig->get(
'ServiceWiringFiles' ) );
24 $testConfig->set(
'ConfigRegistry', $globalConfig->get(
'ConfigRegistry' ) );
33 if ( $config ===
null ) {
40 $wiringFiles = $config->get(
'ServiceWiringFiles' );
41 $instance->loadWiringFiles( $wiringFiles );
47 $services = MediaWikiServices::getInstance();
53 $oldServices = MediaWikiServices::forceGlobalInstance( $newServices );
56 $this->assertNotSame( $oldServices, $newServices );
58 $theServices = MediaWikiServices::getInstance();
59 $this->assertSame( $theServices, $newServices );
61 MediaWikiServices::forceGlobalInstance( $oldServices );
63 $theServices = MediaWikiServices::getInstance();
64 $this->assertSame( $theServices, $oldServices );
69 $oldServices = MediaWikiServices::forceGlobalInstance( $newServices );
72 $service1->expects( $this->never() )
73 ->method(
'salvage' );
75 $newServices->defineService(
77 function ()
use ( $service1 ) {
83 $newServices->getService(
'Test' );
85 MediaWikiServices::resetGlobalInstance( $this->
newTestConfig() );
86 $theServices = MediaWikiServices::getInstance();
90 $theServices->getService(
'Test' ),
91 'service definition should survive reset'
94 $this->assertNotSame( $theServices, $newServices );
95 $this->assertNotSame( $theServices, $oldServices );
97 MediaWikiServices::forceGlobalInstance( $oldServices );
102 $oldServices = MediaWikiServices::forceGlobalInstance( $newServices );
105 $service1->expects( $this->never() )
106 ->method(
'salvage' );
109 $service2->expects( $this->once() )
110 ->method(
'salvage' )
114 $instantiatorReturnValues = [
119 $newServices->defineService(
121 function ()
use ( &$instantiatorReturnValues ) {
122 return array_shift( $instantiatorReturnValues );
127 $newServices->getService(
'Test' );
129 MediaWikiServices::resetGlobalInstance( $this->
newTestConfig(),
'quick' );
130 $theServices = MediaWikiServices::getInstance();
132 $this->assertSame( $service2, $theServices->getService(
'Test' ) );
134 $this->assertNotSame( $theServices, $newServices );
135 $this->assertNotSame( $theServices, $oldServices );
137 MediaWikiServices::forceGlobalInstance( $oldServices );
142 $oldServices = MediaWikiServices::forceGlobalInstance( $newServices );
145 ->disableOriginalConstructor()
148 $newServices->redefineService(
149 'DBLoadBalancerFactory',
150 function ()
use ( $lbFactory ) {
156 $newServices->getService(
'DBLoadBalancerFactory' );
158 MediaWikiServices::disableStorageBackend();
161 MediaWikiServices::getInstance()->getService(
'DBLoadBalancerFactory' );
162 $this->fail(
'DBLoadBalancerFactory should have been disabled' );
166 }
catch ( Throwable $ex ) {
167 $this->fail(
'ServiceDisabledException expected, caught ' . get_class( $ex ) );
170 MediaWikiServices::forceGlobalInstance( $oldServices );
171 $newServices->destroy();
174 $this->assertTrue(
true );
179 $oldServices = MediaWikiServices::forceGlobalInstance( $newServices );
182 $service1->expects( $this->once() )
183 ->method(
'destroy' );
186 $service2->expects( $this->never() )
187 ->method(
'destroy' );
190 $instantiatorReturnValues = [
195 $newServices->defineService(
197 function ()
use ( &$instantiatorReturnValues ) {
198 return array_shift( $instantiatorReturnValues );
203 $oldTestService = $newServices->getService(
'Test' );
205 MediaWikiServices::resetChildProcessServices();
206 $finalServices = MediaWikiServices::getInstance();
208 $newTestService = $finalServices->getService(
'Test' );
209 $this->assertNotSame( $oldTestService, $newTestService );
211 MediaWikiServices::forceGlobalInstance( $oldServices );
220 function ()
use ( &$serviceCounter ) {
223 $service->expects( $this->once() )->method(
'destroy' );
229 $services->resetServiceForTesting(
'Test' );
230 $this->assertEquals( 0, $serviceCounter,
'No service instance should be created yet.' );
232 $oldInstance =
$services->getService(
'Test' );
233 $this->assertEquals( 1, $serviceCounter,
'A service instance should exit now.' );
236 $services->resetServiceForTesting(
'Test' );
237 $newInstance =
$services->getService(
'Test' );
239 $this->assertNotSame( $oldInstance, $newInstance );
242 $newInstance->destroy();
252 $service->expects( $this->never() )->method(
'destroy' );
257 $oldInstance =
$services->getService(
'Test' );
260 $services->resetServiceForTesting(
'Test',
false );
261 $newInstance =
$services->getService(
'Test' );
263 $this->assertNotSame( $oldInstance, $newInstance );
271 foreach ( $getServiceCases
as $name => $case ) {
272 if (
$name[0] ===
'_' ) {
276 list( $service, $class ) = $case;
277 $getterCases[
$name] = [
280 in_array( $service, $this->deprecatedServices )
291 if ( $isDeprecated ) {
296 $services = MediaWikiServices::getInstance();
298 $this->assertInstanceOf(
$type, $service );
303 $serviceList = require
"$IP/includes/ServiceWiring.php";
305 foreach ( $serviceList
as $name => $callback ) {
306 $fun =
new ReflectionFunction( $callback );
307 if ( !$fun->hasReturnType() ) {
308 throw new MWException(
'All service callbacks must have a return type defined, ' .
309 "none found for $name" );
321 $services = MediaWikiServices::getInstance();
324 $this->assertInstanceOf(
$type, $service );
331 $services = MediaWikiServices::getInstance();
337 $this->assertInternalType(
'object', $service );
344 $allServices = array_keys( require
"$IP/includes/ServiceWiring.php" );
347 array_diff( $allServices, $testedServices ),
348 'The following services have not been added to MediaWikiServicesTest::provideGetService'
354 ->getMethods( ReflectionMethod::IS_STATIC | ReflectionMethod::IS_PUBLIC );
356 $names = array_map(
function ( $method ) {
357 return $method->getName();
359 $serviceNames = array_map(
function (
$name ) {
362 $names = array_values( array_filter( $names,
function (
$name )
use ( $serviceNames ) {
363 return in_array(
$name, $serviceNames );
366 $sortedNames = $names;
367 sort( $sortedNames );
369 $this->assertSame( $sortedNames, $names,
370 'Please keep service getters sorted alphabetically' );