Skip to content

Commit

Permalink
[Form] fixed BC-break on grouped choice lists
Browse files Browse the repository at this point in the history
  • Loading branch information
origaminal authored and fabpot committed Aug 22, 2015
1 parent c84a403 commit 12a7dd1
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
Expand Up @@ -64,16 +64,18 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,
// Backwards compatibility
if ($list instanceof LegacyChoiceListAdapter && empty($preferredChoices)
&& null === $label && null === $index && null === $groupBy && null === $attr) {
$mapToNonLegacyChoiceView = function (LegacyChoiceView $choiceView) {
return new ChoiceView($choiceView->data, $choiceView->value, $choiceView->label);
$mapToNonLegacyChoiceView = function (LegacyChoiceView &$choiceView) {
$choiceView = new ChoiceView($choiceView->data, $choiceView->value, $choiceView->label);
};

$adaptedList = $list->getAdaptedList();

return new ChoiceListView(
array_map($mapToNonLegacyChoiceView, $adaptedList->getRemainingViews()),
array_map($mapToNonLegacyChoiceView, $adaptedList->getPreferredViews())
);
$remainingViews = $adaptedList->getRemainingViews();
$preferredViews = $adaptedList->getPreferredViews();
array_walk_recursive($remainingViews, $mapToNonLegacyChoiceView);
array_walk_recursive($preferredViews, $mapToNonLegacyChoiceView);

return new ChoiceListView($remainingViews, $preferredViews);
}

$preferredViews = array();
Expand Down
Expand Up @@ -737,7 +737,7 @@ function ($object, $key, $value) {
/**
* @group legacy
*/
public function testCreateViewForLegacyChoiceList()
public function testCreateViewForFlatLegacyChoiceList()
{
// legacy ChoiceList instances provide legacy ChoiceView objects
$preferred = array(new LegacyChoiceView('x', 'x', 'Preferred'));
Expand All @@ -758,6 +758,36 @@ public function testCreateViewForLegacyChoiceList()
$this->assertEquals(array(new ChoiceView('x', 'x', 'Preferred')), $view->preferredChoices);
}

/**
* @group legacy
*/
public function testCreateViewForNestedLegacyChoiceList()
{
// legacy ChoiceList instances provide legacy ChoiceView objects
$preferred = array('Section 1' => array(new LegacyChoiceView('x', 'x', 'Preferred')));
$other = array(
'Section 2' => array(new LegacyChoiceView('y', 'y', 'Other')),
new LegacyChoiceView('z', 'z', 'Other one'),
);

$list = $this->getMock('Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface');

$list->expects($this->once())
->method('getPreferredViews')
->will($this->returnValue($preferred));
$list->expects($this->once())
->method('getRemainingViews')
->will($this->returnValue($other));

$view = $this->factory->createView(new LegacyChoiceListAdapter($list));

$this->assertEquals(array(
'Section 2' => array(new ChoiceView('y', 'y', 'Other')),
new ChoiceView('z', 'z', 'Other one'),
), $view->choices);
$this->assertEquals(array('Section 1' => array(new ChoiceView('x', 'x', 'Preferred'))), $view->preferredChoices);
}

private function assertScalarListWithChoiceValues(ChoiceListInterface $list)
{
$this->assertSame(array('a', 'b', 'c', 'd'), $list->getValues());
Expand Down

0 comments on commit 12a7dd1

Please sign in to comment.