diff --git a/control/injector/Injector.php b/control/injector/Injector.php index cfe06843e..ddd00616a 100644 --- a/control/injector/Injector.php +++ b/control/injector/Injector.php @@ -692,6 +692,7 @@ class Injector { * Register a service with an explicit name */ public function registerNamedService($name, $service) { + $this->specs[$name] = array('class' => get_class($service)); $this->serviceCache[$name] = $service; $this->inject($service); } diff --git a/tests/injector/InjectorTest.php b/tests/injector/InjectorTest.php index 297ba3af2..3a72cd1be 100644 --- a/tests/injector/InjectorTest.php +++ b/tests/injector/InjectorTest.php @@ -523,6 +523,15 @@ class InjectorTest extends SapphireTest { $this->assertInstanceOf('OtherTestObject', $item->property->property); } + + public function testNamedServices() { + $injector = new Injector(); + $service = new stdClass(); + + $injector->registerNamedService('NamedService', $service); + $this->assertEquals($service, $injector->get('NamedService')); + } + } class InjectorTestConfigLocator extends SilverStripeServiceConfigurationLocator implements TestOnly {