diff options
Diffstat (limited to 'tests/phpunit')
-rw-r--r-- | tests/phpunit/includes/services/ServiceContainerTest.php | 88 |
1 files changed, 86 insertions, 2 deletions
diff --git a/tests/phpunit/includes/services/ServiceContainerTest.php b/tests/phpunit/includes/services/ServiceContainerTest.php index a760908f9e2a..aca88aa4b68d 100644 --- a/tests/phpunit/includes/services/ServiceContainerTest.php +++ b/tests/phpunit/includes/services/ServiceContainerTest.php @@ -186,9 +186,26 @@ class ServiceContainerTest extends PHPUnit\Framework\TestCase { $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!'; @@ -207,7 +224,7 @@ class ServiceContainerTest extends PHPUnit\Framework\TestCase { $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 ); @@ -326,6 +343,73 @@ class ServiceContainerTest extends PHPUnit\Framework\TestCase { } ); } + 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' ] ); |