From 7bdd8b8d6d4181d6f30ffbfe2898edf759959060 Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Wed, 1 Aug 2012 12:00:27 -0500 Subject: [PATCH] [zendframework/zf2#2072] Fix for 5.3 - 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 --- src/Hydrator/ArraySerializable.php | 10 +++-- src/Hydrator/ObjectProperty.php | 5 ++- test/HydratorStrategyTest.php | 59 ++++++++++++++---------------- 3 files changed, 37 insertions(+), 37 deletions(-) diff --git a/src/Hydrator/ArraySerializable.php b/src/Hydrator/ArraySerializable.php index 9c8650787..3b6cb18e2 100644 --- a/src/Hydrator/ArraySerializable.php +++ b/src/Hydrator/ArraySerializable.php @@ -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; } @@ -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'))) { diff --git a/src/Hydrator/ObjectProperty.php b/src/Hydrator/ObjectProperty.php index 14cf9e5f6..2416de8a5 100644 --- a/src/Hydrator/ObjectProperty.php +++ b/src/Hydrator/ObjectProperty.php @@ -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; } diff --git a/test/HydratorStrategyTest.php b/test/HydratorStrategyTest.php index 85316d43e..5a4c14dba 100644 --- a/test/HydratorStrategyTest.php +++ b/test/HydratorStrategyTest.php @@ -12,9 +12,6 @@ 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 @@ -22,15 +19,15 @@ * @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(); @@ -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); } @@ -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); } }