2 use MediaWiki\Services\ServiceContainer
;
5 * @covers MediaWiki\Services\ServiceContainer
9 class ServiceContainerTest
extends PHPUnit_Framework_TestCase
{
11 private function newServiceContainer( $extraArgs = [] ) {
12 return new ServiceContainer( $extraArgs );
15 public function testGetServiceNames() {
16 $services = $this->newServiceContainer();
17 $names = $services->getServiceNames();
19 $this->assertInternalType( 'array', $names );
20 $this->assertEmpty( $names );
22 $name = 'TestService92834576';
23 $services->defineService( $name, function () {
27 $names = $services->getServiceNames();
28 $this->assertContains( $name, $names );
31 public function testHasService() {
32 $services = $this->newServiceContainer();
34 $name = 'TestService92834576';
35 $this->assertFalse( $services->hasService( $name ) );
37 $services->defineService( $name, function () {
41 $this->assertTrue( $services->hasService( $name ) );
44 public function testGetService() {
45 $services = $this->newServiceContainer( [ 'Foo' ] );
47 $theService = new stdClass();
48 $name = 'TestService92834576';
51 $services->defineService(
53 function ( $actualLocator, $extra ) use ( $services, $theService, &$count ) {
55 PHPUnit_Framework_Assert
::assertSame( $services, $actualLocator );
56 PHPUnit_Framework_Assert
::assertSame( $extra, 'Foo' );
61 $this->assertSame( $theService, $services->getService( $name ) );
63 $services->getService( $name );
64 $this->assertSame( 1, $count, 'instantiator should be called exactly once!' );
67 public function testGetService_fail_unknown() {
68 $services = $this->newServiceContainer();
70 $name = 'TestService92834576';
72 $this->setExpectedException( 'MediaWiki\Services\NoSuchServiceException' );
74 $services->getService( $name );
77 public function testPeekService() {
78 $services = $this->newServiceContainer();
80 $services->defineService(
83 return new stdClass();
87 $services->defineService(
90 return new stdClass();
94 // trigger instantiation of Foo
95 $services->getService( 'Foo' );
97 $this->assertInternalType(
99 $services->peekService( 'Foo' ),
100 'Peek should return the service object if it had been accessed before.'
104 $services->peekService( 'Bar' ),
105 'Peek should return null if the service was never accessed.'
109 public function testPeekService_fail_unknown() {
110 $services = $this->newServiceContainer();
112 $name = 'TestService92834576';
114 $this->setExpectedException( 'MediaWiki\Services\NoSuchServiceException' );
116 $services->peekService( $name );
119 public function testDefineService() {
120 $services = $this->newServiceContainer();
122 $theService = new stdClass();
123 $name = 'TestService92834576';
125 $services->defineService( $name, function ( $actualLocator ) use ( $services, $theService ) {
126 PHPUnit_Framework_Assert
::assertSame( $services, $actualLocator );
130 $this->assertTrue( $services->hasService( $name ) );
131 $this->assertSame( $theService, $services->getService( $name ) );
134 public function testDefineService_fail_duplicate() {
135 $services = $this->newServiceContainer();
137 $theService = new stdClass();
138 $name = 'TestService92834576';
140 $services->defineService( $name, function () use ( $theService ) {
144 $this->setExpectedException( 'MediaWiki\Services\ServiceAlreadyDefinedException' );
146 $services->defineService( $name, function () use ( $theService ) {
151 public function testApplyWiring() {
152 $services = $this->newServiceContainer();
155 'Foo' => function () {
158 'Bar' => function () {
163 $services->applyWiring( $wiring );
165 $this->assertSame( 'Foo!', $services->getService( 'Foo' ) );
166 $this->assertSame( 'Bar!', $services->getService( 'Bar' ) );
169 public function testImportWiring() {
170 $services = $this->newServiceContainer();
173 'Foo' => function () {
176 'Bar' => function () {
179 'Car' => function () {
184 $services->applyWiring( $wiring );
186 $newServices = $this->newServiceContainer();
188 // define a service before importing, so we can later check that
189 // existing service instances survive importWiring()
190 $newServices->defineService( 'Car', function () {
194 // force instantiation
195 $newServices->getService( 'Car' );
197 // Define another service, so we can later check that extra wiring
199 $newServices->defineService( 'Xar', function () {
203 // import wiring, but skip `Bar`
204 $newServices->importWiring( $services, [ 'Bar' ] );
206 $this->assertNotContains( 'Bar', $newServices->getServiceNames(), 'Skip `Bar` service' );
207 $this->assertSame( 'Foo!', $newServices->getService( 'Foo' ) );
209 // import all wiring, but preserve existing service instance
210 $newServices->importWiring( $services );
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' );
218 public function testLoadWiringFiles() {
219 $services = $this->newServiceContainer();
222 __DIR__
. '/TestWiring1.php',
223 __DIR__
. '/TestWiring2.php',
226 $services->loadWiringFiles( $wiringFiles );
228 $this->assertSame( 'Foo!', $services->getService( 'Foo' ) );
229 $this->assertSame( 'Bar!', $services->getService( 'Bar' ) );
232 public function testLoadWiringFiles_fail_duplicate() {
233 $services = $this->newServiceContainer();
236 __DIR__
. '/TestWiring1.php',
237 __DIR__
. '/./TestWiring1.php',
240 // loading the same file twice should fail, because
241 $this->setExpectedException( 'MediaWiki\Services\ServiceAlreadyDefinedException' );
243 $services->loadWiringFiles( $wiringFiles );
246 public function testRedefineService() {
247 $services = $this->newServiceContainer( [ 'Foo' ] );
249 $theService1 = new stdClass();
250 $name = 'TestService92834576';
252 $services->defineService( $name, function () {
253 PHPUnit_Framework_Assert
::fail(
254 'The original instantiator function should not get called'
258 // redefine before instantiation
259 $services->redefineService(
261 function ( $actualLocator, $extra ) use ( $services, $theService1 ) {
262 PHPUnit_Framework_Assert
::assertSame( $services, $actualLocator );
263 PHPUnit_Framework_Assert
::assertSame( 'Foo', $extra );
268 // force instantiation, check result
269 $this->assertSame( $theService1, $services->getService( $name ) );
272 public function testRedefineService_disabled() {
273 $services = $this->newServiceContainer( [ 'Foo' ] );
275 $theService1 = new stdClass();
276 $name = 'TestService92834576';
278 $services->defineService( $name, function () {
282 // disable the service. we should be able to redefine it anyway.
283 $services->disableService( $name );
285 $services->redefineService( $name, function () use ( $theService1 ) {
289 // force instantiation, check result
290 $this->assertSame( $theService1, $services->getService( $name ) );
293 public function testRedefineService_fail_undefined() {
294 $services = $this->newServiceContainer();
296 $theService = new stdClass();
297 $name = 'TestService92834576';
299 $this->setExpectedException( 'MediaWiki\Services\NoSuchServiceException' );
301 $services->redefineService( $name, function () use ( $theService ) {
306 public function testRedefineService_fail_in_use() {
307 $services = $this->newServiceContainer( [ 'Foo' ] );
309 $theService = new stdClass();
310 $name = 'TestService92834576';
312 $services->defineService( $name, function () {
316 // create the service, so it can no longer be redefined
317 $services->getService( $name );
319 $this->setExpectedException( 'MediaWiki\Services\CannotReplaceActiveServiceException' );
321 $services->redefineService( $name, function () use ( $theService ) {
326 public function testDisableService() {
327 $services = $this->newServiceContainer( [ 'Foo' ] );
329 $destructible = $this->getMockBuilder( 'MediaWiki\Services\DestructibleService' )
331 $destructible->expects( $this->once() )
332 ->method( 'destroy' );
334 $services->defineService( 'Foo', function () use ( $destructible ) {
335 return $destructible;
337 $services->defineService( 'Bar', function () {
338 return new stdClass();
340 $services->defineService( 'Qux', function () {
341 return new stdClass();
344 // instantiate Foo and Bar services
345 $services->getService( 'Foo' );
346 $services->getService( 'Bar' );
348 // disable service, should call destroy() once.
349 $services->disableService( 'Foo' );
351 // disabled service should still be listed
352 $this->assertContains( 'Foo', $services->getServiceNames() );
354 // getting other services should still work
355 $services->getService( 'Bar' );
357 // disable non-destructible service, and not-yet-instantiated service
358 $services->disableService( 'Bar' );
359 $services->disableService( 'Qux' );
361 $this->assertNull( $services->peekService( 'Bar' ) );
362 $this->assertNull( $services->peekService( 'Qux' ) );
364 // disabled service should still be listed
365 $this->assertContains( 'Bar', $services->getServiceNames() );
366 $this->assertContains( 'Qux', $services->getServiceNames() );
368 $this->setExpectedException( 'MediaWiki\Services\ServiceDisabledException' );
369 $services->getService( 'Qux' );
372 public function testDisableService_fail_undefined() {
373 $services = $this->newServiceContainer();
375 $theService = new stdClass();
376 $name = 'TestService92834576';
378 $this->setExpectedException( 'MediaWiki\Services\NoSuchServiceException' );
380 $services->redefineService( $name, function () use ( $theService ) {
385 public function testDestroy() {
386 $services = $this->newServiceContainer();
388 $destructible = $this->getMockBuilder( 'MediaWiki\Services\DestructibleService' )
390 $destructible->expects( $this->once() )
391 ->method( 'destroy' );
393 $services->defineService( 'Foo', function () use ( $destructible ) {
394 return $destructible;
397 $services->defineService( 'Bar', function () {
398 return new stdClass();
401 // create the service
402 $services->getService( 'Foo' );
404 // destroy the container
405 $services->destroy();
407 $this->setExpectedException( 'MediaWiki\Services\ContainerDisabledException' );
408 $services->getService( 'Bar' );