Skip to content

Commit 3190896

Browse files
committed
bug #15546 [Form] fixed BC-break on grouped choice lists (origaminal)
This PR was squashed before being merged into the 2.7 branch (closes #15546). Discussion ---------- [Form] fixed BC-break on grouped choice lists | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #15037 | License | MIT | Doc PR | Commits ------- 12a7dd1 [Form] fixed BC-break on grouped choice lists
2 parents 927593c + 66cec37 commit 3190896

File tree

2 files changed

+39
-7
lines changed

2 files changed

+39
-7
lines changed

ChoiceList/Factory/DefaultChoiceListFactory.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,18 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,
6464
// Backwards compatibility
6565
if ($list instanceof LegacyChoiceListAdapter && empty($preferredChoices)
6666
&& null === $label && null === $index && null === $groupBy && null === $attr) {
67-
$mapToNonLegacyChoiceView = function (LegacyChoiceView $choiceView) {
68-
return new ChoiceView($choiceView->data, $choiceView->value, $choiceView->label);
67+
$mapToNonLegacyChoiceView = function (LegacyChoiceView &$choiceView) {
68+
$choiceView = new ChoiceView($choiceView->data, $choiceView->value, $choiceView->label);
6969
};
7070

7171
$adaptedList = $list->getAdaptedList();
7272

73-
return new ChoiceListView(
74-
array_map($mapToNonLegacyChoiceView, $adaptedList->getRemainingViews()),
75-
array_map($mapToNonLegacyChoiceView, $adaptedList->getPreferredViews())
76-
);
73+
$remainingViews = $adaptedList->getRemainingViews();
74+
$preferredViews = $adaptedList->getPreferredViews();
75+
array_walk_recursive($remainingViews, $mapToNonLegacyChoiceView);
76+
array_walk_recursive($preferredViews, $mapToNonLegacyChoiceView);
77+
78+
return new ChoiceListView($remainingViews, $preferredViews);
7779
}
7880

7981
$preferredViews = array();

Tests/ChoiceList/Factory/DefaultChoiceListFactoryTest.php

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -737,7 +737,7 @@ function ($object, $key, $value) {
737737
/**
738738
* @group legacy
739739
*/
740-
public function testCreateViewForLegacyChoiceList()
740+
public function testCreateViewForFlatLegacyChoiceList()
741741
{
742742
// legacy ChoiceList instances provide legacy ChoiceView objects
743743
$preferred = array(new LegacyChoiceView('x', 'x', 'Preferred'));
@@ -758,6 +758,36 @@ public function testCreateViewForLegacyChoiceList()
758758
$this->assertEquals(array(new ChoiceView('x', 'x', 'Preferred')), $view->preferredChoices);
759759
}
760760

761+
/**
762+
* @group legacy
763+
*/
764+
public function testCreateViewForNestedLegacyChoiceList()
765+
{
766+
// legacy ChoiceList instances provide legacy ChoiceView objects
767+
$preferred = array('Section 1' => array(new LegacyChoiceView('x', 'x', 'Preferred')));
768+
$other = array(
769+
'Section 2' => array(new LegacyChoiceView('y', 'y', 'Other')),
770+
new LegacyChoiceView('z', 'z', 'Other one'),
771+
);
772+
773+
$list = $this->getMock('Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface');
774+
775+
$list->expects($this->once())
776+
->method('getPreferredViews')
777+
->will($this->returnValue($preferred));
778+
$list->expects($this->once())
779+
->method('getRemainingViews')
780+
->will($this->returnValue($other));
781+
782+
$view = $this->factory->createView(new LegacyChoiceListAdapter($list));
783+
784+
$this->assertEquals(array(
785+
'Section 2' => array(new ChoiceView('y', 'y', 'Other')),
786+
new ChoiceView('z', 'z', 'Other one'),
787+
), $view->choices);
788+
$this->assertEquals(array('Section 1' => array(new ChoiceView('x', 'x', 'Preferred'))), $view->preferredChoices);
789+
}
790+
761791
private function assertScalarListWithChoiceValues(ChoiceListInterface $list)
762792
{
763793
$this->assertSame(array('a', 'b', 'c', 'd'), $list->getValues());

0 commit comments

Comments
 (0)