diff --git a/src/Hydrator/ClassMethods.php b/src/Hydrator/ClassMethods.php index ad565f88d..1ce91f0f1 100644 --- a/src/Hydrator/ClassMethods.php +++ b/src/Hydrator/ClassMethods.php @@ -103,10 +103,10 @@ public function hydrate(array $data, $object) }; foreach ($data as $property => $value) { + $method = 'set' . ucfirst($property); if ($this->underscoreSeparatedKeys) { - $property = preg_replace_callback('/(_[a-z])/', $transform, $property); + $method = preg_replace_callback('/(_[a-z])/', $transform, $method); } - $method = 'set' . ucfirst($property); if (method_exists($object, $method)) { $value = $this->hydrateValue($property, $value); diff --git a/test/HydratorStrategyTest.php b/test/HydratorStrategyTest.php index 5a4c14dba..6f83c2ec2 100644 --- a/test/HydratorStrategyTest.php +++ b/test/HydratorStrategyTest.php @@ -103,4 +103,45 @@ public function testHydratingObjects() $this->assertCount(3, $entities); } + + /** + * @dataProvider underscoreHandlingDataProvider + */ + public function testWhenUsingUnderscoreSeparatedKeysHydratorStrategyIsAlwaysConsideredUnderscoreSeparatedToo($underscoreSeparatedKeys, $formFieldKey) + { + $hydrator = new ClassMethods($underscoreSeparatedKeys); + + $strategy = $this->getMock('Zend\Stdlib\Hydrator\Strategy\StrategyInterface'); + + $entity = new TestAsset\ClassMethodsUnderscore(); + $value = $entity->getFooBar(); + + $hydrator->addStrategy($formFieldKey, $strategy); + + $strategy + ->expects($this->once()) + ->method('extract') + ->with($this->identicalTo($value)) + ->will($this->returnValue($value)) + ; + + $attributes = $hydrator->extract($entity); + + $strategy + ->expects($this->once()) + ->method('hydrate') + ->with($this->identicalTo($value)) + ->will($this->returnValue($value)) + ; + + $hydrator->hydrate($attributes, $entity); + } + + public function underscoreHandlingDataProvider() + { + return array( + array(true, 'foo_bar'), + array(false, 'fooBar'), + ); + } }