Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[Form] Fixed PropertyPath handling of offsetGet() that returns a cons…
…tant value
  • Loading branch information
webmozart committed Jul 9, 2012
1 parent 6e1462e commit 1345360
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 41 deletions.
Expand Up @@ -96,6 +96,45 @@ abstract class PropertyPathCollectionTest extends \PHPUnit_Framework_TestCase
{
abstract protected function getCollection(array $array);

public function testGetValueReadsArrayAccess()
{
$object = $this->getCollection(array('firstName' => 'Bernhard'));

$path = new PropertyPath('[firstName]');

$this->assertEquals('Bernhard', $path->getValue($object));
}

/**
* @expectedException Symfony\Component\Form\Exception\InvalidPropertyException
*/
public function testGetValueThrowsExceptionIfArrayAccessExpected()
{
$path = new PropertyPath('[firstName]');

$path->getValue(new \stdClass());
}

public function testSetValueUpdatesArrayAccess()
{
$object = $this->getCollection(array());

$path = new PropertyPath('[firstName]');
$path->setValue($object, 'Bernhard');

$this->assertEquals('Bernhard', $object['firstName']);
}

/**
* @expectedException Symfony\Component\Form\Exception\InvalidPropertyException
*/
public function testSetValueThrowsExceptionIfArrayAccessExpected()
{
$path = new PropertyPath('[firstName]');

$path->setValue(new \stdClass(), 'Bernhard');
}

public function testSetValueCallsAdderAndRemoverForCollections()
{
$axesBefore = $this->getCollection(array(1 => 'second', 3 => 'fourth', 4 => 'fifth'));
Expand Down
40 changes: 0 additions & 40 deletions src/Symfony/Component/Form/Tests/Util/PropertyPathTest.php
Expand Up @@ -123,26 +123,6 @@ public function testGetValueReadsPropertyWithCustomPropertyPath()
$this->assertEquals('Bernhard', $path->getValue($object));
}

public function testGetValueReadsArrayAccess()
{
$object = new \ArrayObject();
$object['firstName'] = 'Bernhard';

$path = new PropertyPath('[firstName]');

$this->assertEquals('Bernhard', $path->getValue($object));
}

/**
* @expectedException Symfony\Component\Form\Exception\InvalidPropertyException
*/
public function testGetValueThrowsExceptionIfArrayAccessExpected()
{
$path = new PropertyPath('[firstName]');

$path->getValue(new Author());
}

/**
* @expectedException Symfony\Component\Form\Exception\PropertyAccessDeniedException
*/
Expand Down Expand Up @@ -328,16 +308,6 @@ public function testSetValueUpdatesPropertiesWithCustomPropertyPath()
$this->assertEquals('Bernhard', $object->child['index']->firstName);
}

public function testSetValueUpdatesArrayAccess()
{
$object = new \ArrayObject();

$path = new PropertyPath('[firstName]');
$path->setValue($object, 'Bernhard');

$this->assertEquals('Bernhard', $object['firstName']);
}

public function testSetValueUpdateMagicSet()
{
$object = new Magician();
Expand All @@ -348,16 +318,6 @@ public function testSetValueUpdateMagicSet()
$this->assertEquals('foobar', $object->__get('magicProperty'));
}

/**
* @expectedException Symfony\Component\Form\Exception\InvalidPropertyException
*/
public function testSetValueThrowsExceptionIfArrayAccessExpected()
{
$path = new PropertyPath('[firstName]');

$path->setValue(new Author(), 'Bernhard');
}

public function testSetValueUpdatesSetters()
{
$object = new Author();
Expand Down
8 changes: 7 additions & 1 deletion src/Symfony/Component/Form/Util/PropertyPath.php
Expand Up @@ -365,6 +365,8 @@ protected function &readPropertyAt(&$objectOrArray, $index)
*/
protected function &readProperty(&$objectOrArray, $property, $isIndex)
{
// The local variable (instead of immediate returns) is necessary
// because we want to return by reference!
$result = null;

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

if (isset($objectOrArray[$property])) {
$result =& $objectOrArray[$property];
if (is_array($objectOrArray)) {
$result =& $objectOrArray[$property];
} else {
$result = $objectOrArray[$property];
}
}
} elseif (is_object($objectOrArray)) {
$camelProp = $this->camelize($property);
Expand Down

0 comments on commit 1345360

Please sign in to comment.