MediaWiki  master
MediaWikiServicesTest.php
Go to the documentation of this file.
1 <?php
2 use Liuggio\StatsdClient\Factory\StatsdDataFactory;
10 
17 
21  private function newTestConfig() {
22  $globalConfig = new GlobalVarConfig();
23 
24  $testConfig = new HashConfig();
25  $testConfig->set( 'ServiceWiringFiles', $globalConfig->get( 'ServiceWiringFiles' ) );
26  $testConfig->set( 'ConfigRegistry', $globalConfig->get( 'ConfigRegistry' ) );
27 
28  return $testConfig;
29  }
30 
34  private function newMediaWikiServices( Config $config = null ) {
35  if ( $config === null ) {
36  $config = $this->newTestConfig();
37  }
38 
39  $instance = new MediaWikiServices( $config );
40 
41  // Load the default wiring from the specified files.
42  $wiringFiles = $config->get( 'ServiceWiringFiles' );
43  $instance->loadWiringFiles( $wiringFiles );
44 
45  return $instance;
46  }
47 
48  public function testGetInstance() {
49  $services = MediaWikiServices::getInstance();
50  $this->assertInstanceOf( 'MediaWiki\\MediaWikiServices', $services );
51  }
52 
53  public function testForceGlobalInstance() {
54  $newServices = $this->newMediaWikiServices();
55  $oldServices = MediaWikiServices::forceGlobalInstance( $newServices );
56 
57  $this->assertInstanceOf( 'MediaWiki\\MediaWikiServices', $oldServices );
58  $this->assertNotSame( $oldServices, $newServices );
59 
60  $theServices = MediaWikiServices::getInstance();
61  $this->assertSame( $theServices, $newServices );
62 
63  MediaWikiServices::forceGlobalInstance( $oldServices );
64 
65  $theServices = MediaWikiServices::getInstance();
66  $this->assertSame( $theServices, $oldServices );
67  }
68 
69  public function testResetGlobalInstance() {
70  $newServices = $this->newMediaWikiServices();
71  $oldServices = MediaWikiServices::forceGlobalInstance( $newServices );
72 
73  $service1 = $this->getMock( SalvageableService::class );
74  $service1->expects( $this->never() )
75  ->method( 'salvage' );
76 
77  $newServices->defineService(
78  'Test',
79  function() use ( $service1 ) {
80  return $service1;
81  }
82  );
83 
84  // force instantiation
85  $newServices->getService( 'Test' );
86 
87  MediaWikiServices::resetGlobalInstance( $this->newTestConfig() );
88  $theServices = MediaWikiServices::getInstance();
89 
90  $this->assertSame(
91  $service1,
92  $theServices->getService( 'Test' ),
93  'service definition should survive reset'
94  );
95 
96  $this->assertNotSame( $theServices, $newServices );
97  $this->assertNotSame( $theServices, $oldServices );
98 
99  MediaWikiServices::forceGlobalInstance( $oldServices );
100  }
101 
102  public function testResetGlobalInstance_quick() {
103  $newServices = $this->newMediaWikiServices();
104  $oldServices = MediaWikiServices::forceGlobalInstance( $newServices );
105 
106  $service1 = $this->getMock( SalvageableService::class );
107  $service1->expects( $this->never() )
108  ->method( 'salvage' );
109 
110  $service2 = $this->getMock( SalvageableService::class );
111  $service2->expects( $this->once() )
112  ->method( 'salvage' )
113  ->with( $service1 );
114 
115  // sequence of values the instantiator will return
116  $instantiatorReturnValues = [
117  $service1,
118  $service2,
119  ];
120 
121  $newServices->defineService(
122  'Test',
123  function() use ( &$instantiatorReturnValues ) {
124  return array_shift( $instantiatorReturnValues );
125  }
126  );
127 
128  // force instantiation
129  $newServices->getService( 'Test' );
130 
131  MediaWikiServices::resetGlobalInstance( $this->newTestConfig(), 'quick' );
132  $theServices = MediaWikiServices::getInstance();
133 
134  $this->assertSame( $service2, $theServices->getService( 'Test' ) );
135 
136  $this->assertNotSame( $theServices, $newServices );
137  $this->assertNotSame( $theServices, $oldServices );
138 
139  MediaWikiServices::forceGlobalInstance( $oldServices );
140  }
141 
142  public function testDisableStorageBackend() {
143  $newServices = $this->newMediaWikiServices();
144  $oldServices = MediaWikiServices::forceGlobalInstance( $newServices );
145 
146  $lbFactory = $this->getMockBuilder( 'LBFactorySimple' )
147  ->disableOriginalConstructor()
148  ->getMock();
149 
150  $lbFactory->expects( $this->once() )
151  ->method( 'destroy' );
152 
153  $newServices->redefineService(
154  'DBLoadBalancerFactory',
155  function() use ( $lbFactory ) {
156  return $lbFactory;
157  }
158  );
159 
160  // force the service to become active, so we can check that it does get destroyed
161  $newServices->getService( 'DBLoadBalancerFactory' );
162 
163  MediaWikiServices::disableStorageBackend(); // should destroy DBLoadBalancerFactory
164 
165  try {
166  MediaWikiServices::getInstance()->getService( 'DBLoadBalancerFactory' );
167  $this->fail( 'DBLoadBalancerFactory shoudl have been disabled' );
168  }
169  catch ( ServiceDisabledException $ex ) {
170  // ok, as expected
171  }
172  catch ( Throwable $ex ) {
173  $this->fail( 'ServiceDisabledException expected, caught ' . get_class( $ex ) );
174  }
175 
176  MediaWikiServices::forceGlobalInstance( $oldServices );
177  $newServices->destroy();
178  }
179 
180  public function testResetChildProcessServices() {
181  $newServices = $this->newMediaWikiServices();
182  $oldServices = MediaWikiServices::forceGlobalInstance( $newServices );
183 
184  $service1 = $this->getMock( DestructibleService::class );
185  $service1->expects( $this->once() )
186  ->method( 'destroy' );
187 
188  $service2 = $this->getMock( DestructibleService::class );
189  $service2->expects( $this->never() )
190  ->method( 'destroy' );
191 
192  // sequence of values the instantiator will return
193  $instantiatorReturnValues = [
194  $service1,
195  $service2,
196  ];
197 
198  $newServices->defineService(
199  'Test',
200  function() use ( &$instantiatorReturnValues ) {
201  return array_shift( $instantiatorReturnValues );
202  }
203  );
204 
205  // force the service to become active, so we can check that it does get destroyed
206  $oldTestService = $newServices->getService( 'Test' );
207 
208  MediaWikiServices::resetChildProcessServices();
209  $finalServices = MediaWikiServices::getInstance();
210 
211  $newTestService = $finalServices->getService( 'Test' );
212  $this->assertNotSame( $oldTestService, $newTestService );
213 
214  MediaWikiServices::forceGlobalInstance( $oldServices );
215  }
216 
217  public function testResetServiceForTesting() {
218  $services = $this->newMediaWikiServices();
219  $serviceCounter = 0;
220 
221  $services->defineService(
222  'Test',
223  function() use ( &$serviceCounter ) {
224  $serviceCounter++;
225  $service = $this->getMock( 'MediaWiki\Services\DestructibleService' );
226  $service->expects( $this->once() )->method( 'destroy' );
227  return $service;
228  }
229  );
230 
231  // This should do nothing. In particular, it should not create a service instance.
232  $services->resetServiceForTesting( 'Test' );
233  $this->assertEquals( 0, $serviceCounter, 'No service instance should be created yet.' );
234 
235  $oldInstance = $services->getService( 'Test' );
236  $this->assertEquals( 1, $serviceCounter, 'A service instance should exit now.' );
237 
238  // The old instance should be detached, and destroy() called.
239  $services->resetServiceForTesting( 'Test' );
240  $newInstance = $services->getService( 'Test' );
241 
242  $this->assertNotSame( $oldInstance, $newInstance );
243 
244  // Satisfy the expectation that destroy() is called also for the second service instance.
245  $newInstance->destroy();
246  }
247 
249  $services = $this->newMediaWikiServices();
250 
251  $services->defineService(
252  'Test',
253  function() {
254  $service = $this->getMock( 'MediaWiki\Services\DestructibleService' );
255  $service->expects( $this->never() )->method( 'destroy' );
256  return $service;
257  }
258  );
259 
260  $oldInstance = $services->getService( 'Test' );
261 
262  // The old instance should be detached, but destroy() not called.
263  $services->resetServiceForTesting( 'Test', false );
264  $newInstance = $services->getService( 'Test' );
265 
266  $this->assertNotSame( $oldInstance, $newInstance );
267  }
268 
269  public function provideGetters() {
270  $getServiceCases = $this->provideGetService();
271  $getterCases = [];
272 
273  // All getters should be named just like the service, with "get" added.
274  foreach ( $getServiceCases as $name => $case ) {
275  if ( $name[0] === '_' ) {
276  // Internal service, no getter
277  continue;
278  }
279  list( $service, $class ) = $case;
280  $getterCases[$name] = [
281  'get' . $service,
282  $class,
283  ];
284  }
285 
286  return $getterCases;
287  }
288 
292  public function testGetters( $getter, $type ) {
293  // Test against the default instance, since the dummy will not know the default services.
294  $services = MediaWikiServices::getInstance();
295  $service = $services->$getter();
296  $this->assertInstanceOf( $type, $service );
297  }
298 
299  public function provideGetService() {
300  // NOTE: This should list all service getters defined in ServiceWiring.php.
301  // NOTE: For every test case defined here there should be a corresponding
302  // test case defined in provideGetters().
303  return [
304  'BootstrapConfig' => [ 'BootstrapConfig', Config::class ],
305  'ConfigFactory' => [ 'ConfigFactory', ConfigFactory::class ],
306  'MainConfig' => [ 'MainConfig', Config::class ],
307  'SiteStore' => [ 'SiteStore', SiteStore::class ],
308  'SiteLookup' => [ 'SiteLookup', SiteLookup::class ],
309  'StatsdDataFactory' => [ 'StatsdDataFactory', StatsdDataFactory::class ],
310  'InterwikiLookup' => [ 'InterwikiLookup', InterwikiLookup::class ],
311  'EventRelayerGroup' => [ 'EventRelayerGroup', EventRelayerGroup::class ],
312  'SearchEngineFactory' => [ 'SearchEngineFactory', SearchEngineFactory::class ],
313  'SearchEngineConfig' => [ 'SearchEngineConfig', SearchEngineConfig::class ],
314  'SkinFactory' => [ 'SkinFactory', SkinFactory::class ],
315  'DBLoadBalancerFactory' => [ 'DBLoadBalancerFactory', 'LBFactory' ],
316  'DBLoadBalancer' => [ 'DBLoadBalancer', 'LoadBalancer' ],
317  'WatchedItemStore' => [ 'WatchedItemStore', WatchedItemStore::class ],
318  'WatchedItemQueryService' => [ 'WatchedItemQueryService', WatchedItemQueryService::class ],
319  'GenderCache' => [ 'GenderCache', GenderCache::class ],
320  'LinkCache' => [ 'LinkCache', LinkCache::class ],
321  'LinkRenderer' => [ 'LinkRenderer', LinkRenderer::class ],
322  'LinkRendererFactory' => [ 'LinkRendererFactory', LinkRendererFactory::class ],
323  '_MediaWikiTitleCodec' => [ '_MediaWikiTitleCodec', MediaWikiTitleCodec::class ],
324  'TitleFormatter' => [ 'TitleFormatter', TitleFormatter::class ],
325  'TitleParser' => [ 'TitleParser', TitleParser::class ],
326  ];
327  }
328 
332  public function testGetService( $name, $type ) {
333  // Test against the default instance, since the dummy will not know the default services.
334  $services = MediaWikiServices::getInstance();
335 
336  $service = $services->getService( $name );
337  $this->assertInstanceOf( $type, $service );
338  }
339 
341  // Check all services in the default instance, not a dummy instance!
342  // Note that we instantiate all services here, including any that
343  // were registered by extensions.
344  $services = MediaWikiServices::getInstance();
345  $names = $services->getServiceNames();
346 
347  foreach ( $names as $name ) {
348  $this->assertTrue( $services->hasService( $name ) );
349  $service = $services->getService( $name );
350  $this->assertInternalType( 'object', $service );
351  }
352  }
353 
354 }
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
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
testGetService($name, $type)
provideGetService
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
testGetters($getter, $type)
provideGetters
MediaWiki\MediaWikiServices.
Exception thrown when trying to access a disabled service.
Accesses configuration settings from $GLOBALS.
Class that generates HTML links for pages.
Interface for configuration instances.
Definition: Config.php:28
Service interface for looking up Interwiki records.
static configuration should be added through ResourceLoaderGetConfigVars instead can be used to get the real title 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:2044
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
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
newMediaWikiServices(Config $config=null)
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
Factory to create LinkRender objects.
A Config instance which stores all settings as a member variable.
Definition: HashConfig.php:28
SalvageableService defines an interface for services that are able to salvage state from a previous i...
DestructibleService defines a standard interface for shutting down a service instance.
do that in ParserLimitReportFormat instead use this to modify the parameters of the image and a DIV can begin in one section and end in another Make sure your code can handle that case gracefully See the EditSectionClearerLink extension for an example zero but section is usually empty its values are the globals values before the output is cached one of or reset my talk my contributions etc etc otherwise the built in rate limiting checks are if enabled allows for interception of redirect as a string mapping parameter names to values & $type
Definition: hooks.txt:2376
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:310