Skip to content

Commit d5243b5

Browse files
[Form] Fix tests and reference usage
1 parent 98bcc3e commit d5243b5

File tree

2 files changed

+32
-31
lines changed

2 files changed

+32
-31
lines changed

Extension/Core/Type/ChoiceType.php

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ public function finishView(FormView $view, FormInterface $form, array $options)
238238
*/
239239
public function configureOptions(OptionsResolver $resolver)
240240
{
241-
$choiceLabels = array();
241+
$choiceLabels = (object) array('labels' => array());
242242
$choiceListFactory = $this->choiceListFactory;
243243

244244
$emptyData = function (Options $options) {
@@ -254,9 +254,9 @@ public function configureOptions(OptionsResolver $resolver)
254254
};
255255

256256
// BC closure, to be removed in 3.0
257-
$choicesNormalizer = function (Options $options, $choices) use (&$choiceLabels) {
257+
$choicesNormalizer = function (Options $options, $choices) use ($choiceLabels) {
258258
// Unset labels from previous invocations
259-
$choiceLabels = array();
259+
$choiceLabels->labels = array();
260260

261261
// This closure is irrelevant when "choices_as_values" is set to true
262262
if ($options['choices_as_values']) {
@@ -269,22 +269,23 @@ public function configureOptions(OptionsResolver $resolver)
269269
};
270270

271271
// BC closure, to be removed in 3.0
272-
$choiceLabel = function (Options $options) use (&$choiceLabels) {
272+
$choiceLabel = function (Options $options) use ($choiceLabels) {
273273
// If the choices contain duplicate labels, the normalizer of the
274274
// "choices" option stores them in the $choiceLabels variable
275275

276276
// Trigger the normalizer
277277
$options->offsetGet('choices');
278278

279279
// Pick labels from $choiceLabels if available
280-
// Don't invoke count() to avoid creating a copy of the array (yet)
281-
if ($choiceLabels) {
280+
if ($choiceLabels->labels) {
282281
// Don't pass the labels by reference. We do want to create a
283282
// copy here so that every form has an own version of that
284-
// variable (contrary to the global reference shared by all
283+
// variable (contrary to the $choiceLabels object shared by all
285284
// forms)
286-
return function ($choice, $key) use ($choiceLabels) {
287-
return $choiceLabels[$key];
285+
$labels = $choiceLabels->labels;
286+
287+
return function ($choice, $key) use ($labels) {
288+
return $labels[$key];
288289
};
289290
}
290291

@@ -502,26 +503,26 @@ private function createChoiceListView(ChoiceListInterface $choiceList, array $op
502503
* are lost. Store them in a utility array that is used from the
503504
* "choice_label" closure by default.
504505
*
505-
* @param array $choices The choice labels indexed by choices.
506-
* Labels are replaced by generated keys.
507-
* @param array $choiceLabels The array that receives the choice labels
508-
* indexed by generated keys.
509-
* @param int|null $nextKey The next generated key.
506+
* @param array $choices The choice labels indexed by choices.
507+
* Labels are replaced by generated keys.
508+
* @param object $choiceLabels The object that receives the choice labels
509+
* indexed by generated keys.
510+
* @param int $nextKey The next generated key.
510511
*
511512
* @internal Public only to be accessible from closures on PHP 5.3. Don't
512-
* use this method, as it may be removed without notice.
513+
* use this method as it may be removed without notice and will be in 3.0.
513514
*/
514-
public static function normalizeLegacyChoices(array &$choices, array &$choiceLabels, &$nextKey = 0)
515+
public static function normalizeLegacyChoices(array &$choices, $choiceLabels, &$nextKey = 0)
515516
{
516-
foreach ($choices as $choice => &$choiceLabel) {
517+
foreach ($choices as $choice => $choiceLabel) {
517518
if (is_array($choiceLabel)) {
518-
self::normalizeLegacyChoices($choiceLabel, $choiceLabels, $nextKey);
519+
$choiceLabel = ''; // Dereference $choices[$choice]
520+
self::normalizeLegacyChoices($choices[$choice], $choiceLabels, $nextKey);
519521
continue;
520522
}
521523

522-
$choiceLabels[$nextKey] = $choiceLabel;
523-
$choices[$choice] = $nextKey;
524-
++$nextKey;
524+
$choiceLabels->labels[$nextKey] = $choiceLabel;
525+
$choices[$choice] = $nextKey++;
525526
}
526527
}
527528
}

Tests/AbstractBootstrap3HorizontalLayoutTest.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ abstract class AbstractBootstrap3HorizontalLayoutTest extends AbstractBootstrap3
1515
{
1616
public function testLabelOnForm()
1717
{
18-
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\DateType');
18+
$form = $this->factory->createNamed('name', 'date');
1919
$view = $form->createView();
2020
$this->renderWidget($view, array('label' => 'foo'));
2121
$html = $this->renderLabel($view);
@@ -30,7 +30,7 @@ public function testLabelOnForm()
3030

3131
public function testLabelDoesNotRenderFieldAttributes()
3232
{
33-
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\TextType');
33+
$form = $this->factory->createNamed('name', 'text');
3434
$html = $this->renderLabel($form->createView(), null, array(
3535
'attr' => array(
3636
'class' => 'my&class',
@@ -47,7 +47,7 @@ public function testLabelDoesNotRenderFieldAttributes()
4747

4848
public function testLabelWithCustomAttributesPassedDirectly()
4949
{
50-
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\TextType');
50+
$form = $this->factory->createNamed('name', 'text');
5151
$html = $this->renderLabel($form->createView(), null, array(
5252
'label_attr' => array(
5353
'class' => 'my&class',
@@ -64,7 +64,7 @@ public function testLabelWithCustomAttributesPassedDirectly()
6464

6565
public function testLabelWithCustomTextAndCustomAttributesPassedDirectly()
6666
{
67-
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\TextType');
67+
$form = $this->factory->createNamed('name', 'text');
6868
$html = $this->renderLabel($form->createView(), 'Custom label', array(
6969
'label_attr' => array(
7070
'class' => 'my&class',
@@ -82,7 +82,7 @@ public function testLabelWithCustomTextAndCustomAttributesPassedDirectly()
8282

8383
public function testLabelWithCustomTextAsOptionAndCustomAttributesPassedDirectly()
8484
{
85-
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\TextType', null, array(
85+
$form = $this->factory->createNamed('name', 'text', null, array(
8686
'label' => 'Custom label',
8787
));
8888
$html = $this->renderLabel($form->createView(), null, array(
@@ -102,7 +102,7 @@ public function testLabelWithCustomTextAsOptionAndCustomAttributesPassedDirectly
102102

103103
public function testStartTag()
104104
{
105-
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\FormType', null, array(
105+
$form = $this->factory->create('form', null, array(
106106
'method' => 'get',
107107
'action' => 'http://example.com/directory',
108108
));
@@ -114,7 +114,7 @@ public function testStartTag()
114114

115115
public function testStartTagWithOverriddenVars()
116116
{
117-
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\FormType', null, array(
117+
$form = $this->factory->create('form', null, array(
118118
'method' => 'put',
119119
'action' => 'http://example.com/directory',
120120
));
@@ -129,11 +129,11 @@ public function testStartTagWithOverriddenVars()
129129

130130
public function testStartTagForMultipartForm()
131131
{
132-
$form = $this->factory->createBuilder('Symfony\Component\Form\Extension\Core\Type\FormType', null, array(
132+
$form = $this->factory->createBuilder('form', null, array(
133133
'method' => 'get',
134134
'action' => 'http://example.com/directory',
135135
))
136-
->add('file', 'Symfony\Component\Form\Extension\Core\Type\FileType')
136+
->add('file', 'file')
137137
->getForm();
138138

139139
$html = $this->renderStart($form->createView());
@@ -143,7 +143,7 @@ public function testStartTagForMultipartForm()
143143

144144
public function testStartTagWithExtraAttributes()
145145
{
146-
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\FormType', null, array(
146+
$form = $this->factory->create('form', null, array(
147147
'method' => 'get',
148148
'action' => 'http://example.com/directory',
149149
));

0 commit comments

Comments
 (0)