[Form] Fix tests and reference usage

This commit is contained in:
Nicolas Grekas 2015-11-28 17:51:37 +01:00
parent e3f4278d59
commit 6001644ffe
2 changed files with 32 additions and 31 deletions

View File

@ -238,7 +238,7 @@ class ChoiceType extends AbstractType
*/ */
public function configureOptions(OptionsResolver $resolver) public function configureOptions(OptionsResolver $resolver)
{ {
$choiceLabels = array(); $choiceLabels = (object) array('labels' => array());
$choiceListFactory = $this->choiceListFactory; $choiceListFactory = $this->choiceListFactory;
$emptyData = function (Options $options) { $emptyData = function (Options $options) {
@ -254,9 +254,9 @@ class ChoiceType extends AbstractType
}; };
// BC closure, to be removed in 3.0 // BC closure, to be removed in 3.0
$choicesNormalizer = function (Options $options, $choices) use (&$choiceLabels) { $choicesNormalizer = function (Options $options, $choices) use ($choiceLabels) {
// Unset labels from previous invocations // Unset labels from previous invocations
$choiceLabels = array(); $choiceLabels->labels = array();
// This closure is irrelevant when "choices_as_values" is set to true // This closure is irrelevant when "choices_as_values" is set to true
if ($options['choices_as_values']) { if ($options['choices_as_values']) {
@ -269,7 +269,7 @@ class ChoiceType extends AbstractType
}; };
// BC closure, to be removed in 3.0 // BC closure, to be removed in 3.0
$choiceLabel = function (Options $options) use (&$choiceLabels) { $choiceLabel = function (Options $options) use ($choiceLabels) {
// If the choices contain duplicate labels, the normalizer of the // If the choices contain duplicate labels, the normalizer of the
// "choices" option stores them in the $choiceLabels variable // "choices" option stores them in the $choiceLabels variable
@ -277,14 +277,15 @@ class ChoiceType extends AbstractType
$options->offsetGet('choices'); $options->offsetGet('choices');
// Pick labels from $choiceLabels if available // Pick labels from $choiceLabels if available
// Don't invoke count() to avoid creating a copy of the array (yet) if ($choiceLabels->labels) {
if ($choiceLabels) {
// Don't pass the labels by reference. We do want to create a // Don't pass the labels by reference. We do want to create a
// copy here so that every form has an own version of that // copy here so that every form has an own version of that
// variable (contrary to the global reference shared by all // variable (contrary to the $choiceLabels object shared by all
// forms) // forms)
return function ($choice, $key) use ($choiceLabels) { $labels = $choiceLabels->labels;
return $choiceLabels[$key];
return function ($choice, $key) use ($labels) {
return $labels[$key];
}; };
} }
@ -502,26 +503,26 @@ class ChoiceType extends AbstractType
* are lost. Store them in a utility array that is used from the * are lost. Store them in a utility array that is used from the
* "choice_label" closure by default. * "choice_label" closure by default.
* *
* @param array $choices The choice labels indexed by choices. * @param array $choices The choice labels indexed by choices.
* Labels are replaced by generated keys. * Labels are replaced by generated keys.
* @param array $choiceLabels The array that receives the choice labels * @param object $choiceLabels The object that receives the choice labels
* indexed by generated keys. * indexed by generated keys.
* @param int|null $nextKey The next generated key. * @param int $nextKey The next generated key.
* *
* @internal Public only to be accessible from closures on PHP 5.3. Don't * @internal Public only to be accessible from closures on PHP 5.3. Don't
* use this method, as it may be removed without notice. * use this method as it may be removed without notice and will be in 3.0.
*/ */
public static function normalizeLegacyChoices(array &$choices, array &$choiceLabels, &$nextKey = 0) public static function normalizeLegacyChoices(array &$choices, $choiceLabels, &$nextKey = 0)
{ {
foreach ($choices as $choice => &$choiceLabel) { foreach ($choices as $choice => $choiceLabel) {
if (is_array($choiceLabel)) { if (is_array($choiceLabel)) {
self::normalizeLegacyChoices($choiceLabel, $choiceLabels, $nextKey); $choiceLabel = ''; // Dereference $choices[$choice]
self::normalizeLegacyChoices($choices[$choice], $choiceLabels, $nextKey);
continue; continue;
} }
$choiceLabels[$nextKey] = $choiceLabel; $choiceLabels->labels[$nextKey] = $choiceLabel;
$choices[$choice] = $nextKey; $choices[$choice] = $nextKey++;
++$nextKey;
} }
} }
} }

View File

@ -15,7 +15,7 @@ abstract class AbstractBootstrap3HorizontalLayoutTest extends AbstractBootstrap3
{ {
public function testLabelOnForm() public function testLabelOnForm()
{ {
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\DateType'); $form = $this->factory->createNamed('name', 'date');
$view = $form->createView(); $view = $form->createView();
$this->renderWidget($view, array('label' => 'foo')); $this->renderWidget($view, array('label' => 'foo'));
$html = $this->renderLabel($view); $html = $this->renderLabel($view);
@ -30,7 +30,7 @@ abstract class AbstractBootstrap3HorizontalLayoutTest extends AbstractBootstrap3
public function testLabelDoesNotRenderFieldAttributes() public function testLabelDoesNotRenderFieldAttributes()
{ {
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\TextType'); $form = $this->factory->createNamed('name', 'text');
$html = $this->renderLabel($form->createView(), null, array( $html = $this->renderLabel($form->createView(), null, array(
'attr' => array( 'attr' => array(
'class' => 'my&class', 'class' => 'my&class',
@ -47,7 +47,7 @@ abstract class AbstractBootstrap3HorizontalLayoutTest extends AbstractBootstrap3
public function testLabelWithCustomAttributesPassedDirectly() public function testLabelWithCustomAttributesPassedDirectly()
{ {
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\TextType'); $form = $this->factory->createNamed('name', 'text');
$html = $this->renderLabel($form->createView(), null, array( $html = $this->renderLabel($form->createView(), null, array(
'label_attr' => array( 'label_attr' => array(
'class' => 'my&class', 'class' => 'my&class',
@ -64,7 +64,7 @@ abstract class AbstractBootstrap3HorizontalLayoutTest extends AbstractBootstrap3
public function testLabelWithCustomTextAndCustomAttributesPassedDirectly() public function testLabelWithCustomTextAndCustomAttributesPassedDirectly()
{ {
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\TextType'); $form = $this->factory->createNamed('name', 'text');
$html = $this->renderLabel($form->createView(), 'Custom label', array( $html = $this->renderLabel($form->createView(), 'Custom label', array(
'label_attr' => array( 'label_attr' => array(
'class' => 'my&class', 'class' => 'my&class',
@ -82,7 +82,7 @@ abstract class AbstractBootstrap3HorizontalLayoutTest extends AbstractBootstrap3
public function testLabelWithCustomTextAsOptionAndCustomAttributesPassedDirectly() public function testLabelWithCustomTextAsOptionAndCustomAttributesPassedDirectly()
{ {
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\TextType', null, array( $form = $this->factory->createNamed('name', 'text', null, array(
'label' => 'Custom label', 'label' => 'Custom label',
)); ));
$html = $this->renderLabel($form->createView(), null, array( $html = $this->renderLabel($form->createView(), null, array(
@ -102,7 +102,7 @@ abstract class AbstractBootstrap3HorizontalLayoutTest extends AbstractBootstrap3
public function testStartTag() public function testStartTag()
{ {
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\FormType', null, array( $form = $this->factory->create('form', null, array(
'method' => 'get', 'method' => 'get',
'action' => 'http://example.com/directory', 'action' => 'http://example.com/directory',
)); ));
@ -114,7 +114,7 @@ abstract class AbstractBootstrap3HorizontalLayoutTest extends AbstractBootstrap3
public function testStartTagWithOverriddenVars() public function testStartTagWithOverriddenVars()
{ {
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\FormType', null, array( $form = $this->factory->create('form', null, array(
'method' => 'put', 'method' => 'put',
'action' => 'http://example.com/directory', 'action' => 'http://example.com/directory',
)); ));
@ -129,11 +129,11 @@ abstract class AbstractBootstrap3HorizontalLayoutTest extends AbstractBootstrap3
public function testStartTagForMultipartForm() public function testStartTagForMultipartForm()
{ {
$form = $this->factory->createBuilder('Symfony\Component\Form\Extension\Core\Type\FormType', null, array( $form = $this->factory->createBuilder('form', null, array(
'method' => 'get', 'method' => 'get',
'action' => 'http://example.com/directory', 'action' => 'http://example.com/directory',
)) ))
->add('file', 'Symfony\Component\Form\Extension\Core\Type\FileType') ->add('file', 'file')
->getForm(); ->getForm();
$html = $this->renderStart($form->createView()); $html = $this->renderStart($form->createView());
@ -143,7 +143,7 @@ abstract class AbstractBootstrap3HorizontalLayoutTest extends AbstractBootstrap3
public function testStartTagWithExtraAttributes() public function testStartTagWithExtraAttributes()
{ {
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\FormType', null, array( $form = $this->factory->create('form', null, array(
'method' => 'get', 'method' => 'get',
'action' => 'http://example.com/directory', 'action' => 'http://example.com/directory',
)); ));