Skip to content

Commit

Permalink
[Form] Fixed PropertyPath to not modify Collection instances (not eve…
Browse files Browse the repository at this point in the history
…n their clones)
  • Loading branch information
webmozart committed Aug 31, 2012
1 parent c0673d7 commit 04fd5f1
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
Expand Up @@ -168,6 +168,7 @@ public function testSetValueCallsAdderAndRemoverForCollections()
$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
Expand All @@ -178,6 +179,9 @@ public function testSetValueCallsAdderAndRemoverForCollections()
$path->setValue($car, $axesMerged);

$this->assertEquals($axesAfter, $car->getAxes());

// The passed collection was not modified
$this->assertEquals($axesMergedCopy, $axesMerged);
}

public function testSetValueCallsAdderAndRemoverForNestedCollections()
Expand Down
4 changes: 3 additions & 1 deletion src/Symfony/Component/Form/Util/PropertyPath.php
Expand Up @@ -485,7 +485,9 @@ private function writeProperty(&$objectOrArray, $property, $singular, $isIndex,
$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];
Expand Down

0 comments on commit 04fd5f1

Please sign in to comment.