Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Form] Swap new ChoiceView constructor arguments to ease migrating from the deprecated one #14887

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 10, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 2 additions & 18 deletions UPGRADE-2.7.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,24 +176,8 @@ Form
* `Symfony\Component\Form\Extension\Core\ChoiceList\View\ChoiceView` was
deprecated and will be removed in Symfony 3.0. You should use
`Symfony\Component\Form\ChoiceList\View\ChoiceView` instead.

Note that the order of the arguments passed to the constructor was inverted.

Before:

```php
use Symfony\Component\Form\Extension\Core\ChoiceList\View\ChoiceView;

$view = new ChoiceView($data, 'value', 'Label');
```

After:

```php
use Symfony\Component\Form\ChoiceList\View\ChoiceView;

$view = new ChoiceView('Label', 'value', $data);
```
The constructor arguments of the new class are in the same order than in the
deprecated one (this was not true in 2.7.0 but has been fixed in 2.7.1).

* `Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceList` was
deprecated and will be removed in Symfony 3.0. You should use
Expand Down
26 changes: 13 additions & 13 deletions src/Symfony/Bridge/Doctrine/Tests/Form/Type/EntityTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public function testSetDataToUninitializedEntityWithNonRequired()
'choice_label' => 'name',
));

$this->assertEquals(array(1 => new ChoiceView('Foo', '1', $entity1), 2 => new ChoiceView('Bar', '2', $entity2)), $field->createView()->vars['choices']);
$this->assertEquals(array(1 => new ChoiceView($entity1, '1', 'Foo'), 2 => new ChoiceView($entity2, '2', 'Bar')), $field->createView()->vars['choices']);
}

public function testSetDataToUninitializedEntityWithNonRequiredToString()
Expand All @@ -150,7 +150,7 @@ public function testSetDataToUninitializedEntityWithNonRequiredToString()
'required' => false,
));

$this->assertEquals(array(1 => new ChoiceView('Foo', '1', $entity1), 2 => new ChoiceView('Bar', '2', $entity2)), $field->createView()->vars['choices']);
$this->assertEquals(array(1 => new ChoiceView($entity1, '1', 'Foo'), 2 => new ChoiceView($entity2, '2', 'Bar')), $field->createView()->vars['choices']);
}

public function testSetDataToUninitializedEntityWithNonRequiredQueryBuilder()
Expand All @@ -169,7 +169,7 @@ public function testSetDataToUninitializedEntityWithNonRequiredQueryBuilder()
'query_builder' => $qb,
));

$this->assertEquals(array(1 => new ChoiceView('Foo', '1', $entity1), 2 => new ChoiceView('Bar', '2', $entity2)), $field->createView()->vars['choices']);
$this->assertEquals(array(1 => new ChoiceView($entity1, '1', 'Foo'), 2 => new ChoiceView($entity2, '2', 'Bar')), $field->createView()->vars['choices']);
}

