Skip to content

Commit

Permalink
HasOneStrategy now uses the PrototypeStrategy for object creation
Browse files Browse the repository at this point in the history
  • Loading branch information
leogr committed Jul 28, 2015
1 parent 0ca01a5 commit 3ea0fb7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
10 changes: 8 additions & 2 deletions library/Hydrator/Strategy/HasOneStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,19 @@
use Matryoshka\Model\Exception;
use Zend\Stdlib\Hydrator\HydratorAwareInterface;
use Zend\Stdlib\Hydrator\Strategy\StrategyInterface;
use Matryoshka\Model\Object\PrototypeStrategy\PrototypeStrategyAwareTrait;
use Matryoshka\Model\Object\PrototypeStrategy\PrototypeStrategyAwareInterface;

/**
* Class HasOneStrategy
*/
class HasOneStrategy implements StrategyInterface, NullableStrategyInterface
class HasOneStrategy implements
StrategyInterface,
NullableStrategyInterface,
PrototypeStrategyAwareInterface
{
use NullableStrategyTrait;
use PrototypeStrategyAwareTrait;

/**
* @var HydratorAwareInterface
Expand Down Expand Up @@ -84,7 +90,7 @@ public function hydrate($value)
$objectPrototype = $this->getObjectPrototype();

if (is_array($value)) {
$object = clone $objectPrototype;
$object = $this->getPrototypeStrategy()->createObject($objectPrototype, $value);
return $object->getHydrator()->hydrate($value, $object);
}

Expand Down
22 changes: 22 additions & 0 deletions tests/Hydrator/Strategy/HasOneStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace MatryoshkaTest\Model\Hydrator\Strategy;

use Matryoshka\Model\Hydrator\Strategy\HasOneStrategy;
use Matryoshka\Model\Object\PrototypeStrategy\PrototypeStrategyAwareInterface;

/**
* Class HasOneStrategyTest
Expand Down Expand Up @@ -64,4 +65,25 @@ public function testExtract()
$this->setExpectedException('\Matryoshka\Model\Exception\InvalidArgumentException');
$strategy->extract('wrong value');
}

public function testPrototypeStrategy()
{
$abstractObject = $this->getMockForAbstractClass('\Matryoshka\Model\Object\AbstractObject');
$strategy = new HasOneStrategy($abstractObject);

$this->assertInstanceOf('\Matryoshka\Model\Object\PrototypeStrategy\PrototypeStrategyAwareInterface', $strategy);

$prototypeStrategy = $this->getMockForAbstractClass(
'\Matryoshka\Model\Object\PrototypeStrategy\PrototypeStrategyInterface'
);

$prototypeStrategy->expects($this->atLeastOnce())
->method('createObject')
->with($this->equalTo($abstractObject))
->willReturn($abstractObject);

$strategy->setPrototypeStrategy($prototypeStrategy);
$this->assertInstanceOf('\Matryoshka\Model\Object\AbstractObject', $strategy->hydrate([]));

}
}

0 comments on commit 3ea0fb7

Please sign in to comment.