3 use MediaWiki\MediaWikiServices
;
4 use MediaWiki\Services\DestructibleService
;
5 use MediaWiki\Services\SalvageableService
;
6 use MediaWiki\Services\ServiceDisabledException
;
9 * @covers MediaWiki\MediaWikiServices
13 class MediaWikiServicesTest
extends MediaWikiTestCase
{
14 private $deprecatedServices = [ 'CryptRand' ];
19 private function newTestConfig() {
20 $globalConfig = new GlobalVarConfig();
22 $testConfig = new HashConfig();
23 $testConfig->set( 'ServiceWiringFiles', $globalConfig->get( 'ServiceWiringFiles' ) );
24 $testConfig->set( 'ConfigRegistry', $globalConfig->get( 'ConfigRegistry' ) );
30 * @return MediaWikiServices
32 private function newMediaWikiServices( Config
$config = null ) {
33 if ( $config === null ) {
34 $config = $this->newTestConfig();
37 $instance = new MediaWikiServices( $config );
39 // Load the default wiring from the specified files.
40 $wiringFiles = $config->get( 'ServiceWiringFiles' );
41 $instance->loadWiringFiles( $wiringFiles );
46 public function testGetInstance() {
47 $services = MediaWikiServices
::getInstance();
48 $this->assertInstanceOf( MediaWikiServices
::class, $services );
51 public function testForceGlobalInstance() {
52 $newServices = $this->newMediaWikiServices();
53 $oldServices = MediaWikiServices
::forceGlobalInstance( $newServices );
55 $this->assertInstanceOf( MediaWikiServices
::class, $oldServices );
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 );
67 public function testResetGlobalInstance() {
68 $newServices = $this->newMediaWikiServices();
69 $oldServices = MediaWikiServices
::forceGlobalInstance( $newServices );
71 $service1 = $this->createMock( SalvageableService
::class );
72 $service1->expects( $this->never() )
73 ->method( 'salvage' );
75 $newServices->defineService(
77 function () use ( $service1 ) {
82 // force instantiation
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 );
100 public function testResetGlobalInstance_quick() {
101 $newServices = $this->newMediaWikiServices();
102 $oldServices = MediaWikiServices
::forceGlobalInstance( $newServices );
104 $service1 = $this->createMock( SalvageableService
::class );
105 $service1->expects( $this->never() )
106 ->method( 'salvage' );
108 $service2 = $this->createMock( SalvageableService
::class );
109 $service2->expects( $this->once() )
110 ->method( 'salvage' )
113 // sequence of values the instantiator will return
114 $instantiatorReturnValues = [
119 $newServices->defineService(
121 function () use ( &$instantiatorReturnValues ) {
122 return array_shift( $instantiatorReturnValues );
126 // force instantiation
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 );
140 public function testDisableStorageBackend() {
141 $newServices = $this->newMediaWikiServices();
142 $oldServices = MediaWikiServices
::forceGlobalInstance( $newServices );
144 $lbFactory = $this->getMockBuilder( \Wikimedia\Rdbms\LBFactorySimple
::class )
145 ->disableOriginalConstructor()
148 $newServices->redefineService(
149 'DBLoadBalancerFactory',
150 function () use ( $lbFactory ) {
155 // force the service to become active, so we can check that it does get destroyed
156 $newServices->getService( 'DBLoadBalancerFactory' );
158 MediaWikiServices
::disableStorageBackend(); // should destroy DBLoadBalancerFactory
161 MediaWikiServices
::getInstance()->getService( 'DBLoadBalancerFactory' );
162 $this->fail( 'DBLoadBalancerFactory should have been disabled' );
164 catch ( ServiceDisabledException
$ex ) {
166 } catch ( Throwable
$ex ) {
167 $this->fail( 'ServiceDisabledException expected, caught ' . get_class( $ex ) );
170 MediaWikiServices
::forceGlobalInstance( $oldServices );
171 $newServices->destroy();
173 // No exception was thrown, avoid being risky
174 $this->assertTrue( true );
177 public function testResetChildProcessServices() {
178 $newServices = $this->newMediaWikiServices();
179 $oldServices = MediaWikiServices
::forceGlobalInstance( $newServices );
181 $service1 = $this->createMock( DestructibleService
::class );
182 $service1->expects( $this->once() )
183 ->method( 'destroy' );
185 $service2 = $this->createMock( DestructibleService
::class );
186 $service2->expects( $this->never() )
187 ->method( 'destroy' );
189 // sequence of values the instantiator will return
190 $instantiatorReturnValues = [
195 $newServices->defineService(
197 function () use ( &$instantiatorReturnValues ) {
198 return array_shift( $instantiatorReturnValues );
202 // force the service to become active, so we can check that it does get destroyed
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 );
214 public function testResetServiceForTesting() {
215 $services = $this->newMediaWikiServices();
218 $services->defineService(
220 function () use ( &$serviceCounter ) {
222 $service = $this->createMock( MediaWiki\Services\DestructibleService
::class );
223 $service->expects( $this->once() )->method( 'destroy' );
228 // This should do nothing. In particular, it should not create a service instance.
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.' );
235 // The old instance should be detached, and destroy() called.
236 $services->resetServiceForTesting( 'Test' );
237 $newInstance = $services->getService( 'Test' );
239 $this->assertNotSame( $oldInstance, $newInstance );
241 // Satisfy the expectation that destroy() is called also for the second service instance.
242 $newInstance->destroy();
245 public function testResetServiceForTesting_noDestroy() {
246 $services = $this->newMediaWikiServices();
248 $services->defineService(
251 $service = $this->createMock( MediaWiki\Services\DestructibleService
::class );
252 $service->expects( $this->never() )->method( 'destroy' );
257 $oldInstance = $services->getService( 'Test' );
259 // The old instance should be detached, but destroy() not called.
260 $services->resetServiceForTesting( 'Test', false );
261 $newInstance = $services->getService( 'Test' );
263 $this->assertNotSame( $oldInstance, $newInstance );
266 public function provideGetters() {
267 $getServiceCases = $this->provideGetService();
270 // All getters should be named just like the service, with "get" added.
271 foreach ( $getServiceCases as $name => $case ) {
272 if ( $name[0] === '_' ) {
273 // Internal service, no getter
276 list( $service, $class ) = $case;
277 $getterCases[$name] = [
280 in_array( $service, $this->deprecatedServices
)
288 * @dataProvider provideGetters
290 public function testGetters( $getter, $type, $isDeprecated = false ) {
291 if ( $isDeprecated ) {
292 $this->hideDeprecated( MediaWikiServices
::class . "::$getter" );
295 // Test against the default instance, since the dummy will not know the default services.
296 $services = MediaWikiServices
::getInstance();
297 $service = $services->$getter();
298 $this->assertInstanceOf( $type, $service );
301 public function provideGetService() {
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" );
311 $ret[$name] = [ $name, $fun->getReturnType()->__toString() ];
317 * @dataProvider provideGetService
319 public function testGetService( $name, $type ) {
320 // Test against the default instance, since the dummy will not know the default services.
321 $services = MediaWikiServices
::getInstance();
323 $service = $services->getService( $name );
324 $this->assertInstanceOf( $type, $service );
327 public function testDefaultServiceInstantiation() {
328 // Check all services in the default instance, not a dummy instance!
329 // Note that we instantiate all services here, including any that
330 // were registered by extensions.
331 $services = MediaWikiServices
::getInstance();
332 $names = $services->getServiceNames();
334 foreach ( $names as $name ) {
335 $this->assertTrue( $services->hasService( $name ) );
336 $service = $services->getService( $name );
337 $this->assertInternalType( 'object', $service );
341 public function testDefaultServiceWiringServicesHaveTests() {
343 $testedServices = array_keys( $this->provideGetService() );
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'
352 public function testGettersAreSorted() {
353 $methods = ( new ReflectionClass( MediaWikiServices
::class ) )
354 ->getMethods( ReflectionMethod
::IS_STATIC | ReflectionMethod
::IS_PUBLIC
);
356 $names = array_map( function ( $method ) {
357 return $method->getName();
359 $serviceNames = array_map( function ( $name ) {
361 }, array_keys( $this->provideGetService() ) );
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' );