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
This commit is contained in:
Fabien Potencier 2015-08-22 09:11:11 +02:00
commit 5bede45e29
2 changed files with 39 additions and 7 deletions

View File

@ -64,16 +64,18 @@ class DefaultChoiceListFactory implements ChoiceListFactoryInterface
// 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();

View File

@ -737,7 +737,7 @@ class DefaultChoiceListFactoryTest extends \PHPUnit_Framework_TestCase
/**
* @group legacy
*/
public function testCreateViewForLegacyChoiceList()
public function testCreateViewForFlatLegacyChoiceList()
{
// legacy ChoiceList instances provide legacy ChoiceView objects
$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);
}
/**
* @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());