Skip to content

Commit

Permalink
Merge pull request #22 from matryoshka-model/develop
Browse files Browse the repository at this point in the history
Fixed error handling in ServiceLocatorStrategy when type field is not present
  • Loading branch information
leogr committed Mar 8, 2015
2 parents d60b97c + b7a3102 commit bfda9d7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
13 changes: 12 additions & 1 deletion library/ResultSet/PrototypeStrategy/ServiceLocatorStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Matryoshka\Model\Exception\ErrorException;
use Zend\ServiceManager\ServiceLocatorInterface;
use Matryoshka\Model\ModelAwareInterface;
use Matryoshka\Model\Exception\RuntimeException;

/**
* Class ServiceLocatorStrategy
Expand Down Expand Up @@ -131,10 +132,20 @@ public function getCloneObject()
*/
public function createObject($objectPrototype, array $context = null)
{
if (!isset($context[$this->typeField])) {
throw new RuntimeException(sprintf(
'"%s" is not present within object data',
$this->typeField
));
}

$object = $this->serviceLocator->get($context[$this->typeField]);

if ($this->validateObject && !($object instanceof $objectPrototype)) {
throw new ErrorException('Object must be an instance of $objectPrototype');
throw new ErrorException(sprintf(
'Object must be an instance of %s',
get_class($objectPrototype)
));
}

if ($this->cloneObject) {
Expand Down
10 changes: 10 additions & 0 deletions tests/ResultSet/PrototypeStrategy/ServiceLocatorStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,16 @@ public function testCreateObject()
$object = $strategy->createObject(new \stdClass(), $data);
}

public function testCreateShouldThrowExceptionWhenFieldIsNotPresent()
{
$strategy = new ServiceLocatorStrategy($this->serviceManager);
$myDomainObject = $this->serviceManager->get('MyDomainObject');
$data = ['foo' => 'bar'];

$this->setExpectedException('\Matryoshka\Model\Exception\RuntimeException');
$strategy->createObject($myDomainObject, $data);
}

public function testGetSetServiceLocator()
{
$strategy = new ServiceLocatorStrategy($this->serviceManager);
Expand Down

0 comments on commit bfda9d7

Please sign in to comment.