MediaWiki  1.32.0
MediaWikiServicesTest.php
Go to the documentation of this file.
1 <?php
2 
7 
14 
18  private function newTestConfig() {
19  $globalConfig = new GlobalVarConfig();
20 
21  $testConfig = new HashConfig();
22  $testConfig->set( 'ServiceWiringFiles', $globalConfig->get( 'ServiceWiringFiles' ) );
23  $testConfig->set( 'ConfigRegistry', $globalConfig->get( 'ConfigRegistry' ) );
24 
25  return $testConfig;
26  }
27 
31  private function newMediaWikiServices( Config $config = null ) {
32  if ( $config === null ) {
33  $config = $this->newTestConfig();
34  }
35 
36  $instance = new MediaWikiServices( $config );
37 
38  // Load the default wiring from the specified files.
39  $wiringFiles = $config->get( 'ServiceWiringFiles' );
40  $instance->loadWiringFiles( $wiringFiles );
41 
42  return $instance;
43  }
44 
45  public function testGetInstance() {
46  $services = MediaWikiServices::getInstance();
47  $this->assertInstanceOf( MediaWikiServices::class, $services );
48  }
49 
50  public function testForceGlobalInstance() {
51  $newServices = $this->newMediaWikiServices();
52  $oldServices = MediaWikiServices::forceGlobalInstance( $newServices );
53 
54  $this->assertInstanceOf( MediaWikiServices::class, $oldServices );
55  $this->assertNotSame( $oldServices, $newServices );
56 
57  $theServices = MediaWikiServices::getInstance();
58  $this->assertSame( $theServices, $newServices );
59 
60  MediaWikiServices::forceGlobalInstance( $oldServices );
61 
62  $theServices = MediaWikiServices::getInstance();
63  $this->assertSame( $theServices, $oldServices );
64  }
65 
66  public function testResetGlobalInstance() {
67  $newServices = $this->newMediaWikiServices();
68  $oldServices = MediaWikiServices::forceGlobalInstance( $newServices );
69 
70  $service1 = $this->createMock( SalvageableService::class );
71  $service1->expects( $this->never() )
72  ->method( 'salvage' );
73 
74  $newServices->defineService(
75  'Test',
76  function () use ( $service1 ) {
77  return $service1;
78  }
79  );
80 
81  // force instantiation
82  $newServices->getService( 'Test' );
83 
84  MediaWikiServices::resetGlobalInstance( $this->newTestConfig() );
85  $theServices = MediaWikiServices::getInstance();
86 
87  $this->assertSame(
88  $service1,
89  $theServices->getService( 'Test' ),
90  'service definition should survive reset'
91  );
92 
93  $this->assertNotSame( $theServices, $newServices );
94  $this->assertNotSame( $theServices, $oldServices );
95 
96  MediaWikiServices::forceGlobalInstance( $oldServices );
97  }
98 
99  public function testResetGlobalInstance_quick() {
100  $newServices = $this->newMediaWikiServices();
101  $oldServices = MediaWikiServices::forceGlobalInstance( $newServices );
102 
103  $service1 = $this->createMock( SalvageableService::class );
104  $service1->expects( $this->never() )
105  ->method( 'salvage' );
106 
107  $service2 = $this->createMock( SalvageableService::class );
108  $service2->expects( $this->once() )
109  ->method( 'salvage' )
110  ->with( $service1 );
111 
112  // sequence of values the instantiator will return
113  $instantiatorReturnValues = [
114  $service1,
115  $service2,
116  ];
117 
118  $newServices->defineService(
119  'Test',
120  function () use ( &$instantiatorReturnValues ) {
121  return array_shift( $instantiatorReturnValues );
122  }
123  );
124 
125  // force instantiation
126  $newServices->getService( 'Test' );
127 
128  MediaWikiServices::resetGlobalInstance( $this->newTestConfig(), 'quick' );
129  $theServices = MediaWikiServices::getInstance();
130 
131  $this->assertSame( $service2, $theServices->getService( 'Test' ) );
132 
133  $this->assertNotSame( $theServices, $newServices );
134  $this->assertNotSame( $theServices, $oldServices );
135 
136  MediaWikiServices::forceGlobalInstance( $oldServices );
137  }
138 
139  public function testDisableStorageBackend() {
140  $newServices = $this->newMediaWikiServices();
141  $oldServices = MediaWikiServices::forceGlobalInstance( $newServices );
142 
143  $lbFactory = $this->getMockBuilder( \Wikimedia\Rdbms\LBFactorySimple::class )
144  ->disableOriginalConstructor()
145  ->getMock();
146 
147  $newServices->redefineService(
148  'DBLoadBalancerFactory',
149  function () use ( $lbFactory ) {
150  return $lbFactory;
151  }
152  );
153 
154  // force the service to become active, so we can check that it does get destroyed
155  $newServices->getService( 'DBLoadBalancerFactory' );
156 
157  MediaWikiServices::disableStorageBackend(); // should destroy DBLoadBalancerFactory
158 
159  try {
160  MediaWikiServices::getInstance()->getService( 'DBLoadBalancerFactory' );
161  $this->fail( 'DBLoadBalancerFactory should have been disabled' );
162  }
163  catch ( ServiceDisabledException $ex ) {
164  // ok, as expected
165  } catch ( Throwable $ex ) {
166  $this->fail( 'ServiceDisabledException expected, caught ' . get_class( $ex ) );
167  }
168 
169  MediaWikiServices::forceGlobalInstance( $oldServices );
170  $newServices->destroy();
171 
172  // No exception was thrown, avoid being risky
173  $this->assertTrue( true );
174  }
175 
176  public function testResetChildProcessServices() {
177  $newServices = $this->newMediaWikiServices();
178  $oldServices = MediaWikiServices::forceGlobalInstance( $newServices );
179 
180  $service1 = $this->createMock( DestructibleService::class );
181  $service1->expects( $this->once() )
182  ->method( 'destroy' );
183 
184  $service2 = $this->createMock( DestructibleService::class );
185  $service2->expects( $this->never() )
186  ->method( 'destroy' );
187 
188  // sequence of values the instantiator will return
189  $instantiatorReturnValues = [
190  $service1,
191  $service2,
192  ];
193 
194  $newServices->defineService(
195  'Test',
196  function () use ( &$instantiatorReturnValues ) {
197  return array_shift( $instantiatorReturnValues );
198  }
199  );
200 
201  // force the service to become active, so we can check that it does get destroyed
202  $oldTestService = $newServices->getService( 'Test' );
203 
204  MediaWikiServices::resetChildProcessServices();
205  $finalServices = MediaWikiServices::getInstance();
206 
207  $newTestService = $finalServices->getService( 'Test' );
208  $this->assertNotSame( $oldTestService, $newTestService );
209 
210  MediaWikiServices::forceGlobalInstance( $oldServices );
211  }
212 
213  public function testResetServiceForTesting() {
214  $services = $this->newMediaWikiServices();
215  $serviceCounter = 0;
216 
217  $services->defineService(
218  'Test',
219  function () use ( &$serviceCounter ) {
220  $serviceCounter++;
221  $service = $this->createMock( MediaWiki\Services\DestructibleService::class );
222  $service->expects( $this->once() )->method( 'destroy' );
223  return $service;
224  }
225  );
226 
227  // This should do nothing. In particular, it should not create a service instance.
228  $services->resetServiceForTesting( 'Test' );
229  $this->assertEquals( 0, $serviceCounter, 'No service instance should be created yet.' );
230 
231  $oldInstance = $services->getService( 'Test' );
232  $this->assertEquals( 1, $serviceCounter, 'A service instance should exit now.' );
233 
234  // The old instance should be detached, and destroy() called.
235  $services->resetServiceForTesting( 'Test' );
236  $newInstance = $services->getService( 'Test' );
237 
238  $this->assertNotSame( $oldInstance, $newInstance );
239 
240  // Satisfy the expectation that destroy() is called also for the second service instance.
241  $newInstance->destroy();
242  }
243 
245  $services = $this->newMediaWikiServices();
246 
247  $services->defineService(
248  'Test',
249  function () {
250  $service = $this->createMock( MediaWiki\Services\DestructibleService::class );
251  $service->expects( $this->never() )->method( 'destroy' );
252  return $service;
253  }
254  );
255 
256  $oldInstance = $services->getService( 'Test' );
257 
258  // The old instance should be detached, but destroy() not called.
259  $services->resetServiceForTesting( 'Test', false );
260  $newInstance = $services->getService( 'Test' );
261 
262  $this->assertNotSame( $oldInstance, $newInstance );
263  }
264 
265  public function provideGetters() {
266  $getServiceCases = $this->provideGetService();
267  $getterCases = [];
268 
269  // All getters should be named just like the service, with "get" added.
270  foreach ( $getServiceCases as $name => $case ) {
271  if ( $name[0] === '_' ) {
272  // Internal service, no getter
273  continue;
274  }
275  list( $service, $class ) = $case;
276  $getterCases[$name] = [
277  'get' . $service,
278  $class,
279  ];
280  }
281 
282  return $getterCases;
283  }
284 
288  public function testGetters( $getter, $type ) {
289  // Test against the default instance, since the dummy will not know the default services.
290  $services = MediaWikiServices::getInstance();
291  $service = $services->$getter();
292  $this->assertInstanceOf( $type, $service );
293  }
294 
295  public function provideGetService() {
296  global $IP;
297  $serviceList = require "$IP/includes/ServiceWiring.php";
298  $ret = [];
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" );
304  }
305  $ret[$name] = [ $name, $fun->getReturnType()->__toString() ];
306  }
307  return $ret;
308  }
309 
313  public function testGetService( $name, $type ) {
314  // Test against the default instance, since the dummy will not know the default services.
315  $services = MediaWikiServices::getInstance();
316 
317  $service = $services->getService( $name );
318  $this->assertInstanceOf( $type, $service );
319  }
320 
322  // Check all services in the default instance, not a dummy instance!
323  // Note that we instantiate all services here, including any that
324  // were registered by extensions.
325  $services = MediaWikiServices::getInstance();
326  $names = $services->getServiceNames();
327 
328  foreach ( $names as $name ) {
329  $this->assertTrue( $services->hasService( $name ) );
330  $service = $services->getService( $name );
331  $this->assertInternalType( 'object', $service );
332  }
333  }
334 
336  global $IP;
337  $testedServices = array_keys( $this->provideGetService() );
338  $allServices = array_keys( require "$IP/includes/ServiceWiring.php" );
339  $this->assertEquals(
340  [],
341  array_diff( $allServices, $testedServices ),
342  'The following services have not been added to MediaWikiServicesTest::provideGetService'
343  );
344  }
345 
346  public function testGettersAreSorted() {
347  $methods = ( new ReflectionClass( MediaWikiServices::class ) )
348  ->getMethods( ReflectionMethod::IS_STATIC | ReflectionMethod::IS_PUBLIC );
349 
350  $names = array_map( function ( $method ) {
351  return $method->getName();
352  }, $methods );
353  $serviceNames = array_map( function ( $name ) {
354  return "get$name";
355  }, array_keys( $this->provideGetService() ) );
356  $names = array_values( array_filter( $names, function ( $name ) use ( $serviceNames ) {
357  return in_array( $name, $serviceNames );
358  } ) );
359 
360  $sortedNames = $names;
361  sort( $sortedNames );
362 
363  $this->assertSame( $sortedNames, $names,
364  'Please keep service getters sorted alphabetically' );
365  }
366 }
MediaWiki\Services\DestructibleService
DestructibleService defines a standard interface for shutting down a service instance.
Definition: DestructibleService.php:35
MediaWikiServicesTest\testForceGlobalInstance
testForceGlobalInstance()
Definition: MediaWikiServicesTest.php:50
MediaWikiServicesTest\testResetGlobalInstance
testResetGlobalInstance()
Definition: MediaWikiServicesTest.php:66
MediaWikiServicesTest\provideGetService
provideGetService()
Definition: MediaWikiServicesTest.php:295
HashConfig
A Config instance which stores all settings as a member variable.
Definition: HashConfig.php:28
MediaWikiServicesTest
MediaWiki\MediaWikiServices.
Definition: MediaWikiServicesTest.php:13
MediaWikiServicesTest\testResetServiceForTesting
testResetServiceForTesting()
Definition: MediaWikiServicesTest.php:213
MediaWikiServicesTest\testGetters
testGetters( $getter, $type)
provideGetters
Definition: MediaWikiServicesTest.php:288
MediaWikiServicesTest\newMediaWikiServices
newMediaWikiServices(Config $config=null)
Definition: MediaWikiServicesTest.php:31
MediaWikiServicesTest\newTestConfig
newTestConfig()
Definition: MediaWikiServicesTest.php:18
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
Config
Interface for configuration instances.
Definition: Config.php:28
MediaWikiServicesTest\testResetServiceForTesting_noDestroy
testResetServiceForTesting_noDestroy()
Definition: MediaWikiServicesTest.php:244
MediaWikiServicesTest\testGettersAreSorted
testGettersAreSorted()
Definition: MediaWikiServicesTest.php:346
MWException
MediaWiki exception.
Definition: MWException.php:26
MediaWikiServicesTest\provideGetters
provideGetters()
Definition: MediaWikiServicesTest.php:265
MediaWikiTestCase
Definition: MediaWikiTestCase.php:16
MediaWiki
A helper class for throttling authentication attempts.
$IP
$IP
Definition: update.php:3
MediaWikiServicesTest\testDefaultServiceInstantiation
testDefaultServiceInstantiation()
Definition: MediaWikiServicesTest.php:321
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
GlobalVarConfig
Accesses configuration settings from $GLOBALS.
Definition: GlobalVarConfig.php:28
list
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
Definition: deferred.txt:11
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:302
MediaWikiServicesTest\testGetInstance
testGetInstance()
Definition: MediaWikiServicesTest.php:45
$ret
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
Definition: hooks.txt:2036
MediaWiki\Services\SalvageableService
SalvageableService defines an interface for services that are able to salvage state from a previous i...
Definition: SalvageableService.php:35
MediaWikiServicesTest\testResetChildProcessServices
testResetChildProcessServices()
Definition: MediaWikiServicesTest.php:176
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
Wikimedia
This program is free software; you can redistribute it and/or modify it under the terms of the GNU Ge...
MediaWikiServicesTest\testGetService
testGetService( $name, $type)
provideGetService
Definition: MediaWikiServicesTest.php:313
MediaWiki\Services\ServiceDisabledException
Exception thrown when trying to access a disabled service.
Definition: ServiceDisabledException.php:33
MediaWikiServicesTest\testDefaultServiceWiringServicesHaveTests
testDefaultServiceWiringServicesHaveTests()
Definition: MediaWikiServicesTest.php:335
MediaWikiServicesTest\testDisableStorageBackend
testDisableStorageBackend()
Definition: MediaWikiServicesTest.php:139
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
$services
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
Definition: hooks.txt:2270
MediaWikiServices
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency MediaWikiServices
Definition: injection.txt:23
MediaWikiServicesTest\testResetGlobalInstance_quick
testResetGlobalInstance_quick()
Definition: MediaWikiServicesTest.php:99
$type
$type
Definition: testCompression.php:48