Skip to content
This repository has been archived by the owner on Feb 6, 2020. It is now read-only.

Commit

Permalink
Merge branch 'hotfix/3424'
Browse files Browse the repository at this point in the history
  • Loading branch information
weierophinney committed Jan 14, 2013
2 parents 9f8afee + 6ba3916 commit 86f0f77
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/ServiceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ public function setInvokableClass($name, $invokableClass, $shared = true)
{
$cName = $this->canonicalizeName($name);

if ($this->has(array($cName, $name), false)) {
if ($this->has(array($cName, $name), false, false)) {
if ($this->allowOverride === false) {
throw new Exception\InvalidServiceNameException(sprintf(
'A service by the name or alias "%s" already exists and cannot be overridden; please use an alternate name',
Expand Down Expand Up @@ -263,7 +263,7 @@ public function setFactory($name, $factory, $shared = true)
);
}

if ($this->has(array($cName, $name), false)) {
if ($this->has(array($cName, $name), false, false)) {
if ($this->allowOverride === false) {
throw new Exception\InvalidServiceNameException(sprintf(
'A service by the name or alias "%s" already exists and cannot be overridden, please use an alternate name',
Expand Down Expand Up @@ -356,7 +356,7 @@ public function setService($name, $service, $shared = true)
{
$cName = $this->canonicalizeName($name);

if ($this->has($cName, false)) {
if ($this->has($cName, false, false)) {
if ($this->allowOverride === false) {
throw new Exception\InvalidServiceNameException(sprintf(
'%s: A service by the name "%s" or alias already exists and cannot be overridden, please use an alternate name.',
Expand Down Expand Up @@ -425,7 +425,7 @@ public function get($name, $usePeeringServiceManagers = true)
if ($usePeeringServiceManagers && $retrieveFromPeeringManagerFirst) {
$instance = $this->retrieveFromPeeringManager($name);

if(null !== $instance) {
if (null !== $instance) {
return $instance;
}
}
Expand Down Expand Up @@ -630,7 +630,7 @@ public function setAlias($alias, $nameOrAlias)
throw new Exception\InvalidServiceNameException('Invalid service name alias');
}

if ($this->allowOverride === false && $this->has(array($cAlias, $alias), false)) {
if ($this->allowOverride === false && $this->has(array($cAlias, $alias), false, false)) {
throw new Exception\InvalidServiceNameException(sprintf(
'An alias by the name "%s" or "%s" already exists',
$cAlias,
Expand Down
90 changes: 90 additions & 0 deletions test/ServiceManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -699,4 +699,94 @@ public function testRetrieveServiceFromPeeringServiceManagerIfretrieveFromPeerin
$this->assertEquals($serviceManagerChild->get($foo1), $boo2);
$this->assertEquals($this->serviceManager->get($foo1), $boo2);
}

/**
* @covers Zend\ServiceManager\ServiceManager::setService
* @covers Zend\ServiceManager\ServiceManager::get
* @covers Zend\ServiceManager\ServiceManager::createScopedServiceManager
*/
public function testParentServiceManagerCanCreateServiceWithSameNameThatAlreadyUsedByChildServiceManager()
{
$foo1 = "foo1";
$boo1 = "boo1";
$boo2 = "boo2";

$this->serviceManager->setService($foo1, $boo1);
$this->assertEquals($this->serviceManager->get($foo1), $boo1);

$serviceManagerParent = $this->serviceManager->createScopedServiceManager();

$this->assertContains($this->serviceManager, $this->readAttribute($serviceManagerParent, 'peeringServiceManagers'));

$serviceManagerParent->setService($foo1, $boo2);
$this->assertEquals($serviceManagerParent->get($foo1), $boo2);
}

/**
* @covers Zend\ServiceManager\ServiceManager::setInvokableClass
* @covers Zend\ServiceManager\ServiceManager::get
* @covers Zend\ServiceManager\ServiceManager::createScopedServiceManager
*/
public function testParentServiceManagerCanCreateInvokableServiceWithSameNameThatAlreadyUsedByChildServiceManager()
{
$foo1 = "foo1";
$boo1 = "ZendTest\ServiceManager\TestAsset\Foo";
$boo2 = "\stdClass";

$this->serviceManager->setInvokableClass($foo1, $boo1);
$this->assertInstanceOf($boo1, $this->serviceManager->get($foo1));

$serviceManagerParent = $this->serviceManager->createScopedServiceManager();

$this->assertContains($this->serviceManager, $this->readAttribute($serviceManagerParent, 'peeringServiceManagers'));

$serviceManagerParent->setInvokableClass($foo1, $boo2);
$this->assertInstanceOf($boo2, $serviceManagerParent->get($foo1));
}

/**
* @covers Zend\ServiceManager\ServiceManager::setFactory
* @covers Zend\ServiceManager\ServiceManager::get
* @covers Zend\ServiceManager\ServiceManager::createScopedServiceManager
*/
public function testParentServiceManagerCanCreateFactoryServiceWithSameNameThatAlreadyUsedByChildServiceManager()
{
$foo1 = "foo1";
$boo1 = "ZendTest\ServiceManager\TestAsset\FooFactory";
$boo1_returns = "ZendTest\ServiceManager\TestAsset\Foo";
$boo2 = function() { return new \stdClass(); };

$this->serviceManager->setFactory($foo1, $boo1);
$this->assertInstanceOf($boo1_returns, $this->serviceManager->get($foo1));

$serviceManagerParent = $this->serviceManager->createScopedServiceManager();

$this->assertContains($this->serviceManager, $this->readAttribute($serviceManagerParent, 'peeringServiceManagers'));

$serviceManagerParent->setFactory($foo1, $boo2);
$this->assertTrue($serviceManagerParent->get($foo1) instanceof \stdClass);
}

/**
* @covers Zend\ServiceManager\ServiceManager::setAlias
* @covers Zend\ServiceManager\ServiceManager::hasAlias
* @covers Zend\ServiceManager\ServiceManager::createScopedServiceManager
*/
public function testParentServiceManagerCanCreateAliasWithSameNameThatAlreadyUsedByChildServiceManager()
{
$foo1 = "foo1";
$boo1 = "boo1";
$boo2 = "boo2";

$this->serviceManager->setAlias($foo1, $boo1);
$this->assertTrue($this->serviceManager->hasAlias($foo1));

$serviceManagerParent = $this->serviceManager->createScopedServiceManager();

$this->assertFalse($serviceManagerParent->hasAlias($foo1));
$this->assertContains($this->serviceManager, $this->readAttribute($serviceManagerParent, 'peeringServiceManagers'));

$serviceManagerParent->setAlias($foo1, $boo2);
$this->assertTrue($serviceManagerParent->hasAlias($foo1));
}
}

0 comments on commit 86f0f77

Please sign in to comment.