*
* @group MediaWiki
*/
-class ServiceContainerTest extends PHPUnit_Framework_TestCase {
+class ServiceContainerTest extends PHPUnit\Framework\TestCase {
use MediaWikiCoversValidator;
+ use PHPUnit4And6Compat;
private function newServiceContainer( $extraArgs = [] ) {
return new ServiceContainer( $extraArgs );
$name = 'TestService92834576';
- $this->setExpectedException( 'MediaWiki\Services\NoSuchServiceException' );
+ $this->setExpectedException( MediaWiki\Services\NoSuchServiceException::class );
$services->getService( $name );
}
$name = 'TestService92834576';
- $this->setExpectedException( 'MediaWiki\Services\NoSuchServiceException' );
+ $this->setExpectedException( MediaWiki\Services\NoSuchServiceException::class );
$services->peekService( $name );
}
return $theService;
} );
- $this->setExpectedException( 'MediaWiki\Services\ServiceAlreadyDefinedException' );
+ $this->setExpectedException( MediaWiki\Services\ServiceAlreadyDefinedException::class );
$services->defineService( $name, function () use ( $theService ) {
return $theService;
$services->applyWiring( $wiring );
+ $services->addServiceManipulator( 'Foo', function ( $service ) {
+ return $service . '+X';
+ } );
+
+ $services->addServiceManipulator( 'Car', function ( $service ) {
+ return $service . '+X';
+ } );
+
$newServices = $this->newServiceContainer();
- // define a service before importing, so we can later check that
+ // create a service with manipulator
+ $newServices->defineService( 'Foo', function () {
+ return 'Foo!';
+ } );
+
+ $newServices->addServiceManipulator( 'Foo', function ( $service ) {
+ return $service . '+Y';
+ } );
+
+ // create a service before importing, so we can later check that
// existing service instances survive importWiring()
$newServices->defineService( 'Car', function () {
return 'Car!';
$newServices->importWiring( $services, [ 'Bar' ] );
$this->assertNotContains( 'Bar', $newServices->getServiceNames(), 'Skip `Bar` service' );
- $this->assertSame( 'Foo!', $newServices->getService( 'Foo' ) );
+ $this->assertSame( 'Foo!+Y+X', $newServices->getService( 'Foo' ) );
// import all wiring, but preserve existing service instance
$newServices->importWiring( $services );
];
// loading the same file twice should fail, because
- $this->setExpectedException( 'MediaWiki\Services\ServiceAlreadyDefinedException' );
+ $this->setExpectedException( MediaWiki\Services\ServiceAlreadyDefinedException::class );
$services->loadWiringFiles( $wiringFiles );
}
$theService = new stdClass();
$name = 'TestService92834576';
- $this->setExpectedException( 'MediaWiki\Services\NoSuchServiceException' );
+ $this->setExpectedException( MediaWiki\Services\NoSuchServiceException::class );
$services->redefineService( $name, function () use ( $theService ) {
return $theService;
// create the service, so it can no longer be redefined
$services->getService( $name );
- $this->setExpectedException( 'MediaWiki\Services\CannotReplaceActiveServiceException' );
+ $this->setExpectedException( MediaWiki\Services\CannotReplaceActiveServiceException::class );
$services->redefineService( $name, function () use ( $theService ) {
return $theService;
} );
}
+ public function testAddServiceManipulator() {
+ $services = $this->newServiceContainer( [ 'Foo' ] );
+
+ $theService1 = new stdClass();
+ $theService2 = new stdClass();
+ $name = 'TestService92834576';
+
+ $services->defineService(
+ $name,
+ function ( $actualLocator, $extra ) use ( $services, $theService1 ) {
+ PHPUnit_Framework_Assert::assertSame( $services, $actualLocator );
+ PHPUnit_Framework_Assert::assertSame( 'Foo', $extra );
+ return $theService1;
+ }
+ );
+
+ $services->addServiceManipulator(
+ $name,
+ function (
+ $theService, $actualLocator, $extra
+ ) use (
+ $services, $theService1, $theService2
+ ) {
+ PHPUnit_Framework_Assert::assertSame( $theService1, $theService );
+ PHPUnit_Framework_Assert::assertSame( $services, $actualLocator );
+ PHPUnit_Framework_Assert::assertSame( 'Foo', $extra );
+ return $theService2;
+ }
+ );
+
+ // force instantiation, check result
+ $this->assertSame( $theService2, $services->getService( $name ) );
+ }
+
+ public function testAddServiceManipulator_fail_undefined() {
+ $services = $this->newServiceContainer();
+
+ $theService = new stdClass();
+ $name = 'TestService92834576';
+
+ $this->setExpectedException( MediaWiki\Services\NoSuchServiceException::class );
+
+ $services->addServiceManipulator( $name, function () use ( $theService ) {
+ return $theService;
+ } );
+ }
+
+ public function testAddServiceManipulator_fail_in_use() {
+ $services = $this->newServiceContainer( [ 'Foo' ] );
+
+ $theService = new stdClass();
+ $name = 'TestService92834576';
+
+ $services->defineService( $name, function () use ( $theService ) {
+ return $theService;
+ } );
+
+ // create the service, so it can no longer be redefined
+ $services->getService( $name );
+
+ $this->setExpectedException( MediaWiki\Services\CannotReplaceActiveServiceException::class );
+
+ $services->addServiceManipulator( $name, function () {
+ return 'Foo';
+ } );
+ }
+
public function testDisableService() {
$services = $this->newServiceContainer( [ 'Foo' ] );
- $destructible = $this->getMockBuilder( 'MediaWiki\Services\DestructibleService' )
+ $destructible = $this->getMockBuilder( MediaWiki\Services\DestructibleService::class )
->getMock();
$destructible->expects( $this->once() )
->method( 'destroy' );
$this->assertContains( 'Bar', $services->getServiceNames() );
$this->assertContains( 'Qux', $services->getServiceNames() );
- $this->setExpectedException( 'MediaWiki\Services\ServiceDisabledException' );
+ $this->setExpectedException( MediaWiki\Services\ServiceDisabledException::class );
$services->getService( 'Qux' );
}
$theService = new stdClass();
$name = 'TestService92834576';
- $this->setExpectedException( 'MediaWiki\Services\NoSuchServiceException' );
+ $this->setExpectedException( MediaWiki\Services\NoSuchServiceException::class );
$services->redefineService( $name, function () use ( $theService ) {
return $theService;
public function testDestroy() {
$services = $this->newServiceContainer();
- $destructible = $this->getMockBuilder( 'MediaWiki\Services\DestructibleService' )
+ $destructible = $this->getMockBuilder( MediaWiki\Services\DestructibleService::class )
->getMock();
$destructible->expects( $this->once() )
->method( 'destroy' );
// destroy the container
$services->destroy();
- $this->setExpectedException( 'MediaWiki\Services\ContainerDisabledException' );
+ $this->setExpectedException( MediaWiki\Services\ContainerDisabledException::class );
$services->getService( 'Bar' );
}