MediaWiki  1.29.1
MediaWikiServicesTest.php
Go to the documentation of this file.
1 <?php
2 use Liuggio\StatsdClient\Factory\StatsdDataFactory;
11 
18 
22  private function newTestConfig() {
23  $globalConfig = new GlobalVarConfig();
24 
25  $testConfig = new HashConfig();
26  $testConfig->set( 'ServiceWiringFiles', $globalConfig->get( 'ServiceWiringFiles' ) );
27  $testConfig->set( 'ConfigRegistry', $globalConfig->get( 'ConfigRegistry' ) );
28 
29  return $testConfig;
30  }
31 
35  private function newMediaWikiServices( Config $config = null ) {
36  if ( $config === null ) {
37  $config = $this->newTestConfig();
38  }
39 
40  $instance = new MediaWikiServices( $config );
41 
42  // Load the default wiring from the specified files.
43  $wiringFiles = $config->get( 'ServiceWiringFiles' );
44  $instance->loadWiringFiles( $wiringFiles );
45 
46  return $instance;
47  }
48 
49  public function testGetInstance() {
50  $services = MediaWikiServices::getInstance();
51  $this->assertInstanceOf( 'MediaWiki\\MediaWikiServices', $services );
52  }
53 
54  public function testForceGlobalInstance() {
55  $newServices = $this->newMediaWikiServices();
56  $oldServices = MediaWikiServices::forceGlobalInstance( $newServices );
57 
58  $this->assertInstanceOf( 'MediaWiki\\MediaWikiServices', $oldServices );
59  $this->assertNotSame( $oldServices, $newServices );
60 
61  $theServices = MediaWikiServices::getInstance();
62  $this->assertSame( $theServices, $newServices );
63 
64  MediaWikiServices::forceGlobalInstance( $oldServices );
65 
66  $theServices = MediaWikiServices::getInstance();
67  $this->assertSame( $theServices, $oldServices );
68  }
69 
70  public function testResetGlobalInstance() {
71  $newServices = $this->newMediaWikiServices();
72  $oldServices = MediaWikiServices::forceGlobalInstance( $newServices );
73 
74  $service1 = $this->createMock( SalvageableService::class );
75  $service1->expects( $this->never() )
76  ->method( 'salvage' );
77 
78  $newServices->defineService(
79  'Test',
80  function() use ( $service1 ) {
81  return $service1;
82  }
83  );
84 
85  // force instantiation
86  $newServices->getService( 'Test' );
87 
88  MediaWikiServices::resetGlobalInstance( $this->newTestConfig() );
89  $theServices = MediaWikiServices::getInstance();
90 
91  $this->assertSame(
92  $service1,
93  $theServices->getService( 'Test' ),
94  'service definition should survive reset'
95  );
96 
97  $this->assertNotSame( $theServices, $newServices );
98  $this->assertNotSame( $theServices, $oldServices );
99 
100  MediaWikiServices::forceGlobalInstance( $oldServices );
101  }
102 
103  public function testResetGlobalInstance_quick() {
104  $newServices = $this->newMediaWikiServices();
105  $oldServices = MediaWikiServices::forceGlobalInstance( $newServices );
106 
107  $service1 = $this->createMock( SalvageableService::class );
108  $service1->expects( $this->never() )
109  ->method( 'salvage' );
110 
111  $service2 = $this->createMock( SalvageableService::class );
112  $service2->expects( $this->once() )
113  ->method( 'salvage' )
114  ->with( $service1 );
115 
116  // sequence of values the instantiator will return
117  $instantiatorReturnValues = [
118  $service1,
119  $service2,
120  ];
121 
122  $newServices->defineService(
123  'Test',
124  function() use ( &$instantiatorReturnValues ) {
125  return array_shift( $instantiatorReturnValues );
126  }
127  );
128 
129  // force instantiation
130  $newServices->getService( 'Test' );
131 
132  MediaWikiServices::resetGlobalInstance( $this->newTestConfig(), 'quick' );
133  $theServices = MediaWikiServices::getInstance();
134 
135  $this->assertSame( $service2, $theServices->getService( 'Test' ) );
136 
137  $this->assertNotSame( $theServices, $newServices );
138  $this->assertNotSame( $theServices, $oldServices );
139 
140  MediaWikiServices::forceGlobalInstance( $oldServices );
141  }
142 
143  public function testDisableStorageBackend() {
144  $newServices = $this->newMediaWikiServices();
145  $oldServices = MediaWikiServices::forceGlobalInstance( $newServices );
146 
147  $lbFactory = $this->getMockBuilder( 'LBFactorySimple' )
148  ->disableOriginalConstructor()
149  ->getMock();
150 
151  $newServices->redefineService(
152  'DBLoadBalancerFactory',
153  function() use ( $lbFactory ) {
154  return $lbFactory;
155  }
156  );
157 
158  // force the service to become active, so we can check that it does get destroyed
159  $newServices->getService( 'DBLoadBalancerFactory' );
160 
161  MediaWikiServices::disableStorageBackend(); // should destroy DBLoadBalancerFactory
162 
163  try {
164  MediaWikiServices::getInstance()->getService( 'DBLoadBalancerFactory' );
165  $this->fail( 'DBLoadBalancerFactory should have been disabled' );
166  }
167  catch ( ServiceDisabledException $ex ) {
168  // ok, as expected
169  } catch ( Throwable $ex ) {
170  $this->fail( 'ServiceDisabledException expected, caught ' . get_class( $ex ) );
171  }
172 
173  MediaWikiServices::forceGlobalInstance( $oldServices );
174  $newServices->destroy();
175  }
176 
177  public function testResetChildProcessServices() {
178  $newServices = $this->newMediaWikiServices();
179  $oldServices = MediaWikiServices::forceGlobalInstance( $newServices );
180 
181  $service1 = $this->createMock( DestructibleService::class );
182  $service1->expects( $this->once() )
183  ->method( 'destroy' );
184 
185  $service2 = $this->createMock( DestructibleService::class );
186  $service2->expects( $this->never() )
187  ->method( 'destroy' );
188 
189  // sequence of values the instantiator will return
190  $instantiatorReturnValues = [
191  $service1,
192  $service2,
193  ];
194 
195  $newServices->defineService(
196  'Test',
197  function() use ( &$instantiatorReturnValues ) {
198  return array_shift( $instantiatorReturnValues );
199  }
200  );
201 
202  // force the service to become active, so we can check that it does get destroyed
203  $oldTestService = $newServices->getService( 'Test' );
204 
205  MediaWikiServices::resetChildProcessServices();
206  $finalServices = MediaWikiServices::getInstance();
207 
208  $newTestService = $finalServices->getService( 'Test' );
209  $this->assertNotSame( $oldTestService, $newTestService );
210 
211  MediaWikiServices::forceGlobalInstance( $oldServices );
212  }
213 
214  public function testResetServiceForTesting() {
215  $services = $this->newMediaWikiServices();
216  $serviceCounter = 0;
217 
218  $services->defineService(
219  'Test',
220  function() use ( &$serviceCounter ) {
221  $serviceCounter++;
222  $service = $this->createMock( 'MediaWiki\Services\DestructibleService' );
223  $service->expects( $this->once() )->method( 'destroy' );
224  return $service;
225  }
226  );
227 
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.' );
231 
232  $oldInstance = $services->getService( 'Test' );
233  $this->assertEquals( 1, $serviceCounter, 'A service instance should exit now.' );
234 
235  // The old instance should be detached, and destroy() called.
236  $services->resetServiceForTesting( 'Test' );
237  $newInstance = $services->getService( 'Test' );
238 
239  $this->assertNotSame( $oldInstance, $newInstance );
240 
241  // Satisfy the expectation that destroy() is called also for the second service instance.
242  $newInstance->destroy();
243  }
244 
246  $services = $this->newMediaWikiServices();
247 
248  $services->defineService(
249  'Test',
250  function() {
251  $service = $this->createMock( 'MediaWiki\Services\DestructibleService' );
252  $service->expects( $this->never() )->method( 'destroy' );
253  return $service;
254  }
255  );
256 
257  $oldInstance = $services->getService( 'Test' );
258 
259  // The old instance should be detached, but destroy() not called.
260  $services->resetServiceForTesting( 'Test', false );
261  $newInstance = $services->getService( 'Test' );
262 
263  $this->assertNotSame( $oldInstance, $newInstance );
264  }
265 
266  public function provideGetters() {
267  $getServiceCases = $this->provideGetService();
268  $getterCases = [];
269 
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
274  continue;
275  }
276  list( $service, $class ) = $case;
277  $getterCases[$name] = [
278  'get' . $service,
279  $class,
280  ];
281  }
282 
283  return $getterCases;
284  }
285 
289  public function testGetters( $getter, $type ) {
290  // Test against the default instance, since the dummy will not know the default services.
291  $services = MediaWikiServices::getInstance();
292  $service = $services->$getter();
293  $this->assertInstanceOf( $type, $service );
294  }
295 
296  public function provideGetService() {
297  // NOTE: This should list all service getters defined in ServiceWiring.php.
298  // NOTE: For every test case defined here there should be a corresponding
299  // test case defined in provideGetters().
300  return [
301  'BootstrapConfig' => [ 'BootstrapConfig', Config::class ],
302  'ConfigFactory' => [ 'ConfigFactory', ConfigFactory::class ],
303  'MainConfig' => [ 'MainConfig', Config::class ],
304  'SiteStore' => [ 'SiteStore', SiteStore::class ],
305  'SiteLookup' => [ 'SiteLookup', SiteLookup::class ],
306  'StatsdDataFactory' => [ 'StatsdDataFactory', StatsdDataFactory::class ],
307  'InterwikiLookup' => [ 'InterwikiLookup', InterwikiLookup::class ],
308  'EventRelayerGroup' => [ 'EventRelayerGroup', EventRelayerGroup::class ],
309  'SearchEngineFactory' => [ 'SearchEngineFactory', SearchEngineFactory::class ],
310  'SearchEngineConfig' => [ 'SearchEngineConfig', SearchEngineConfig::class ],
311  'SkinFactory' => [ 'SkinFactory', SkinFactory::class ],
312  'DBLoadBalancerFactory' => [ 'DBLoadBalancerFactory', Wikimedia\Rdbms\LBFactory::class ],
313  'DBLoadBalancer' => [ 'DBLoadBalancer', 'LoadBalancer' ],
314  'WatchedItemStore' => [ 'WatchedItemStore', WatchedItemStore::class ],
315  'WatchedItemQueryService' => [ 'WatchedItemQueryService', WatchedItemQueryService::class ],
316  'CryptRand' => [ 'CryptRand', CryptRand::class ],
317  'CryptHKDF' => [ 'CryptHKDF', CryptHKDF::class ],
318  'MediaHandlerFactory' => [ 'MediaHandlerFactory', MediaHandlerFactory::class ],
319  'Parser' => [ 'Parser', Parser::class ],
320  'GenderCache' => [ 'GenderCache', GenderCache::class ],
321  'LinkCache' => [ 'LinkCache', LinkCache::class ],
322  'LinkRenderer' => [ 'LinkRenderer', LinkRenderer::class ],
323  'LinkRendererFactory' => [ 'LinkRendererFactory', LinkRendererFactory::class ],
324  '_MediaWikiTitleCodec' => [ '_MediaWikiTitleCodec', MediaWikiTitleCodec::class ],
325  'MimeAnalyzer' => [ 'MimeAnalyzer', MimeAnalyzer::class ],
326  'TitleFormatter' => [ 'TitleFormatter', TitleFormatter::class ],
327  'TitleParser' => [ 'TitleParser', TitleParser::class ],
328  'ProxyLookup' => [ 'ProxyLookup', ProxyLookup::class ],
329  'MainObjectStash' => [ 'MainObjectStash', BagOStuff::class ],
330  'MainWANObjectCache' => [ 'MainWANObjectCache', WANObjectCache::class ],
331  'LocalServerObjectCache' => [ 'LocalServerObjectCache', BagOStuff::class ],
332  'VirtualRESTServiceClient' => [ 'VirtualRESTServiceClient', VirtualRESTServiceClient::class ]
333  ];
334  }
335 
339  public function testGetService( $name, $type ) {
340  // Test against the default instance, since the dummy will not know the default services.
341  $services = MediaWikiServices::getInstance();
342 
343  $service = $services->getService( $name );
344  $this->assertInstanceOf( $type, $service );
345  }
346 
348  // Check all services in the default instance, not a dummy instance!
349  // Note that we instantiate all services here, including any that
350  // were registered by extensions.
351  $services = MediaWikiServices::getInstance();
352  $names = $services->getServiceNames();
353 
354  foreach ( $names as $name ) {
355  $this->assertTrue( $services->hasService( $name ) );
356  $service = $services->getService( $name );
357  $this->assertInternalType( 'object', $service );
358  }
359  }
360 
361 }
MediaWiki\Services\DestructibleService
DestructibleService defines a standard interface for shutting down a service instance.
Definition: DestructibleService.php:35
MediaWikiServicesTest\testForceGlobalInstance
testForceGlobalInstance()
Definition: MediaWikiServicesTest.php:54
MediaWikiServicesTest\testResetGlobalInstance
testResetGlobalInstance()
Definition: MediaWikiServicesTest.php:70
MediaWikiServicesTest\provideGetService
provideGetService()
Definition: MediaWikiServicesTest.php:296
HashConfig
A Config instance which stores all settings as a member variable.
Definition: HashConfig.php:28
MediaWikiServicesTest
MediaWiki\MediaWikiServices.
Definition: MediaWikiServicesTest.php:17
MediaWiki\Linker\LinkRenderer
Class that generates HTML links for pages.
Definition: LinkRenderer.php:42
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
MediaWiki\Linker\LinkRendererFactory
Factory to create LinkRender objects.
Definition: LinkRendererFactory.php:32
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:304
$type
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 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:2536
MediaWikiServicesTest\testResetServiceForTesting
testResetServiceForTesting()
Definition: MediaWikiServicesTest.php:214
MediaWikiServicesTest\testGetters
testGetters( $getter, $type)
provideGetters
Definition: MediaWikiServicesTest.php:289
$lbFactory
$lbFactory
Definition: doMaintenance.php:117
MediaWikiServicesTest\newMediaWikiServices
newMediaWikiServices(Config $config=null)
Definition: MediaWikiServicesTest.php:35
MediaWikiServicesTest\newTestConfig
newTestConfig()
Definition: MediaWikiServicesTest.php:22
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
MediaWikiServicesTest\testResetServiceForTesting_noDestroy
testResetServiceForTesting_noDestroy()
Definition: MediaWikiServicesTest.php:245
MediaWikiServicesTest\provideGetters
provideGetters()
Definition: MediaWikiServicesTest.php:266
MediaWikiTestCase
Definition: MediaWikiTestCase.php:13
MediaWiki\Interwiki\InterwikiLookup
Service interface for looking up Interwiki records.
Definition: InterwikiLookup.php:31
MediaWikiServicesTest\testDefaultServiceInstantiation
testDefaultServiceInstantiation()
Definition: MediaWikiServicesTest.php:347
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
$services
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:2179
MediaWikiServicesTest\testGetInstance
testGetInstance()
Definition: MediaWikiServicesTest.php:49
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:177
Wikimedia\Rdbms\LBFactory
An interface for generating database load balancers.
Definition: LBFactory.php:38
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
MediaWikiServicesTest\testGetService
testGetService( $name, $type)
provideGetService
Definition: MediaWikiServicesTest.php:339
MediaWiki\Services\ServiceDisabledException
Exception thrown when trying to access a disabled service.
Definition: ServiceDisabledException.php:33
MediaWikiServicesTest\testDisableStorageBackend
testDisableStorageBackend()
Definition: MediaWikiServicesTest.php:143
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
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:103