diff --git a/src/Joomla/DI/Container.php b/src/Joomla/DI/Container.php index e1e9c29b5..0b1c894be 100644 --- a/src/Joomla/DI/Container.php +++ b/src/Joomla/DI/Container.php @@ -340,5 +340,21 @@ public function getNewInstance($key) { return $this->get($key, true); } + + /** + * Register a service provider to the container. + * + * @param ServiceProviderInterface $provider + * + * @return Container This object for chaining. + * + * @since 1.0 + */ + public function registerServiceProvider(ServiceProviderInterface $provider) + { + $provider->register($this); + + return $this; + } } diff --git a/src/Joomla/DI/ServiceProviderInterface.php b/src/Joomla/DI/ServiceProviderInterface.php new file mode 100644 index 000000000..e1a7da363 --- /dev/null +++ b/src/Joomla/DI/ServiceProviderInterface.php @@ -0,0 +1,28 @@ +assertNotSame($this->fixture->getNewInstance('foo'), $this->fixture->getNewInstance('foo')); } + + /** + * Test registering a service provider. Make sure register get's called. + * + * @return void + * + * @since 1.0 + */ + public function testRegisterServiceProvider() + { + $mock = $this->getMock('Joomla\\DI\\ServiceProviderInterface'); + + $mock->expects($this->once()) + ->method('register'); + + $returned = $this->fixture->registerServiceProvider($mock); + + $this->assertSame( + $returned, + $this->fixture, + 'When registering a service provider, the container instance should be returned.' + ); + } }