/**
Expand Down Expand Up @@ -513,7 +513,7 @@ public function testOverrideChoices()

$field->submit('2');

$this->assertEquals(array(1 => new ChoiceView('Foo', '1', $entity1), 2 => new ChoiceView('Bar', '2', $entity2)), $field->createView()->vars['choices']);
$this->assertEquals(array(1 => new ChoiceView($entity1, '1', 'Foo'), 2 => new ChoiceView($entity2, '2', 'Bar')), $field->createView()->vars['choices']);
$this->assertTrue($field->isSynchronized());
$this->assertSame($entity2, $field->getData());
$this->assertSame('2', $field->getViewData());
Expand Down Expand Up @@ -541,13 +541,13 @@ public function testGroupByChoices()
$this->assertSame('2', $field->getViewData());
$this->assertEquals(array(
'Group1' => new ChoiceGroupView('Group1', array(
1 => new ChoiceView('Foo', '1', $item1),
2 => new ChoiceView('Bar', '2', $item2),
1 => new ChoiceView($item1, '1', 'Foo'),
2 => new ChoiceView($item2, '2', 'Bar'),
)),
'Group2' => new ChoiceGroupView('Group2', array(
3 => new ChoiceView('Baz', '3', $item3),
3 => new ChoiceView($item3, '3', 'Baz'),
)),
4 => new ChoiceView('Boo!', '4', $item4),
4 => new ChoiceView($item4, '4', 'Boo!'),
), $field->createView()->vars['choices']);
}

Expand All @@ -566,8 +566,8 @@ public function testPreferredChoices()
'choice_label' => 'name',
));

$this->assertEquals(array(3 => new ChoiceView('Baz', '3', $entity3), 2 => new ChoiceView('Bar', '2', $entity2)), $field->createView()->vars['preferred_choices']);
$this->assertEquals(array(1 => new ChoiceView('Foo', '1', $entity1)), $field->createView()->vars['choices']);
$this->assertEquals(array(3 => new ChoiceView($entity3, '3', 'Baz'), 2 => new ChoiceView($entity2, '2', 'Bar')), $field->createView()->vars['preferred_choices']);
$this->assertEquals(array(1 => new ChoiceView($entity1, '1', 'Foo')), $field->createView()->vars['choices']);
}

public function testOverrideChoicesWithPreferredChoices()
Expand All @@ -586,8 +586,8 @@ public function testOverrideChoicesWithPreferredChoices()
'choice_label' => 'name',
));

$this->assertEquals(array(3 => new ChoiceView('Baz', '3', $entity3)), $field->createView()->vars['preferred_choices']);
$this->assertEquals(array(2 => new ChoiceView('Bar', '2', $entity2)), $field->createView()->vars['choices']);
$this->assertEquals(array(3 => new ChoiceView($entity3, '3', 'Baz')), $field->createView()->vars['preferred_choices']);
$this->assertEquals(array(2 => new ChoiceView($entity2, '2', 'Bar')), $field->createView()->vars['choices']);
}

public function testDisallowChoicesThatAreNotIncludedChoicesSingleIdentifier()
Expand Down Expand Up @@ -883,7 +883,7 @@ public function testPropertyOption()
'property' => 'name',
));

$this->assertEquals(array(1 => new ChoiceView('Foo', '1', $entity1), 2 => new ChoiceView('Bar', '2', $entity2)), $field->createView()->vars['choices']);
$this->assertEquals(array(1 => new ChoiceView($entity1, '1', 'Foo'), 2 => new ChoiceView($entity2, '2', 'Bar')), $field->createView()->vars['choices']);
}

protected function createRegistryMock($name, $em)
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Bridge/Doctrine/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"symfony/phpunit-bridge": "~2.7",
"symfony/stopwatch": "~2.2",
"symfony/dependency-injection": "~2.2",
"symfony/form": "~2.7",
"symfony/form": "~2.7,>=2.7.1",
"symfony/http-kernel": "~2.2",
"symfony/property-access": "~2.3",
"symfony/security": "~2.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public function isSelectedChoiceProvider()
*/
public function testIsChoiceSelected($expected, $choice, $value)
{
$choice = new ChoiceView($choice.' label', $choice, $choice);
$choice = new ChoiceView($choice, $choice, $choice.' label');

$this->assertSame($expected, $this->extension->isSelectedChoice($choice, $value));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Bridge/Twig/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"symfony/phpunit-bridge": "~2.7",
"symfony/asset": "~2.7",
"symfony/finder": "~2.3",
"symfony/form": "~2.7",
"symfony/form": "~2.7,>=2.7.1",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is ^2.7.1

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be the first use of the caret operator isn't it?
We'd rather stick with what we do elsewhere here, and open an other PR for moving everything to the caret operator (and discuss there if it's too early or not)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's keep it like it is now.

