$this->assertEmpty( $names );
$name = 'TestService92834576';
- $services->defineService( $name, function() {
+ $services->defineService( $name, function () {
return null;
} );
$name = 'TestService92834576';
$this->assertFalse( $services->hasService( $name ) );
- $services->defineService( $name, function() {
+ $services->defineService( $name, function () {
return null;
} );
$services->defineService(
$name,
- function( $actualLocator, $extra ) use ( $services, $theService, &$count ) {
+ function ( $actualLocator, $extra ) use ( $services, $theService, &$count ) {
$count++;
PHPUnit_Framework_Assert::assertSame( $services, $actualLocator );
PHPUnit_Framework_Assert::assertSame( $extra, 'Foo' );
$services->defineService(
'Foo',
- function() {
+ function () {
return new stdClass();
}
);
$services->defineService(
'Bar',
- function() {
+ function () {
return new stdClass();
}
);
$theService = new stdClass();
$name = 'TestService92834576';
- $services->defineService( $name, function( $actualLocator ) use ( $services, $theService ) {
+ $services->defineService( $name, function ( $actualLocator ) use ( $services, $theService ) {
PHPUnit_Framework_Assert::assertSame( $services, $actualLocator );
return $theService;
} );
$theService = new stdClass();
$name = 'TestService92834576';
- $services->defineService( $name, function() use ( $theService ) {
+ $services->defineService( $name, function () use ( $theService ) {
return $theService;
} );
$this->setExpectedException( 'MediaWiki\Services\ServiceAlreadyDefinedException' );
- $services->defineService( $name, function() use ( $theService ) {
+ $services->defineService( $name, function () use ( $theService ) {
return $theService;
} );
}
$services = $this->newServiceContainer();
$wiring = [
- 'Foo' => function() {
+ 'Foo' => function () {
return 'Foo!';
},
- 'Bar' => function() {
+ 'Bar' => function () {
return 'Bar!';
},
];
$this->assertSame( 'Bar!', $services->getService( 'Bar' ) );
}
+ public function testImportWiring() {
+ $services = $this->newServiceContainer();
+
+ $wiring = [
+ 'Foo' => function () {
+ return 'Foo!';
+ },
+ 'Bar' => function () {
+ return 'Bar!';
+ },
+ 'Car' => function () {
+ return 'FUBAR!';
+ },
+ ];
+
+ $services->applyWiring( $wiring );
+
+ $newServices = $this->newServiceContainer();
+
+ // define a service before importing, so we can later check that
+ // existing service instances survive importWiring()
+ $newServices->defineService( 'Car', function () {
+ return 'Car!';
+ } );
+
+ // force instantiation
+ $newServices->getService( 'Car' );
+
+ // Define another service, so we can later check that extra wiring
+ // is not lost.
+ $newServices->defineService( 'Xar', function () {
+ return 'Xar!';
+ } );
+
+ // import wiring, but skip `Bar`
+ $newServices->importWiring( $services, [ 'Bar' ] );
+
+ $this->assertNotContains( 'Bar', $newServices->getServiceNames(), 'Skip `Bar` service' );
+ $this->assertSame( 'Foo!', $newServices->getService( 'Foo' ) );
+
+ // import all wiring, but preserve existing service instance
+ $newServices->importWiring( $services );
+
+ $this->assertContains( 'Bar', $newServices->getServiceNames(), 'Import all services' );
+ $this->assertSame( 'Bar!', $newServices->getService( 'Bar' ) );
+ $this->assertSame( 'Car!', $newServices->getService( 'Car' ), 'Use existing service instance' );
+ $this->assertSame( 'Xar!', $newServices->getService( 'Xar' ), 'Predefined services are kept' );
+ }
+
public function testLoadWiringFiles() {
$services = $this->newServiceContainer();
$theService1 = new stdClass();
$name = 'TestService92834576';
- $services->defineService( $name, function() {
+ $services->defineService( $name, function () {
PHPUnit_Framework_Assert::fail(
'The original instantiator function should not get called'
);
// redefine before instantiation
$services->redefineService(
$name,
- function( $actualLocator, $extra ) use ( $services, $theService1 ) {
+ function ( $actualLocator, $extra ) use ( $services, $theService1 ) {
PHPUnit_Framework_Assert::assertSame( $services, $actualLocator );
PHPUnit_Framework_Assert::assertSame( 'Foo', $extra );
return $theService1;
$this->assertSame( $theService1, $services->getService( $name ) );
}
+ public function testRedefineService_disabled() {
+ $services = $this->newServiceContainer( [ 'Foo' ] );
+
+ $theService1 = new stdClass();
+ $name = 'TestService92834576';
+
+ $services->defineService( $name, function () {
+ return 'Foo';
+ } );
+
+ // disable the service. we should be able to redefine it anyway.
+ $services->disableService( $name );
+
+ $services->redefineService( $name, function () use ( $theService1 ) {
+ return $theService1;
+ } );
+
+ // force instantiation, check result
+ $this->assertSame( $theService1, $services->getService( $name ) );
+ }
+
public function testRedefineService_fail_undefined() {
$services = $this->newServiceContainer();
$this->setExpectedException( 'MediaWiki\Services\NoSuchServiceException' );
- $services->redefineService( $name, function() use ( $theService ) {
+ $services->redefineService( $name, function () use ( $theService ) {
return $theService;
} );
}
$theService = new stdClass();
$name = 'TestService92834576';
- $services->defineService( $name, function() {
+ $services->defineService( $name, function () {
return 'Foo';
} );
$this->setExpectedException( 'MediaWiki\Services\CannotReplaceActiveServiceException' );
- $services->redefineService( $name, function() use ( $theService ) {
+ $services->redefineService( $name, function () use ( $theService ) {
return $theService;
} );
}
public function testDisableService() {
$services = $this->newServiceContainer( [ 'Foo' ] );
- $destructible = $this->getMock( 'MediaWiki\Services\DestructibleService' );
+ $destructible = $this->getMockBuilder( 'MediaWiki\Services\DestructibleService' )
+ ->getMock();
$destructible->expects( $this->once() )
->method( 'destroy' );
- $services->defineService( 'Foo', function() use ( $destructible ) {
+ $services->defineService( 'Foo', function () use ( $destructible ) {
return $destructible;
} );
- $services->defineService( 'Bar', function() {
+ $services->defineService( 'Bar', function () {
return new stdClass();
} );
- $services->defineService( 'Qux', function() {
+ $services->defineService( 'Qux', function () {
return new stdClass();
} );
$this->assertContains( 'Bar', $services->getServiceNames() );
$this->assertContains( 'Qux', $services->getServiceNames() );
- // re-enable Bar service
- $services->redefineService( 'Bar', function() {
- return new stdClass();
- } );
-
- $services->getService( 'Bar' );
-
$this->setExpectedException( 'MediaWiki\Services\ServiceDisabledException' );
$services->getService( 'Qux' );
}
$this->setExpectedException( 'MediaWiki\Services\NoSuchServiceException' );
- $services->redefineService( $name, function() use ( $theService ) {
+ $services->redefineService( $name, function () use ( $theService ) {
return $theService;
} );
}
public function testDestroy() {
$services = $this->newServiceContainer();
- $destructible = $this->getMock( 'MediaWiki\Services\DestructibleService' );
+ $destructible = $this->getMockBuilder( 'MediaWiki\Services\DestructibleService' )
+ ->getMock();
$destructible->expects( $this->once() )
->method( 'destroy' );
- $services->defineService( 'Foo', function() use ( $destructible ) {
+ $services->defineService( 'Foo', function () use ( $destructible ) {
return $destructible;
} );
- $services->defineService( 'Bar', function() {
+ $services->defineService( 'Bar', function () {
return new stdClass();
} );