minor #16729 [Form] Fix tests and reference usage (nicolas-grekas)

This PR was merged into the 2.7 branch.

Discussion
----------

[Form] Fix tests and reference usage

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

Commits
-------

6001644 [Form] Fix tests and reference usage
This commit is contained in:
Nicolas Grekas 2015-11-29 10:16:52 +01:00
commit a3991535d7
2 changed files with 32 additions and 31 deletions

View File

@ -238,7 +238,7 @@ class ChoiceType extends AbstractType
*/
public function configureOptions(OptionsResolver $resolver)
{
$choiceLabels = array();
$choiceLabels = (object) array('labels' => array());
$choiceListFactory = $this->choiceListFactory;
$emptyData = function (Options $options) {
@ -254,9 +254,9 @@ class ChoiceType extends AbstractType
};
// 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
$choiceLabels = array();
$choiceLabels->labels = array();
// This closure is irrelevant when "choices_as_values" is set to true
if ($options['choices_as_values']) {
@ -269,7 +269,7 @@ class ChoiceType extends AbstractType
};
// 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
// "choices" option stores them in the $choiceLabels variable
@ -277,14 +277,15 @@ class ChoiceType extends AbstractType
$options->offsetGet('choices');
// Pick labels from $choiceLabels if available
// Don't invoke count() to avoid creating a copy of the array (yet)
if ($choiceLabels) {
if ($choiceLabels->labels) {
// 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
// variable (contrary to the global reference shared by all
// variable (contrary to the $choiceLabels object shared by all
// forms)
return function ($choice, $key) use ($choiceLabels) {
return $choiceLabels[$key];
$labels = $choiceLabels->labels;
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
* "choice_label" closure by default.
*
* @param array $choices The choice labels indexed by choices.
* Labels are replaced by generated keys.
* @param array $choiceLabels The array that receives the choice labels
* indexed by generated keys.
* @param int|null $nextKey The next generated key.
* @param array $choices The choice labels indexed by choices.
* Labels are replaced by generated keys.
* @param object $choiceLabels The object that receives the choice labels
* indexed by generated keys.
* @param int $nextKey The next generated key.
*
* @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)) {
self::normalizeLegacyChoices($choiceLabel, $choiceLabels, $nextKey);
$choiceLabel = ''; // Dereference $choices[$choice]
self::normalizeLegacyChoices($choices[$choice], $choiceLabels, $nextKey);
continue;
}
$choiceLabels[$nextKey] = $choiceLabel;
$choices[$choice] = $nextKey;
++$nextKey;
$choiceLabels->labels[$nextKey] = $choiceLabel;
$choices[$choice] = $nextKey++;
}
}
}

View File

@ -15,7 +15,7 @@ abstract class AbstractBootstrap3HorizontalLayoutTest extends AbstractBootstrap3
{
public function testLabelOnForm()
{
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\DateType');
$form = $this->factory->createNamed('name', 'date');
$view = $form->createView();
$this->renderWidget($view, array('label' => 'foo'));
$html = $this->renderLabel($view);
@ -30,7 +30,7 @@ abstract class AbstractBootstrap3HorizontalLayoutTest extends AbstractBootstrap3
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(
'attr' => array(
'class' => 'my&class',
@ -47,7 +47,7 @@ abstract class AbstractBootstrap3HorizontalLayoutTest extends AbstractBootstrap3
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(
'label_attr' => array(
'class' => 'my&class',
@ -64,7 +64,7 @@ abstract class AbstractBootstrap3HorizontalLayoutTest extends AbstractBootstrap3
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(
'label_attr' => array(
'class' => 'my&class',
@ -82,7 +82,7 @@ abstract class AbstractBootstrap3HorizontalLayoutTest extends AbstractBootstrap3
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',
));
$html = $this->renderLabel($form->createView(), null, array(
@ -102,7 +102,7 @@ abstract class AbstractBootstrap3HorizontalLayoutTest extends AbstractBootstrap3
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',
'action' => 'http://example.com/directory',
));
@ -114,7 +114,7 @@ abstract class AbstractBootstrap3HorizontalLayoutTest extends AbstractBootstrap3
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',
'action' => 'http://example.com/directory',
));
@ -129,11 +129,11 @@ abstract class AbstractBootstrap3HorizontalLayoutTest extends AbstractBootstrap3
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',
'action' => 'http://example.com/directory',
))
->add('file', 'Symfony\Component\Form\Extension\Core\Type\FileType')
->add('file', 'file')
->getForm();
$html = $this->renderStart($form->createView());
@ -143,7 +143,7 @@ abstract class AbstractBootstrap3HorizontalLayoutTest extends AbstractBootstrap3
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',
'action' => 'http://example.com/directory',
));