"symfony/http-kernel": "~2.3",
"symfony/intl": "~2.3",
"symfony/routing": "~2.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,
if ($list instanceof LegacyChoiceListInterface && empty($preferredChoices)
&& null === $label && null === $index && null === $groupBy && null === $attr) {
$mapToNonLegacyChoiceView = function (LegacyChoiceView $choiceView) {
return new ChoiceView($choiceView->label, $choiceView->value, $choiceView->data);
return new ChoiceView($choiceView->data, $choiceView->value, $choiceView->label);
};

return new ChoiceListView(
Expand Down Expand Up @@ -245,10 +245,10 @@ private static function addChoiceView($choice, $key, $label, $values, &$index, $
$nextIndex = is_int($index) ? $index++ : call_user_func($index, $choice, $key, $value);

$view = new ChoiceView(
$choice,
$value,
// If the labels are null, use the choice key by default
null === $label ? (string) $key : (string) call_user_func($label, $choice, $key, $value),
$value,
$choice,
// The attributes may be a callable or a mapping from choice indices
// to nested arrays
is_callable($attr) ? call_user_func($attr, $choice, $key, $value) : (isset($attr[$key]) ? $attr[$key] : array())
Expand Down
6 changes: 3 additions & 3 deletions src/Symfony/Component/Form/ChoiceList/View/ChoiceView.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ class ChoiceView extends LegacyChoiceView
/**
* Creates a new choice view.
*
* @param string $label The label displayed to humans
* @param string $value The view representation of the choice
* @param mixed $data The original choice
* @param string $value The view representation of the choice
* @param string $label The label displayed to humans
* @param array $attr Additional attributes for the HTML tag
*/
public function __construct($label, $value, $data, array $attr = array())
public function __construct($data, $value, $label, array $attr = array())
{
parent::__construct($data, $value, $label);

Expand Down
5 changes: 2 additions & 3 deletions src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\ChoiceList\Factory\PropertyAccessDecorator;
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
use Symfony\Component\Form\ChoiceList\View\ChoiceGroupView;
use Symfony\Component\Form\ChoiceList\ChoiceListInterface;
use Symfony\Component\Form\ChoiceList\Factory\DefaultChoiceListFactory;
Expand Down Expand Up @@ -71,7 +70,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
// Check if the choices already contain the empty value
// Only add the placeholder option if this is not the case
if (null !== $options['placeholder'] && 0 === count($options['choice_list']->getChoicesForValues(array('')))) {
$placeholderView = new ChoiceView($options['placeholder'], '', null);
$placeholderView = new ChoiceView(null, '', $options['placeholder']);

// "placeholder" is a reserved name
$this->addSubForm($builder, 'placeholder', $placeholderView, $options);
Expand Down Expand Up @@ -436,7 +435,7 @@ private function createChoiceListView(ChoiceListInterface $choiceList, array $op
// information from the "choices" option for creating groups
if (!$options['group_by'] && $options['choices']) {
$options['group_by'] = !$options['choices_as_values']
? ChoiceType::flipRecursive($options['choices'])
? self::flipRecursive($options['choices'])
: $options['choices'];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,10 +320,10 @@ public function testCreateViewFlat()

$this->assertEquals(new ChoiceListView(
array(
0 => new ChoiceView('A', '0', $this->obj1),
1 => new ChoiceView('B', '1', $this->obj2),
2 => new ChoiceView('C', '2', $this->obj3),
3 => new ChoiceView('D', '3', $this->obj4),
0 => new ChoiceView($this->obj1, '0', 'A'),
1 => new ChoiceView($this->obj2, '1', 'B'),
2 => new ChoiceView($this->obj3, '2', 'C'),
3 => new ChoiceView($this->obj4, '3', 'D'),
), array()
), $view);
}
Expand All @@ -347,10 +347,10 @@ public function testCreateViewFlatPreferredChoicesEmptyArray()

$this->assertEquals(new ChoiceListView(
array(
0 => new ChoiceView('A', '0', $this->obj1),
1 => new ChoiceView('B', '1', $this->obj2),
2 => new ChoiceView('C', '2', $this->obj3),
3 => new ChoiceView('D', '3', $this->obj4),
0 => new ChoiceView($this->obj1, '0', 'A'),
1 => new ChoiceView($this->obj2, '1', 'B'),
2 => new ChoiceView($this->obj3, '2', 'C'),
3 => new ChoiceView($this->obj4, '3', 'D'),
), array()
), $view);
}
Expand Down Expand Up @@ -751,8 +751,8 @@ public function testCreateViewForLegacyChoiceList()

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

$this->assertEquals(array(new ChoiceView('Other', 'y', 'y')), $view->choices);
$this->assertEquals(array(new ChoiceView('Preferred', 'x', 'x')), $view->preferredChoices);
$this->assertEquals(array(new ChoiceView('y', 'y', 'Other')), $view->choices);
$this->assertEquals(array(new ChoiceView('x', 'x', 'Preferred')), $view->preferredChoices);
}

private function assertScalarListWithGeneratedValues(ChoiceListInterface $list)
Expand Down Expand Up @@ -827,11 +827,11 @@ private function assertFlatView($view)
{
$this->assertEquals(new ChoiceListView(
array(
0 => new ChoiceView('A', '0', $this->obj1),
3 => new ChoiceView('D', '3', $this->obj4),
0 => new ChoiceView($this->obj1, '0', 'A'),
3 => new ChoiceView($this->obj4, '3', 'D'),
), array(
1 => new ChoiceView('B', '1', $this->obj2),
2 => new ChoiceView('C', '2', $this->obj3),
1 => new ChoiceView($this->obj2, '1', 'B'),
2 => new ChoiceView($this->obj3, '2', 'C'),
)
), $view);
}
Expand All @@ -840,11 +840,11 @@ private function assertFlatViewWithCustomIndices($view)
{
$this->assertEquals(new ChoiceListView(
array(
'w' => new ChoiceView('A', '0', $this->obj1),
'z' => new ChoiceView('D', '3', $this->obj4),
'w' => new ChoiceView($this->obj1, '0', 'A'),
'z' => new ChoiceView($this->obj4, '3', 'D'),
), array(
'x' => new ChoiceView('B', '1', $this->obj2),
'y' => new ChoiceView('C', '2', $this->obj3),
'x' => new ChoiceView($this->obj2, '1', 'B'),
'y' => new ChoiceView($this->obj3, '2', 'C'),
)
), $view);
}
Expand All @@ -853,19 +853,19 @@ private function assertFlatViewWithAttr($view)
{
$this->assertEquals(new ChoiceListView(
array(
0 => new ChoiceView('A', '0', $this->obj1),
3 => new ChoiceView('D', '3', $this->obj4),
0 => new ChoiceView($this->obj1, '0', 'A'),
3 => new ChoiceView($this->obj4, '3', 'D'),
), array(
1 => new ChoiceView(
'B',
'1',
$this->obj2,
'1',
'B',
array('attr1' => 'value1')
),
2 => new ChoiceView(
'C',
'2',
$this->obj3,
'2',
'C',
array('attr2' => 'value2')
),
)
Expand All @@ -878,20 +878,20 @@ private function assertGroupedView($view)
array(
'Group 1' => new ChoiceGroupView(
'Group 1',
array(0 => new ChoiceView('A', '0', $this->obj1))
array(0 => new ChoiceView($this->obj1, '0', 'A'))
),
'Group 2' => new ChoiceGroupView(
'Group 2',
array(3 => new ChoiceView('D', '3', $this->obj4))
array(3 => new ChoiceView($this->obj4, '3', 'D'))
),
), array(
'Group 1' => new ChoiceGroupView(
'Group 1',
array(1 => new ChoiceView('B', '1', $this->obj2))
array(1 => new ChoiceView($this->obj2, '1', 'B'))
),
'Group 2' => new ChoiceGroupView(
'Group 2',
array(2 => new ChoiceView('C', '2', $this->obj3))
array(2 => new ChoiceView($this->obj3, '2', 'C'))
),
)
), $view);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1556,10 +1556,10 @@ public function testPassChoicesToView()
$view = $form->createView();

$this->assertEquals(array(
new ChoiceView('A', 'a', 'a'),
new ChoiceView('B', 'b', 'b'),
new ChoiceView('C', 'c', 'c'),
new ChoiceView('D', 'd', 'd'),
new ChoiceView('a', 'a', 'A'),
new ChoiceView('b', 'b', 'B'),
new ChoiceView('c', 'c', 'C'),
new ChoiceView('d', 'd', 'D'),
), $view->vars['choices']);
}

Expand All @@ -1573,12 +1573,12 @@ public function testPassPreferredChoicesToView()
$view = $form->createView();

$this->assertEquals(array(
0 => new ChoiceView('A', 'a', 'a'),
2 => new ChoiceView('C', 'c', 'c'),
0 => new ChoiceView('a', 'a', 'A'),
2 => new ChoiceView('c', 'c', 'C'),
), $view->vars['choices']);
$this->assertEquals(array(
1 => new ChoiceView('B', 'b', 'b'),
3 => new ChoiceView('D', 'd', 'd'),
1 => new ChoiceView('b', 'b', 'B'),
3 => new ChoiceView('d', 'd', 'D'),
), $view->vars['preferred_choices']);
}

Expand All @@ -1592,19 +1592,19 @@ public function testPassHierarchicalChoicesToView()

$this->assertEquals(array(
'Symfony' => new ChoiceGroupView('Symfony', array(
0 => new ChoiceView('Bernhard', 'a', 'a'),
2 => new ChoiceView('Kris', 'c', 'c'),
0 => new ChoiceView('a', 'a', 'Bernhard'),
2 => new ChoiceView('c', 'c', 'Kris'),
)),
'Doctrine' => new ChoiceGroupView('Doctrine', array(
4 => new ChoiceView('Roman', 'e', 'e'),
4 => new ChoiceView('e', 'e', 'Roman'),
)),
), $view->vars['choices']);
$this->assertEquals(array(
'Symfony' => new ChoiceGroupView('Symfony', array(
1 => new ChoiceView('Fabien', 'b', 'b'),
1 => new ChoiceView('b', 'b', 'Fabien'),
)),
'Doctrine' => new ChoiceGroupView('Doctrine', array(
3 => new ChoiceView('Jon', 'd', 'd'),
3 => new ChoiceView('d', 'd', 'Jon'),
)),
), $view->vars['preferred_choices']);
}
Expand All @@ -1624,10 +1624,10 @@ public function testPassChoiceDataToView()
$view = $form->createView();

$this->assertEquals(array(
new ChoiceView('A', 'a', $obj1),
new ChoiceView('B', 'b', $obj2),
new ChoiceView('C', 'c', $obj3),
new ChoiceView('D', 'd', $obj4),
new ChoiceView($obj1, 'a', 'A'),
new ChoiceView($obj2, 'b', 'B'),
new ChoiceView($obj3, 'c', 'C'),
new ChoiceView($obj4, 'd', 'D'),
), $view->vars['choices']);
}

Expand Down
Loading