[Form] fixed BC-break on grouped choice lists

This commit is contained in:
origaminal 2015-08-14 00:56:05 +03:00 committed by Fabien Potencier
parent c84a403557
commit 12a7dd1714
2 changed files with 39 additions and 7 deletions

View File

@ -64,16 +64,18 @@ class DefaultChoiceListFactory implements ChoiceListFactoryInterface
// Backwards compatibility // Backwards compatibility
if ($list instanceof LegacyChoiceListAdapter && empty($preferredChoices) if ($list instanceof LegacyChoiceListAdapter && empty($preferredChoices)
&& null === $label && null === $index && null === $groupBy && null === $attr) { && null === $label && null === $index && null === $groupBy && null === $attr) {
$mapToNonLegacyChoiceView = function (LegacyChoiceView $choiceView) { $mapToNonLegacyChoiceView = function (LegacyChoiceView &$choiceView) {
return new ChoiceView($choiceView->data, $choiceView->value, $choiceView->label); $choiceView = new ChoiceView($choiceView->data, $choiceView->value, $choiceView->label);
}; };
$adaptedList = $list->getAdaptedList(); $adaptedList = $list->getAdaptedList();
return new ChoiceListView( $remainingViews = $adaptedList->getRemainingViews();
array_map($mapToNonLegacyChoiceView, $adaptedList->getRemainingViews()), $preferredViews = $adaptedList->getPreferredViews();
array_map($mapToNonLegacyChoiceView, $adaptedList->getPreferredViews()) array_walk_recursive($remainingViews, $mapToNonLegacyChoiceView);
); array_walk_recursive($preferredViews, $mapToNonLegacyChoiceView);
return new ChoiceListView($remainingViews, $preferredViews);
} }
$preferredViews = array(); $preferredViews = array();

View File

@ -737,7 +737,7 @@ class DefaultChoiceListFactoryTest extends \PHPUnit_Framework_TestCase
/** /**
* @group legacy * @group legacy
*/ */
public function testCreateViewForLegacyChoiceList() public function testCreateViewForFlatLegacyChoiceList()
{ {
// legacy ChoiceList instances provide legacy ChoiceView objects // legacy ChoiceList instances provide legacy ChoiceView objects
$preferred = array(new LegacyChoiceView('x', 'x', 'Preferred')); $preferred = array(new LegacyChoiceView('x', 'x', 'Preferred'));
@ -758,6 +758,36 @@ class DefaultChoiceListFactoryTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(array(new ChoiceView('x', 'x', 'Preferred')), $view->preferredChoices); $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) private function assertScalarListWithChoiceValues(ChoiceListInterface $list)
{ {
$this->assertSame(array('a', 'b', 'c', 'd'), $list->getValues()); $this->assertSame(array('a', 'b', 'c', 'd'), $list->getValues());