build: Prepare for mediawiki/mediawiki-codesniffer to 0.9.0
[lhc/web/wiklou.git] / tests / phpunit / includes / Services / ServiceContainerTest.php
index 933777c..b68ee48 100644 (file)
@@ -20,7 +20,7 @@ class ServiceContainerTest extends PHPUnit_Framework_TestCase {
                $this->assertEmpty( $names );
 
                $name = 'TestService92834576';
-               $services->defineService( $name, function() {
+               $services->defineService( $name, function () {
                        return null;
                } );
 
@@ -34,7 +34,7 @@ class ServiceContainerTest extends PHPUnit_Framework_TestCase {
                $name = 'TestService92834576';
                $this->assertFalse( $services->hasService( $name ) );
 
-               $services->defineService( $name, function() {
+               $services->defineService( $name, function () {
                        return null;
                } );
 
@@ -50,7 +50,7 @@ class ServiceContainerTest extends PHPUnit_Framework_TestCase {
 
                $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' );
@@ -79,14 +79,14 @@ class ServiceContainerTest extends PHPUnit_Framework_TestCase {
 
                $services->defineService(
                        'Foo',
-                       function() {
+                       function () {
                                return new stdClass();
                        }
                );
 
                $services->defineService(
                        'Bar',
-                       function() {
+                       function () {
                                return new stdClass();
                        }
                );
@@ -122,7 +122,7 @@ class ServiceContainerTest extends PHPUnit_Framework_TestCase {
                $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;
                } );
@@ -137,13 +137,13 @@ class ServiceContainerTest extends PHPUnit_Framework_TestCase {
                $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;
                } );
        }
@@ -152,10 +152,10 @@ class ServiceContainerTest extends PHPUnit_Framework_TestCase {
                $services = $this->newServiceContainer();
 
                $wiring = [
-                       'Foo' => function() {
+                       'Foo' => function () {
                                return 'Foo!';
                        },
-                       'Bar' => function() {
+                       'Bar' => function () {
                                return 'Bar!';
                        },
                ];
@@ -166,6 +166,55 @@ class ServiceContainerTest extends PHPUnit_Framework_TestCase {
                $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();
 
@@ -200,7 +249,7 @@ class ServiceContainerTest extends PHPUnit_Framework_TestCase {
                $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'
                        );
@@ -209,7 +258,7 @@ class ServiceContainerTest extends PHPUnit_Framework_TestCase {
                // 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;
@@ -220,6 +269,27 @@ class ServiceContainerTest extends PHPUnit_Framework_TestCase {
                $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();
 
@@ -228,7 +298,7 @@ class ServiceContainerTest extends PHPUnit_Framework_TestCase {
 
                $this->setExpectedException( 'MediaWiki\Services\NoSuchServiceException' );
 
-               $services->redefineService( $name, function() use ( $theService ) {
+               $services->redefineService( $name, function () use ( $theService ) {
                        return $theService;
                } );
        }
@@ -239,7 +309,7 @@ class ServiceContainerTest extends PHPUnit_Framework_TestCase {
                $theService = new stdClass();
                $name = 'TestService92834576';
 
-               $services->defineService( $name, function() {
+               $services->defineService( $name, function () {
                        return 'Foo';
                } );
 
@@ -248,7 +318,7 @@ class ServiceContainerTest extends PHPUnit_Framework_TestCase {
 
                $this->setExpectedException( 'MediaWiki\Services\CannotReplaceActiveServiceException' );
 
-               $services->redefineService( $name, function() use ( $theService ) {
+               $services->redefineService( $name, function () use ( $theService ) {
                        return $theService;
                } );
        }
@@ -256,17 +326,18 @@ class ServiceContainerTest extends PHPUnit_Framework_TestCase {
        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();
                } );
 
@@ -294,13 +365,6 @@ class ServiceContainerTest extends PHPUnit_Framework_TestCase {
                $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' );
        }
@@ -313,7 +377,7 @@ class ServiceContainerTest extends PHPUnit_Framework_TestCase {
 
                $this->setExpectedException( 'MediaWiki\Services\NoSuchServiceException' );
 
-               $services->redefineService( $name, function() use ( $theService ) {
+               $services->redefineService( $name, function () use ( $theService ) {
                        return $theService;
                } );
        }
@@ -321,15 +385,16 @@ class ServiceContainerTest extends PHPUnit_Framework_TestCase {
        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();
                } );