2 use MediaWiki\Services\ServiceContainer
;
5 * @covers MediaWiki\Services\ServiceContainer
9 class ServiceContainerTest
extends PHPUnit\Framework\TestCase
{
11 use MediaWikiCoversValidator
;
12 use PHPUnit4And6Compat
;
14 private function newServiceContainer( $extraArgs = [] ) {
15 return new ServiceContainer( $extraArgs );
18 public function testGetServiceNames() {
19 $services = $this->newServiceContainer();
20 $names = $services->getServiceNames();
22 $this->assertInternalType( 'array', $names );
23 $this->assertEmpty( $names );
25 $name = 'TestService92834576';
26 $services->defineService( $name, function () {
30 $names = $services->getServiceNames();
31 $this->assertContains( $name, $names );
34 public function testHasService() {
35 $services = $this->newServiceContainer();
37 $name = 'TestService92834576';
38 $this->assertFalse( $services->hasService( $name ) );
40 $services->defineService( $name, function () {
44 $this->assertTrue( $services->hasService( $name ) );
47 public function testGetService() {
48 $services = $this->newServiceContainer( [ 'Foo' ] );
50 $theService = new stdClass();
51 $name = 'TestService92834576';
54 $services->defineService(
56 function ( $actualLocator, $extra ) use ( $services, $theService, &$count ) {
58 PHPUnit_Framework_Assert
::assertSame( $services, $actualLocator );
59 PHPUnit_Framework_Assert
::assertSame( $extra, 'Foo' );
64 $this->assertSame( $theService, $services->getService( $name ) );
66 $services->getService( $name );
67 $this->assertSame( 1, $count, 'instantiator should be called exactly once!' );
70 public function testGetService_fail_unknown() {
71 $services = $this->newServiceContainer();
73 $name = 'TestService92834576';
75 $this->setExpectedException( MediaWiki\Services\NoSuchServiceException
::class );
77 $services->getService( $name );
80 public function testPeekService() {
81 $services = $this->newServiceContainer();
83 $services->defineService(
86 return new stdClass();
90 $services->defineService(
93 return new stdClass();
97 // trigger instantiation of Foo
98 $services->getService( 'Foo' );
100 $this->assertInternalType(
102 $services->peekService( 'Foo' ),
103 'Peek should return the service object if it had been accessed before.'
107 $services->peekService( 'Bar' ),
108 'Peek should return null if the service was never accessed.'
112 public function testPeekService_fail_unknown() {
113 $services = $this->newServiceContainer();
115 $name = 'TestService92834576';
117 $this->setExpectedException( MediaWiki\Services\NoSuchServiceException
::class );
119 $services->peekService( $name );
122 public function testDefineService() {
123 $services = $this->newServiceContainer();
125 $theService = new stdClass();
126 $name = 'TestService92834576';
128 $services->defineService( $name, function ( $actualLocator ) use ( $services, $theService ) {
129 PHPUnit_Framework_Assert
::assertSame( $services, $actualLocator );
133 $this->assertTrue( $services->hasService( $name ) );
134 $this->assertSame( $theService, $services->getService( $name ) );
137 public function testDefineService_fail_duplicate() {
138 $services = $this->newServiceContainer();
140 $theService = new stdClass();
141 $name = 'TestService92834576';
143 $services->defineService( $name, function () use ( $theService ) {
147 $this->setExpectedException( MediaWiki\Services\ServiceAlreadyDefinedException
::class );
149 $services->defineService( $name, function () use ( $theService ) {
154 public function testApplyWiring() {
155 $services = $this->newServiceContainer();
158 'Foo' => function () {
161 'Bar' => function () {
166 $services->applyWiring( $wiring );
168 $this->assertSame( 'Foo!', $services->getService( 'Foo' ) );
169 $this->assertSame( 'Bar!', $services->getService( 'Bar' ) );
172 public function testImportWiring() {
173 $services = $this->newServiceContainer();
176 'Foo' => function () {
179 'Bar' => function () {
182 'Car' => function () {
187 $services->applyWiring( $wiring );
189 $services->addServiceManipulator( 'Foo', function ( $service ) {
190 return $service . '+X';
193 $services->addServiceManipulator( 'Car', function ( $service ) {
194 return $service . '+X';
197 $newServices = $this->newServiceContainer();
199 // create a service with manipulator
200 $newServices->defineService( 'Foo', function () {
204 $newServices->addServiceManipulator( 'Foo', function ( $service ) {
205 return $service . '+Y';
208 // create a service before importing, so we can later check that
209 // existing service instances survive importWiring()
210 $newServices->defineService( 'Car', function () {
214 // force instantiation
215 $newServices->getService( 'Car' );
217 // Define another service, so we can later check that extra wiring
219 $newServices->defineService( 'Xar', function () {
223 // import wiring, but skip `Bar`
224 $newServices->importWiring( $services, [ 'Bar' ] );
226 $this->assertNotContains( 'Bar', $newServices->getServiceNames(), 'Skip `Bar` service' );
227 $this->assertSame( 'Foo!+Y+X', $newServices->getService( 'Foo' ) );
229 // import all wiring, but preserve existing service instance
230 $newServices->importWiring( $services );
232 $this->assertContains( 'Bar', $newServices->getServiceNames(), 'Import all services' );
233 $this->assertSame( 'Bar!', $newServices->getService( 'Bar' ) );
234 $this->assertSame( 'Car!', $newServices->getService( 'Car' ), 'Use existing service instance' );
235 $this->assertSame( 'Xar!', $newServices->getService( 'Xar' ), 'Predefined services are kept' );
238 public function testLoadWiringFiles() {
239 $services = $this->newServiceContainer();
242 __DIR__
. '/TestWiring1.php',
243 __DIR__
. '/TestWiring2.php',
246 $services->loadWiringFiles( $wiringFiles );
248 $this->assertSame( 'Foo!', $services->getService( 'Foo' ) );
249 $this->assertSame( 'Bar!', $services->getService( 'Bar' ) );
252 public function testLoadWiringFiles_fail_duplicate() {
253 $services = $this->newServiceContainer();
256 __DIR__
. '/TestWiring1.php',
257 __DIR__
. '/./TestWiring1.php',
260 // loading the same file twice should fail, because
261 $this->setExpectedException( MediaWiki\Services\ServiceAlreadyDefinedException
::class );
263 $services->loadWiringFiles( $wiringFiles );
266 public function testRedefineService() {
267 $services = $this->newServiceContainer( [ 'Foo' ] );
269 $theService1 = new stdClass();
270 $name = 'TestService92834576';
272 $services->defineService( $name, function () {
273 PHPUnit_Framework_Assert
::fail(
274 'The original instantiator function should not get called'
278 // redefine before instantiation
279 $services->redefineService(
281 function ( $actualLocator, $extra ) use ( $services, $theService1 ) {
282 PHPUnit_Framework_Assert
::assertSame( $services, $actualLocator );
283 PHPUnit_Framework_Assert
::assertSame( 'Foo', $extra );
288 // force instantiation, check result
289 $this->assertSame( $theService1, $services->getService( $name ) );
292 public function testRedefineService_disabled() {
293 $services = $this->newServiceContainer( [ 'Foo' ] );
295 $theService1 = new stdClass();
296 $name = 'TestService92834576';
298 $services->defineService( $name, function () {
302 // disable the service. we should be able to redefine it anyway.
303 $services->disableService( $name );
305 $services->redefineService( $name, function () use ( $theService1 ) {
309 // force instantiation, check result
310 $this->assertSame( $theService1, $services->getService( $name ) );
313 public function testRedefineService_fail_undefined() {
314 $services = $this->newServiceContainer();
316 $theService = new stdClass();
317 $name = 'TestService92834576';
319 $this->setExpectedException( MediaWiki\Services\NoSuchServiceException
::class );
321 $services->redefineService( $name, function () use ( $theService ) {
326 public function testRedefineService_fail_in_use() {
327 $services = $this->newServiceContainer( [ 'Foo' ] );
329 $theService = new stdClass();
330 $name = 'TestService92834576';
332 $services->defineService( $name, function () {
336 // create the service, so it can no longer be redefined
337 $services->getService( $name );
339 $this->setExpectedException( MediaWiki\Services\CannotReplaceActiveServiceException
::class );
341 $services->redefineService( $name, function () use ( $theService ) {
346 public function testAddServiceManipulator() {
347 $services = $this->newServiceContainer( [ 'Foo' ] );
349 $theService1 = new stdClass();
350 $theService2 = new stdClass();
351 $name = 'TestService92834576';
353 $services->defineService(
355 function ( $actualLocator, $extra ) use ( $services, $theService1 ) {
356 PHPUnit_Framework_Assert
::assertSame( $services, $actualLocator );
357 PHPUnit_Framework_Assert
::assertSame( 'Foo', $extra );
362 $services->addServiceManipulator(
365 $theService, $actualLocator, $extra
367 $services, $theService1, $theService2
369 PHPUnit_Framework_Assert
::assertSame( $theService1, $theService );
370 PHPUnit_Framework_Assert
::assertSame( $services, $actualLocator );
371 PHPUnit_Framework_Assert
::assertSame( 'Foo', $extra );
376 // force instantiation, check result
377 $this->assertSame( $theService2, $services->getService( $name ) );
380 public function testAddServiceManipulator_fail_undefined() {
381 $services = $this->newServiceContainer();
383 $theService = new stdClass();
384 $name = 'TestService92834576';
386 $this->setExpectedException( MediaWiki\Services\NoSuchServiceException
::class );
388 $services->addServiceManipulator( $name, function () use ( $theService ) {
393 public function testAddServiceManipulator_fail_in_use() {
394 $services = $this->newServiceContainer( [ 'Foo' ] );
396 $theService = new stdClass();
397 $name = 'TestService92834576';
399 $services->defineService( $name, function () use ( $theService ) {
403 // create the service, so it can no longer be redefined
404 $services->getService( $name );
406 $this->setExpectedException( MediaWiki\Services\CannotReplaceActiveServiceException
::class );
408 $services->addServiceManipulator( $name, function () {
413 public function testDisableService() {
414 $services = $this->newServiceContainer( [ 'Foo' ] );
416 $destructible = $this->getMockBuilder( MediaWiki\Services\DestructibleService
::class )
418 $destructible->expects( $this->once() )
419 ->method( 'destroy' );
421 $services->defineService( 'Foo', function () use ( $destructible ) {
422 return $destructible;
424 $services->defineService( 'Bar', function () {
425 return new stdClass();
427 $services->defineService( 'Qux', function () {
428 return new stdClass();
431 // instantiate Foo and Bar services
432 $services->getService( 'Foo' );
433 $services->getService( 'Bar' );
435 // disable service, should call destroy() once.
436 $services->disableService( 'Foo' );
438 // disabled service should still be listed
439 $this->assertContains( 'Foo', $services->getServiceNames() );
441 // getting other services should still work
442 $services->getService( 'Bar' );
444 // disable non-destructible service, and not-yet-instantiated service
445 $services->disableService( 'Bar' );
446 $services->disableService( 'Qux' );
448 $this->assertNull( $services->peekService( 'Bar' ) );
449 $this->assertNull( $services->peekService( 'Qux' ) );
451 // disabled service should still be listed
452 $this->assertContains( 'Bar', $services->getServiceNames() );
453 $this->assertContains( 'Qux', $services->getServiceNames() );
455 $this->setExpectedException( MediaWiki\Services\ServiceDisabledException
::class );
456 $services->getService( 'Qux' );
459 public function testDisableService_fail_undefined() {
460 $services = $this->newServiceContainer();
462 $theService = new stdClass();
463 $name = 'TestService92834576';
465 $this->setExpectedException( MediaWiki\Services\NoSuchServiceException
::class );
467 $services->redefineService( $name, function () use ( $theService ) {
472 public function testDestroy() {
473 $services = $this->newServiceContainer();
475 $destructible = $this->getMockBuilder( MediaWiki\Services\DestructibleService
::class )
477 $destructible->expects( $this->once() )
478 ->method( 'destroy' );
480 $services->defineService( 'Foo', function () use ( $destructible ) {
481 return $destructible;
484 $services->defineService( 'Bar', function () {
485 return new stdClass();
488 // create the service
489 $services->getService( 'Foo' );
491 // destroy the container
492 $services->destroy();
494 $this->setExpectedException( MediaWiki\Services\ContainerDisabledException
::class );
495 $services->getService( 'Bar' );