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

Commit

Permalink
Merge remote-tracking branch 'SpiffyJr/hotfix/sm-alias'
Browse files Browse the repository at this point in the history
  • Loading branch information
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
11 changes: 7 additions & 4 deletions src/ServiceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,13 @@ public function get($name, $usePeeringServiceManagers = true)
do {
$cName = $this->aliases[$cName];
} while ($this->hasAlias($cName));

if (!$this->has($cName)) {
throw new Exception\ServiceNotFoundException(sprintf(
'An alias "%s" was requested but no service could be found.',
$name
));
}
}

$instance = null;
Expand Down Expand Up @@ -477,10 +484,6 @@ public function setAlias($alias, $nameOrAlias)
throw new Exception\InvalidServiceNameException('An alias by this name already exists');
}

if (!$this->has($nameOrAlias)) {
throw new Exception\ServiceNotFoundException('A target service or target alias could not be located');
}

$this->aliases[$alias] = $nameOrAlias;
return $this;
}
Expand Down
22 changes: 21 additions & 1 deletion test/ServiceManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -303,10 +303,30 @@ public function testSetAliasThrowsExceptionOnDuplicateAlias()
/**
* @covers Zend\ServiceManager\ServiceManager::setAlias
*/
public function testSetAliasThrowExceptionOnServiceNotFound()
public function testSetAliasDoesNotThrowExceptionOnServiceNotFound()
{
$this->serviceManager->setAlias('foo', 'bar');
}

/**
* @covers Zend\ServiceManager\ServiceManager::get
*/
public function testGetServiceThrowsExceptionOnAliasWithNoSetService()
{
$this->setExpectedException('Zend\ServiceManager\Exception\ServiceNotFoundException');
$this->serviceManager->setAlias('foo', 'bar');
$this->serviceManager->get('foo');
}

/**
* @cover Zend\ServiceManager\ServiceManager::get
*/
public function testGetServiceThrowsExceptionOnMultipleAliasesWithNoSetService()
{
$this->setExpectedException('Zend\ServiceManager\Exception\ServiceNotFoundException');
$this->serviceManager->setAlias('foo', 'bar');
$this->serviceManager->setAlias('baz', 'foo');
$this->serviceManager->get('foo');
}

/**
Expand Down

0 comments on commit 39aca71

Please sign in to comment.