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();
52 $oldServices = MediaWikiServices::forceGlobalInstance( $newServices );
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 );
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 );
104 $service1->expects( $this->never() )
105 ->method(
'salvage' );
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 );
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 );
181 $service1->expects( $this->once() )
182 ->method(
'destroy' );
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 ) {
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();
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();
318 $this->assertInstanceOf(
$type, $service );
325 $services = MediaWikiServices::getInstance();
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'
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' );