Skip to content

Commit

Permalink
HasManyStrategy now passes the PrototypeStrategy to the composed
Browse files Browse the repository at this point in the history
HasOneStrategy
  • Loading branch information
leogr committed Jul 30, 2015
1 parent c9e63e9 commit 49279be
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
11 changes: 9 additions & 2 deletions library/Hydrator/Strategy/HasManyStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,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 HasManyStrategy
*/
class HasManyStrategy implements StrategyInterface, NullableStrategyInterface
class HasManyStrategy implements
StrategyInterface,
NullableStrategyInterface,
PrototypeStrategyAwareInterface
{
use NullableStrategyTrait;
use PrototypeStrategyAwareTrait;

/**
* @var HasOneStrategy
Expand All @@ -42,7 +48,7 @@ public function __construct(
\ArrayAccess $arrayObjectPrototype = null,
$nullable = true
) {
$this->hasOneStrategy = new HasOneStrategy($objectPrototype);
$this->hasOneStrategy = new HasOneStrategy($objectPrototype, false);
$this->arrayObjectPrototype = $arrayObjectPrototype ? $arrayObjectPrototype : new ArrayObject([], ArrayObject::ARRAY_AS_PROPS);
$this->setNullable($nullable);
}
Expand Down Expand Up @@ -94,6 +100,7 @@ public function hydrate($value)
return null;
}

$this->hasOneStrategy->setPrototypeStrategy($this->getPrototypeStrategy());
$return = clone $this->arrayObjectPrototype;
if (is_array($value) || $value instanceof \Traversable) {
foreach ($value as $key => $data) {
Expand Down
21 changes: 21 additions & 0 deletions tests/Hydrator/Strategy/HasManyStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,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 HasManyStrategy($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(\ArrayObject::class, $strategy->hydrate([[]]));

}
}

0 comments on commit 49279be

Please sign in to comment.