Przeglądaj źródła

[Form] PropertyPath camelizes property names when setting values

Bernhard Schussek 14 lat temu
rodzic
commit
b902cb31d7

+ 1 - 1
src/Symfony/Component/Form/PropertyPath.php

@@ -353,7 +353,7 @@ class PropertyPath implements \IteratorAggregate
             $objectOrArray[$property] = $value;
         } else if (is_object($objectOrArray)) {
             $reflClass = new \ReflectionClass($objectOrArray);
-            $setter = 'set'.ucfirst($property); // TODO camelize correctly
+            $setter = 'set'.$this->camelize($property);
 
             if ($reflClass->hasMethod($setter)) {
                 if (!$reflClass->getMethod($setter)->isPublic()) {

+ 20 - 0
tests/Symfony/Tests/Component/Form/PropertyPathTest.php

@@ -97,6 +97,16 @@ class PropertyPathTest extends \PHPUnit_Framework_TestCase
         $this->assertEquals('Schussek', $path->getValue($object));
     }
 
+    public function testGetValueCamelizesGetterNames()
+    {
+        $path = new PropertyPath('last_name');
+
+        $object = new Author();
+        $object->setLastName('Schussek');
+
+        $this->assertEquals('Schussek', $path->getValue($object));
+    }
+
     public function testGetValueThrowsExceptionIfGetterIsNotPublic()
     {
         $path = new PropertyPath('privateGetter');
@@ -205,6 +215,16 @@ class PropertyPathTest extends \PHPUnit_Framework_TestCase
         $this->assertEquals('Schussek', $object->getLastName());
     }
 
+    public function testSetValueCamelizesSetterNames()
+    {
+        $object = new Author();
+
+        $path = new PropertyPath('last_name');
+        $path->setValue($object, 'Schussek');
+
+        $this->assertEquals('Schussek', $object->getLastName());
+    }
+
     public function testSetValueThrowsExceptionIfGetterIsNotPublic()
     {
         $path = new PropertyPath('privateSetter');