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

Commit

Permalink
Browse files Browse the repository at this point in the history
- Defined a number of closures that relied on the current method context.
  Unfortunately, this does not work in 5.3 unless you import the current object
  ($self = $this; use ($self) in the closure declaration).
- Renamed HydratorStrategyTest to remove naming conflicts
  • Loading branch information
weierophinney committed Aug 1, 2012
1 parent 5c1adbd commit 7bdd8b8
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 37 deletions.
10 changes: 6 additions & 4 deletions src/Hydrator/ArraySerializable.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ public function extract($object)
));
}

$self = $this;
$data = $object->getArrayCopy();
array_walk($data, function(&$value, $name) {
$value = $this->extractValue($name, $value);
array_walk($data, function(&$value, $name) use ($self) {
$value = $self->extractValue($name, $value);
});
return $data;
}
Expand All @@ -57,8 +58,9 @@ public function extract($object)
*/
public function hydrate(array $data, $object)
{
array_walk($data, function(&$value, $name) {
$value = $this->hydrateValue($name, $value);
$self = $this;
array_walk($data, function(&$value, $name) use ($self) {
$value = $self->hydrateValue($name, $value);
});

if (is_callable(array($object, 'exchangeArray'))) {
Expand Down
5 changes: 3 additions & 2 deletions src/Hydrator/ObjectProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ public function extract($object)
));
}

$self = $this;
$data = get_object_vars($object);
array_walk($data, function(&$value, $name) {
$value = $this->extractValue($name, $value);
array_walk($data, function(&$value, $name) use ($self) {
$value = $self->extractValue($name, $value);
});
return $data;
}
Expand Down
59 changes: 28 additions & 31 deletions test/HydratorStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,22 @@

use Zend\Stdlib\Hydrator\HydratorInterface;
use Zend\Stdlib\Hydrator\ClassMethods;
use ZendTest\StdLib\TestAsset\HydratorStrategy;
use ZendTest\StdLib\TestAsset\HydratorStrategyEntityA;
use ZendTest\StdLib\TestAsset\HydratorStrategyEntityB;

/**
* @category Zend
* @package Zend_Stdlib
* @subpackage UnitTests
* @group Zend_Stdlib
*/
class HydratorTest extends \PHPUnit_Framework_TestCase
class HydratorStrategyTest extends \PHPUnit_Framework_TestCase
{
/**
* The hydrator that is used during testing.
*
*
* @var HydratorInterface
*/
private $hydrator;

public function setUp()
{
$this->hydrator = new ClassMethods();
Expand All @@ -39,9 +36,9 @@ public function setUp()
public function testAddingStrategy()
{
$this->assertAttributeCount(0, 'strategies', $this->hydrator);
$this->hydrator->addStrategy('myStrategy', new HydratorStrategy());

$this->hydrator->addStrategy('myStrategy', new TestAsset\HydratorStrategy());

$this->assertAttributeCount(1, 'strategies', $this->hydrator);
}

Expand All @@ -52,58 +49,58 @@ public function testCheckStrategyEmpty()

public function testCheckStrategyNotEmpty()
{
$this->hydrator->addStrategy('myStrategy', new HydratorStrategy());
$this->hydrator->addStrategy('myStrategy', new TestAsset\HydratorStrategy());

$this->assertTrue($this->hydrator->hasStrategy('myStrategy'));
}

public function testRemovingStrategy()
{
$this->assertAttributeCount(0, 'strategies', $this->hydrator);
$this->hydrator->addStrategy('myStrategy', new HydratorStrategy());

$this->hydrator->addStrategy('myStrategy', new TestAsset\HydratorStrategy());
$this->assertAttributeCount(1, 'strategies', $this->hydrator);

$this->hydrator->removeStrategy('myStrategy');
$this->assertAttributeCount(0, 'strategies', $this->hydrator);
}

public function testRetrieveStrategy()
{
$strategy = new HydratorStrategy();
$strategy = new TestAsset\HydratorStrategy();
$this->hydrator->addStrategy('myStrategy', $strategy);

$this->assertEquals($strategy, $this->hydrator->getStrategy('myStrategy'));
}

public function testExtractingObjects()
{
$this->hydrator->addStrategy('entities', new HydratorStrategy());
$entityA = new HydratorStrategyEntityA();
$entityA->addEntity(new HydratorStrategyEntityB(111, 'AAA'));
$entityA->addEntity(new HydratorStrategyEntityB(222, 'BBB'));
$this->hydrator->addStrategy('entities', new TestAsset\HydratorStrategy());

$entityA = new TestAsset\HydratorStrategyEntityA();
$entityA->addEntity(new TestAsset\HydratorStrategyEntityB(111, 'AAA'));
$entityA->addEntity(new TestAsset\HydratorStrategyEntityB(222, 'BBB'));

$attributes = $this->hydrator->extract($entityA);

$this->assertContains(111, $attributes['entities']);
$this->assertContains(222, $attributes['entities']);
}

public function testHydratingObjects()
{
$this->hydrator->addStrategy('entities', new HydratorStrategy());
$entityA = new HydratorStrategyEntityA();
$entityA->addEntity(new HydratorStrategyEntityB(111, 'AAA'));
$entityA->addEntity(new HydratorStrategyEntityB(222, 'BBB'));
$this->hydrator->addStrategy('entities', new TestAsset\HydratorStrategy());

$entityA = new TestAsset\HydratorStrategyEntityA();
$entityA->addEntity(new TestAsset\HydratorStrategyEntityB(111, 'AAA'));
$entityA->addEntity(new TestAsset\HydratorStrategyEntityB(222, 'BBB'));

$attributes = $this->hydrator->extract($entityA);
$attributes['entities'][] = 333;

$this->hydrator->hydrate($attributes, $entityA);
$entities = $entityA->getEntities();

$this->assertCount(3, $entities);
}
}

0 comments on commit 7bdd8b8

Please sign in to comment.