MediaWiki  1.30.0
ServiceContainerTest.php
Go to the documentation of this file.
1 <?php
3 
9 class ServiceContainerTest extends PHPUnit_Framework_TestCase {
10 
11  private function newServiceContainer( $extraArgs = [] ) {
12  return new ServiceContainer( $extraArgs );
13  }
14 
15  public function testGetServiceNames() {
16  $services = $this->newServiceContainer();
17  $names = $services->getServiceNames();
18 
19  $this->assertInternalType( 'array', $names );
20  $this->assertEmpty( $names );
21 
22  $name = 'TestService92834576';
23  $services->defineService( $name, function () {
24  return null;
25  } );
26 
27  $names = $services->getServiceNames();
28  $this->assertContains( $name, $names );
29  }
30 
31  public function testHasService() {
32  $services = $this->newServiceContainer();
33 
34  $name = 'TestService92834576';
35  $this->assertFalse( $services->hasService( $name ) );
36 
37  $services->defineService( $name, function () {
38  return null;
39  } );
40 
41  $this->assertTrue( $services->hasService( $name ) );
42  }
43 
44  public function testGetService() {
45  $services = $this->newServiceContainer( [ 'Foo' ] );
46 
47  $theService = new stdClass();
48  $name = 'TestService92834576';
49  $count = 0;
50 
51  $services->defineService(
52  $name,
53  function ( $actualLocator, $extra ) use ( $services, $theService, &$count ) {
54  $count++;
55  PHPUnit_Framework_Assert::assertSame( $services, $actualLocator );
56  PHPUnit_Framework_Assert::assertSame( $extra, 'Foo' );
57  return $theService;
58  }
59  );
60 
61  $this->assertSame( $theService, $services->getService( $name ) );
62 
63  $services->getService( $name );
64  $this->assertSame( 1, $count, 'instantiator should be called exactly once!' );
65  }
66 
67  public function testGetService_fail_unknown() {
68  $services = $this->newServiceContainer();
69 
70  $name = 'TestService92834576';
71 
72  $this->setExpectedException( 'MediaWiki\Services\NoSuchServiceException' );
73 
74  $services->getService( $name );
75  }
76 
77  public function testPeekService() {
78  $services = $this->newServiceContainer();
79 
80  $services->defineService(
81  'Foo',
82  function () {
83  return new stdClass();
84  }
85  );
86 
87  $services->defineService(
88  'Bar',
89  function () {
90  return new stdClass();
91  }
92  );
93 
94  // trigger instantiation of Foo
95  $services->getService( 'Foo' );
96 
97  $this->assertInternalType(
98  'object',
99  $services->peekService( 'Foo' ),
100  'Peek should return the service object if it had been accessed before.'
101  );
102 
103  $this->assertNull(
104  $services->peekService( 'Bar' ),
105  'Peek should return null if the service was never accessed.'
106  );
107  }
108 
109  public function testPeekService_fail_unknown() {
110  $services = $this->newServiceContainer();
111 
112  $name = 'TestService92834576';
113 
114  $this->setExpectedException( 'MediaWiki\Services\NoSuchServiceException' );
115 
116  $services->peekService( $name );
117  }
118 
119  public function testDefineService() {
120  $services = $this->newServiceContainer();
121 
122  $theService = new stdClass();
123  $name = 'TestService92834576';
124 
125  $services->defineService( $name, function ( $actualLocator ) use ( $services, $theService ) {
126  PHPUnit_Framework_Assert::assertSame( $services, $actualLocator );
127  return $theService;
128  } );
129 
130  $this->assertTrue( $services->hasService( $name ) );
131  $this->assertSame( $theService, $services->getService( $name ) );
132  }
133 
135  $services = $this->newServiceContainer();
136 
137  $theService = new stdClass();
138  $name = 'TestService92834576';
139 
140  $services->defineService( $name, function () use ( $theService ) {
141  return $theService;
142  } );
143 
144  $this->setExpectedException( 'MediaWiki\Services\ServiceAlreadyDefinedException' );
145 
146  $services->defineService( $name, function () use ( $theService ) {
147  return $theService;
148  } );
149  }
150 
151  public function testApplyWiring() {
152  $services = $this->newServiceContainer();
153 
154  $wiring = [
155  'Foo' => function () {
156  return 'Foo!';
157  },
158  'Bar' => function () {
159  return 'Bar!';
160  },
161  ];
162 
163  $services->applyWiring( $wiring );
164 
165  $this->assertSame( 'Foo!', $services->getService( 'Foo' ) );
166  $this->assertSame( 'Bar!', $services->getService( 'Bar' ) );
167  }
168 
169  public function testImportWiring() {
170  $services = $this->newServiceContainer();
171 
172  $wiring = [
173  'Foo' => function () {
174  return 'Foo!';
175  },
176  'Bar' => function () {
177  return 'Bar!';
178  },
179  'Car' => function () {
180  return 'FUBAR!';
181  },
182  ];
183 
184  $services->applyWiring( $wiring );
185 
186  $newServices = $this->newServiceContainer();
187 
188  // define a service before importing, so we can later check that
189  // existing service instances survive importWiring()
190  $newServices->defineService( 'Car', function () {
191  return 'Car!';
192  } );
193 
194  // force instantiation
195  $newServices->getService( 'Car' );
196 
197  // Define another service, so we can later check that extra wiring
198  // is not lost.
199  $newServices->defineService( 'Xar', function () {
200  return 'Xar!';
201  } );
202 
203  // import wiring, but skip `Bar`
204  $newServices->importWiring( $services, [ 'Bar' ] );
205 
206  $this->assertNotContains( 'Bar', $newServices->getServiceNames(), 'Skip `Bar` service' );
207  $this->assertSame( 'Foo!', $newServices->getService( 'Foo' ) );
208 
209  // import all wiring, but preserve existing service instance
210  $newServices->importWiring( $services );
211 
212  $this->assertContains( 'Bar', $newServices->getServiceNames(), 'Import all services' );
213  $this->assertSame( 'Bar!', $newServices->getService( 'Bar' ) );
214  $this->assertSame( 'Car!', $newServices->getService( 'Car' ), 'Use existing service instance' );
215  $this->assertSame( 'Xar!', $newServices->getService( 'Xar' ), 'Predefined services are kept' );
216  }
217 
218  public function testLoadWiringFiles() {
219  $services = $this->newServiceContainer();
220 
221  $wiringFiles = [
222  __DIR__ . '/TestWiring1.php',
223  __DIR__ . '/TestWiring2.php',
224  ];
225 
226  $services->loadWiringFiles( $wiringFiles );
227 
228  $this->assertSame( 'Foo!', $services->getService( 'Foo' ) );
229  $this->assertSame( 'Bar!', $services->getService( 'Bar' ) );
230  }
231 
233  $services = $this->newServiceContainer();
234 
235  $wiringFiles = [
236  __DIR__ . '/TestWiring1.php',
237  __DIR__ . '/./TestWiring1.php',
238  ];
239 
240  // loading the same file twice should fail, because
241  $this->setExpectedException( 'MediaWiki\Services\ServiceAlreadyDefinedException' );
242 
243  $services->loadWiringFiles( $wiringFiles );
244  }
245 
246  public function testRedefineService() {
247  $services = $this->newServiceContainer( [ 'Foo' ] );
248 
249  $theService1 = new stdClass();
250  $name = 'TestService92834576';
251 
252  $services->defineService( $name, function () {
253  PHPUnit_Framework_Assert::fail(
254  'The original instantiator function should not get called'
255  );
256  } );
257 
258  // redefine before instantiation
259  $services->redefineService(
260  $name,
261  function ( $actualLocator, $extra ) use ( $services, $theService1 ) {
262  PHPUnit_Framework_Assert::assertSame( $services, $actualLocator );
263  PHPUnit_Framework_Assert::assertSame( 'Foo', $extra );
264  return $theService1;
265  }
266  );
267 
268  // force instantiation, check result
269  $this->assertSame( $theService1, $services->getService( $name ) );
270  }
271 
272  public function testRedefineService_disabled() {
273  $services = $this->newServiceContainer( [ 'Foo' ] );
274 
275  $theService1 = new stdClass();
276  $name = 'TestService92834576';
277 
278  $services->defineService( $name, function () {
279  return 'Foo';
280  } );
281 
282  // disable the service. we should be able to redefine it anyway.
283  $services->disableService( $name );
284 
285  $services->redefineService( $name, function () use ( $theService1 ) {
286  return $theService1;
287  } );
288 
289  // force instantiation, check result
290  $this->assertSame( $theService1, $services->getService( $name ) );
291  }
292 
294  $services = $this->newServiceContainer();
295 
296  $theService = new stdClass();
297  $name = 'TestService92834576';
298 
299  $this->setExpectedException( 'MediaWiki\Services\NoSuchServiceException' );
300 
301  $services->redefineService( $name, function () use ( $theService ) {
302  return $theService;
303  } );
304  }
305 
307  $services = $this->newServiceContainer( [ 'Foo' ] );
308 
309  $theService = new stdClass();
310  $name = 'TestService92834576';
311 
312  $services->defineService( $name, function () {
313  return 'Foo';
314  } );
315 
316  // create the service, so it can no longer be redefined
317  $services->getService( $name );
318 
319  $this->setExpectedException( 'MediaWiki\Services\CannotReplaceActiveServiceException' );
320 
321  $services->redefineService( $name, function () use ( $theService ) {
322  return $theService;
323  } );
324  }
325 
326  public function testDisableService() {
327  $services = $this->newServiceContainer( [ 'Foo' ] );
328 
329  $destructible = $this->getMockBuilder( 'MediaWiki\Services\DestructibleService' )
330  ->getMock();
331  $destructible->expects( $this->once() )
332  ->method( 'destroy' );
333 
334  $services->defineService( 'Foo', function () use ( $destructible ) {
335  return $destructible;
336  } );
337  $services->defineService( 'Bar', function () {
338  return new stdClass();
339  } );
340  $services->defineService( 'Qux', function () {
341  return new stdClass();
342  } );
343 
344  // instantiate Foo and Bar services
345  $services->getService( 'Foo' );
346  $services->getService( 'Bar' );
347 
348  // disable service, should call destroy() once.
349  $services->disableService( 'Foo' );
350 
351  // disabled service should still be listed
352  $this->assertContains( 'Foo', $services->getServiceNames() );
353 
354  // getting other services should still work
355  $services->getService( 'Bar' );
356 
357  // disable non-destructible service, and not-yet-instantiated service
358  $services->disableService( 'Bar' );
359  $services->disableService( 'Qux' );
360 
361  $this->assertNull( $services->peekService( 'Bar' ) );
362  $this->assertNull( $services->peekService( 'Qux' ) );
363 
364  // disabled service should still be listed
365  $this->assertContains( 'Bar', $services->getServiceNames() );
366  $this->assertContains( 'Qux', $services->getServiceNames() );
367 
368  $this->setExpectedException( 'MediaWiki\Services\ServiceDisabledException' );
369  $services->getService( 'Qux' );
370  }
371 
373  $services = $this->newServiceContainer();
374 
375  $theService = new stdClass();
376  $name = 'TestService92834576';
377 
378  $this->setExpectedException( 'MediaWiki\Services\NoSuchServiceException' );
379 
380  $services->redefineService( $name, function () use ( $theService ) {
381  return $theService;
382  } );
383  }
384 
385  public function testDestroy() {
386  $services = $this->newServiceContainer();
387 
388  $destructible = $this->getMockBuilder( 'MediaWiki\Services\DestructibleService' )
389  ->getMock();
390  $destructible->expects( $this->once() )
391  ->method( 'destroy' );
392 
393  $services->defineService( 'Foo', function () use ( $destructible ) {
394  return $destructible;
395  } );
396 
397  $services->defineService( 'Bar', function () {
398  return new stdClass();
399  } );
400 
401  // create the service
402  $services->getService( 'Foo' );
403 
404  // destroy the container
405  $services->destroy();
406 
407  $this->setExpectedException( 'MediaWiki\Services\ContainerDisabledException' );
408  $services->getService( 'Bar' );
409  }
410 
411 }
ServiceContainerTest\testDisableService
testDisableService()
Definition: ServiceContainerTest.php:326
ServiceContainerTest\testRedefineService_fail_in_use
testRedefineService_fail_in_use()
Definition: ServiceContainerTest.php:306
ServiceContainerTest\testApplyWiring
testApplyWiring()
Definition: ServiceContainerTest.php:151
ServiceContainerTest\testDefineService
testDefineService()
Definition: ServiceContainerTest.php:119
MediaWiki\Services\ServiceContainer
ServiceContainer provides a generic service to manage named services using lazy instantiation based o...
Definition: ServiceContainer.php:46
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
ServiceContainerTest\testGetServiceNames
testGetServiceNames()
Definition: ServiceContainerTest.php:15
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:302
ServiceContainerTest\testDestroy
testDestroy()
Definition: ServiceContainerTest.php:385
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
ServiceContainerTest\testLoadWiringFiles
testLoadWiringFiles()
Definition: ServiceContainerTest.php:218
ServiceContainerTest\testDisableService_fail_undefined
testDisableService_fail_undefined()
Definition: ServiceContainerTest.php:372
ServiceContainerTest
MediaWiki\Services\ServiceContainer.
Definition: ServiceContainerTest.php:9
ServiceContainerTest\testDefineService_fail_duplicate
testDefineService_fail_duplicate()
Definition: ServiceContainerTest.php:134
ServiceContainerTest\testRedefineService
testRedefineService()
Definition: ServiceContainerTest.php:246
ServiceContainerTest\testLoadWiringFiles_fail_duplicate
testLoadWiringFiles_fail_duplicate()
Definition: ServiceContainerTest.php:232
$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:2198
ServiceContainerTest\testPeekService
testPeekService()
Definition: ServiceContainerTest.php:77
ServiceContainerTest\testGetService_fail_unknown
testGetService_fail_unknown()
Definition: ServiceContainerTest.php:67
ServiceContainerTest\testHasService
testHasService()
Definition: ServiceContainerTest.php:31
ServiceContainerTest\testPeekService_fail_unknown
testPeekService_fail_unknown()
Definition: ServiceContainerTest.php:109
ServiceContainerTest\testRedefineService_disabled
testRedefineService_disabled()
Definition: ServiceContainerTest.php:272
ServiceContainerTest\testRedefineService_fail_undefined
testRedefineService_fail_undefined()
Definition: ServiceContainerTest.php:293
ServiceContainerTest\testImportWiring
testImportWiring()
Definition: ServiceContainerTest.php:169
ServiceContainerTest\testGetService
testGetService()
Definition: ServiceContainerTest.php:44
ServiceContainerTest\newServiceContainer
newServiceContainer( $extraArgs=[])
Definition: ServiceContainerTest.php:11