[Form] Fixed PropertyPath to not modify Collection instances (not even their clones)

This commit is contained in:
Bernhard Schussek 2012-08-31 14:34:27 +02:00
parent c0673d77d0
commit 04fd5f1b21
2 changed files with 7 additions and 1 deletions

View File

@ -168,6 +168,7 @@ abstract class PropertyPathCollectionTest extends \PHPUnit_Framework_TestCase
$axesBefore = $this->getCollection(array(1 => 'second', 3 => 'fourth', 4 => 'fifth'));
$axesMerged = $this->getCollection(array(1 => 'first', 2 => 'second', 3 => 'third'));
$axesAfter = $this->getCollection(array(1 => 'second', 5 => 'first', 6 => 'third'));
$axesMergedCopy = is_object($axesMerged) ? clone $axesMerged : $axesMerged;
// Don't use a mock in order to test whether the collections are
// modified while iterating them
@ -178,6 +179,9 @@ abstract class PropertyPathCollectionTest extends \PHPUnit_Framework_TestCase
$path->setValue($car, $axesMerged);
$this->assertEquals($axesAfter, $car->getAxes());
// The passed collection was not modified
$this->assertEquals($axesMergedCopy, $axesMerged);
}
public function testSetValueCallsAdderAndRemoverForNestedCollections()

View File

@ -485,7 +485,9 @@ class PropertyPath implements \IteratorAggregate, PropertyPathInterface
$methods = $this->findAdderAndRemover($reflClass, $singulars);
if (null !== $methods) {
// At this point the add and remove methods have been found
$itemsToAdd = is_object($value) ? clone $value : $value;
// Use iterator_to_array() instead of clone in order to prevent side effects
// see https://github.com/symfony/symfony/issues/4670
$itemsToAdd = is_object($value) ? iterator_to_array($value) : $value;
$itemToRemove = array();
$propertyValue = $this->readProperty($objectOrArray, $property, $isIndex);
$previousValue = $propertyValue[self::VALUE];