[Form] Fixed PropertyPath handling of offsetGet() that returns a constant value

This commit is contained in:
Bernhard Schussek 2012-07-09 18:21:55 +02:00
parent 6e1462e0c0
commit 1345360e3f
3 changed files with 46 additions and 41 deletions

View File

@ -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'));

View File

@ -123,26 +123,6 @@ class PropertyPathTest extends \PHPUnit_Framework_TestCase
$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
*/
@ -328,16 +308,6 @@ class PropertyPathTest extends \PHPUnit_Framework_TestCase
$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();
@ -348,16 +318,6 @@ class PropertyPathTest extends \PHPUnit_Framework_TestCase
$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();

View File

@ -365,6 +365,8 @@ class PropertyPath implements \IteratorAggregate, PropertyPathInterface
*/
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) {
@ -373,7 +375,11 @@ class PropertyPath implements \IteratorAggregate, PropertyPathInterface
}
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);