22 $testConfig->set(
'ServiceWiringFiles', $globalConfig->get(
'ServiceWiringFiles' ) );
23 $testConfig->set(
'ConfigRegistry', $globalConfig->get(
'ConfigRegistry' ) );
32 if ( $config ===
null ) {
39 $wiringFiles = $config->get(
'ServiceWiringFiles' );
40 $instance->loadWiringFiles( $wiringFiles );
46 $services = MediaWikiServices::getInstance();
47 $this->assertInstanceOf( MediaWikiServices::class,
$services );
52 $oldServices = MediaWikiServices::forceGlobalInstance( $newServices );
54 $this->assertInstanceOf( MediaWikiServices::class, $oldServices );
55 $this->assertNotSame( $oldServices, $newServices );
57 $theServices = MediaWikiServices::getInstance();
58 $this->assertSame( $theServices, $newServices );
60 MediaWikiServices::forceGlobalInstance( $oldServices );
62 $theServices = MediaWikiServices::getInstance();
63 $this->assertSame( $theServices, $oldServices );
68 $oldServices = MediaWikiServices::forceGlobalInstance( $newServices );
70 $service1 = $this->createMock( SalvageableService::class );
71 $service1->expects( $this->never() )
72 ->method(
'salvage' );
74 $newServices->defineService(
76 function () use ( $service1 ) {
82 $newServices->getService(
'Test' );
84 MediaWikiServices::resetGlobalInstance( $this->
newTestConfig() );
85 $theServices = MediaWikiServices::getInstance();
89 $theServices->getService(
'Test' ),
90 'service definition should survive reset'
93 $this->assertNotSame( $theServices, $newServices );
94 $this->assertNotSame( $theServices, $oldServices );
96 MediaWikiServices::forceGlobalInstance( $oldServices );
101 $oldServices = MediaWikiServices::forceGlobalInstance( $newServices );
103 $service1 = $this->createMock( SalvageableService::class );
104 $service1->expects( $this->never() )
105 ->method(
'salvage' );
107 $service2 = $this->createMock( SalvageableService::class );
108 $service2->expects( $this->once() )
109 ->method(
'salvage' )
113 $instantiatorReturnValues = [
118 $newServices->defineService(
120 function () use ( &$instantiatorReturnValues ) {
121 return array_shift( $instantiatorReturnValues );
126 $newServices->getService(
'Test' );
128 MediaWikiServices::resetGlobalInstance( $this->
newTestConfig(),
'quick' );
129 $theServices = MediaWikiServices::getInstance();
131 $this->assertSame( $service2, $theServices->getService(
'Test' ) );
133 $this->assertNotSame( $theServices, $newServices );
134 $this->assertNotSame( $theServices, $oldServices );
136 MediaWikiServices::forceGlobalInstance( $oldServices );
141 $oldServices = MediaWikiServices::forceGlobalInstance( $newServices );
143 $lbFactory = $this->getMockBuilder( \
Wikimedia\Rdbms\LBFactorySimple::class )
144 ->disableOriginalConstructor()
147 $newServices->redefineService(
148 'DBLoadBalancerFactory',
149 function () use ( $lbFactory ) {
155 $newServices->getService(
'DBLoadBalancerFactory' );
157 MediaWikiServices::disableStorageBackend();
160 MediaWikiServices::getInstance()->getService(
'DBLoadBalancerFactory' );
161 $this->fail(
'DBLoadBalancerFactory should have been disabled' );
165 }
catch ( Throwable $ex ) {
166 $this->fail(
'ServiceDisabledException expected, caught ' . get_class( $ex ) );
169 MediaWikiServices::forceGlobalInstance( $oldServices );
170 $newServices->destroy();
173 $this->assertTrue(
true );
178 $oldServices = MediaWikiServices::forceGlobalInstance( $newServices );
180 $service1 = $this->createMock( DestructibleService::class );
181 $service1->expects( $this->once() )
182 ->method(
'destroy' );
184 $service2 = $this->createMock( DestructibleService::class );
185 $service2->expects( $this->never() )
186 ->method(
'destroy' );
189 $instantiatorReturnValues = [
194 $newServices->defineService(
196 function () use ( &$instantiatorReturnValues ) {
197 return array_shift( $instantiatorReturnValues );
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 );
219 function () use ( &$serviceCounter ) {
221 $service = $this->createMock(
MediaWiki\Services\DestructibleService::class );
222 $service->expects( $this->once() )->method(
'destroy' );
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.' );
235 $services->resetServiceForTesting(
'Test' );
236 $newInstance =
$services->getService(
'Test' );
238 $this->assertNotSame( $oldInstance, $newInstance );
241 $newInstance->destroy();
250 $service = $this->createMock(
MediaWiki\Services\DestructibleService::class );
251 $service->expects( $this->never() )->method(
'destroy' );
256 $oldInstance =
$services->getService(
'Test' );
259 $services->resetServiceForTesting(
'Test',
false );
260 $newInstance =
$services->getService(
'Test' );
262 $this->assertNotSame( $oldInstance, $newInstance );
270 foreach ( $getServiceCases as $name => $case ) {
271 if ( $name[0] ===
'_' ) {
275 list( $service, $class ) = $case;
276 $getterCases[
$name] = [
290 $services = MediaWikiServices::getInstance();
292 $this->assertInstanceOf(
$type, $service );
297 $serviceList = require
"$IP/includes/ServiceWiring.php";
299 foreach ( $serviceList as $name => $callback ) {
300 $fun =
new ReflectionFunction( $callback );
301 if ( !$fun->hasReturnType() ) {
302 throw new MWException(
'All service callbacks must have a return type defined, ' .
303 "none found for $name" );
315 $services = MediaWikiServices::getInstance();
317 $service =
$services->getService( $name );
318 $this->assertInstanceOf(
$type, $service );
325 $services = MediaWikiServices::getInstance();
328 foreach ( $names as $name ) {
329 $this->assertTrue(
$services->hasService( $name ) );
330 $service =
$services->getService( $name );
331 $this->assertInternalType(
'object', $service );
338 $allServices = array_keys( require
"$IP/includes/ServiceWiring.php" );
341 array_diff( $allServices, $testedServices ),
342 'The following services have not been added to MediaWikiServicesTest::provideGetService'
347 $methods = (
new ReflectionClass( MediaWikiServices::class ) )
348 ->getMethods( ReflectionMethod::IS_STATIC | ReflectionMethod::IS_PUBLIC );
350 $names = array_map(
function ( $method ) {
351 return $method->getName();
353 $serviceNames = array_map(
function ( $name ) {
356 $names = array_values( array_filter( $names,
function ( $name ) use ( $serviceNames ) {
357 return in_array( $name, $serviceNames );
360 $sortedNames = $names;
361 sort( $sortedNames );
363 $this->assertSame( $sortedNames, $names,
364 'Please keep service getters sorted alphabetically' );
Accesses configuration settings from $GLOBALS.
A Config instance which stores all settings as a member variable.
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
static configuration should be added through ResourceLoaderGetConfigVars instead can be used to get the real title e g db for database replication lag or jobqueue for job queue size converted to pseudo seconds It is possible to add more fields and they will be returned to the user in the API response after the basic globals have been set but before ordinary actions take place or wrap services the preferred way to define a new service is the $wgServiceWiringFiles array $services
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses & $ret
Allows to change the fields on the form that will be generated $name
Interface for configuration instances.