8000 [Form] Fixed PropertyPath handling of offsetGet() that returns a cons… · symfony/symfony@1345360 · GitHub
[go: up one dir, main page]

Skip to content 8000

Commit 1345360

Browse files
committed
[Form] Fixed PropertyPath handling of offsetGet() that returns a constant value
1 parent 6e1462e commit 1345360

File tree

3 files changed

+46
-41
lines changed

3 files changed

+46
-41
lines changed

src/Symfony/Component/Form/Tests/Util/PropertyPathCollectionTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,45 @@ abstract class PropertyPathCollectionTest extends \PHPUnit_Framework_TestCase
9696
{
9797
abstract protected function getCollection(array $array);
9898

99+
public function testGetValueReadsArrayAccess()
100+
{
101+
$object = $this->getCollection(array('firstName' => 'Bernhard'));
102+
103+
$path = new PropertyPath('[firstName]');
104+
105+
$this->assertEquals('Bernhard', $path->getValue($object));
106+
}
107+
108+
/**
109+
* @expectedException Symfony\Component\Form\Exception\InvalidPropertyException
110+
*/
111+
public function testGetValueThrowsExceptionIfArrayAccessExpected()
112+
{
113+
$path = new PropertyPath('[firstName]');
114+
115+
$path->getValue(new \stdClass());
116+
}
117+
118+
public function testSetValueUpdatesArrayAccess()
119+
{
120+
$object = $this->getCollection(array());
121+
122+
$path = new PropertyPath('[firstName]');
123+
$path->setValue($object, 'Bernhard');
124+
125+
$this->assertEquals('Bernhard', $object['firstName']);
126+
}
127+
128+
/**
129+
* @expectedException Symfony\Component\Form\Exception\InvalidPropertyException
130+
*/
131+
public function testSetValueThrowsExceptionIfArrayAccessExpected()
132+
{
133+
$path = new PropertyPath('[firstName]');
134+
135+
$path->setValue(new \stdClass(), 'Bernhard');
136+
}
137+
99138
public function testSetValueCallsAdderAndRemoverForCollections()
100139
{
101140
$axesBefore = $this->getCollection(array(1 => 'second', 3 => 'fourth', 4 => 'fifth'));

src/Symfony/Component/Form/Tests/Util/PropertyPathTest.php

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -123,26 +123,6 @@ public function testGetValueReadsPropertyWithCustomPropertyPath()
123123
$this->assertEquals('Bernhard', $path->getValue($object));
124124
}
125125

126-
public function testGetValueReadsArrayAccess()
127-
{
128-
$object = new \ArrayObject();
129-
$object['firstName'] = 'Bernhard';
130-
131-
$path = new PropertyPath('[firstName]');
132-
133-
$this->assertEquals('Bernhard', $path->getValue($object));
134-
}
135-
136-
/**
137-
* @expectedException Symfony\Component\Form\Exception\InvalidPropertyException
138-
*/
139-
public function testGetValueThrowsExceptionIfArrayAccessExpected()
140-
{
141-
$path = new PropertyPath('[firstName]');
142-
143-
$path->getValue(new Author());
144-
}
145-
146126
/**
147127
* @expectedException Symfony\Component\Form\Exception\PropertyAccessDeniedException
148128
*/
@@ -328,16 +308,6 @@ public function testSetValueUpdatesPropertiesWithCustomPropertyPath()
328308
$this->assertEquals('Bernhard', $object->child['index']->firstName);
329309
}
330310

331-
public function testSetValueUpdatesArrayAccess()
332-
{
333-
$object = new \ArrayObject();
334-
335-
$path = new PropertyPath('[firstName]');
336-
$path->setValue($object, 'Bernhard');
337-
338-
$this->assertEquals('Bernhard', $object['firstName']);
339-
}
340-
341311
public function testSetValueUpdateMagicSet()
342312
{
343313
$object = new Magician();
@@ -348,16 +318,6 @@ public function testSetValueUpdateMagicSet()
348318
$this->assertEquals('foobar', $object->__get('magicProperty'));
349319
}
350320

351-
/**
352-
* @expectedException Symfony\Component\Form\Exception\InvalidPropertyException
353-
*/
354-
public function testSetValueThrowsExceptionIfArrayAccessExpected()
355-
{
356-
$path = new PropertyPath('[firstName]');
357-
358-
$path->setValue(new Author(), 'Bernhard');
359-
}
360-
361321
public function testSetValueUpdatesSetters()
362322
{
363323
$object = new Author();

src/Symfony/Component/Form/Util/PropertyPath.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,8 @@ protected function &readPropertyAt(&$objectOrArray, $index)
365365
*/
366366
protected function &readProperty(&$objectOrArray, $property, $isIndex)
367367
{
368+
// The local variable (instead of immediate returns) is necessary
369+
// because we want to return by reference!
368370
$result = null;
369371

370372
if ($isIndex) {
@@ -373,7 +375,11 @@ protected function &readProperty(&$objectOrArray, $property, $isIndex)
373375
}
374376

375377
if (isset($objectOrArray[$property])) {
376-
$result =& $objectOrArray[$property];
378+
if (is_array($objectOrArray)) {
379+
$result =& $objectOrArray[$property];
380+
} else {
381+
$result = $objectOrArray[$property];
382+
}
377383
}
378384
} elseif (is_object($objectOrArray)) {
379385
$camelProp = $this->camelize($property);

0 commit comments

Comments
 (0)
0