8000 [Form] Fix same choice loader with different choice values by HeahDude · Pull Request #46098 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Form] Fix same choice loader with different choice values #46098

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
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
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

use Doctrine\Persistence\ObjectManager;
use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
use Symfony\Component\Form\ChoiceList\ChoiceListInterface;
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;

/**
Expand All @@ -29,9 +28,9 @@ class DoctrineChoiceLoader implements ChoiceLoaderInterface
private $objectLoader;

/**
* @var ChoiceListInterface
* @var array|null
*/
private $choiceList;
private $choices;

/**
* Creates a new choice loader.
Expand Down Expand Up @@ -74,15 +73,13 @@ public function __construct(ObjectManager $manager, string $class, IdReader $idR
*/
public function loadChoiceList($value = null)
{
if ($this->choiceList) {
return $this->choiceList;
if (null === $this->choices) {
$this->choices = $this->objectLoader
? $this->objectLoader->getEntities()
: $this->manager->getRepository($this->class)->findAll();
}

$objects = $this->objectLoader
? $this->objectLoader->getEntities()
: $this->manager->getRepository($this->class)->findAll();

return $this->choiceList = new ArrayChoiceList($objects, $value);
return new ArrayChoiceList($this->choices, $value);
}

/**
Expand All @@ -100,7 +97,7 @@ public function loadValuesForChoices(array $choices, $value = null)
$optimize = $this->idReader && (null === $value || \is_array($value) && $value[0] === $this->idReader);

// Attention: This optimization does not check choices for existence
if ($optimize && !$this->choiceList && $this->idReader->isSingleId()) {
if ($optimize && !$this->choices && $this->idReader->isSingleId()) {
$values = [];

// Maintain order and indices of the given objects
Expand Down Expand Up @@ -136,7 +133,7 @@ public function loadChoicesForValues(array $values, $value = null)
// a single-field identifier
$optimize = $this->idReader && (null === $value || \is_array($value) && $this->idReader === $value[0]);

if ($optimize && !$this->choiceList && $this->objectLoader && $this->idReader->isSingleId()) {
if ($optimize && !$this->choices && $this->objectLoader && $this->idReader->isSingleId()) {
$unorderedObjects = $this->objectLoader->getEntitiesByIds($this->idReader->getIdField(), $values);
$objectsById = [];
$objects = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,7 @@ public function testLoadChoiceListUsesObjectLoaderIfAvailable()
$this->assertEquals($choiceList, $loaded = $loader->loadChoiceList());

// no further loads on subsequent calls

$this->assertSame($loaded, $loader->loadChoiceList());
$this->assertEquals($loaded, $loader->loadChoiceList());
}

public function testLoadValuesForChoices()
Expand Down
28 changes: 28 additions & 0 deletions src/Symfony/Bridge/Doctrine/Tests/Form/Type/EntityTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1779,4 +1779,32 @@ public function testSubmitNullMultipleUsesDefaultEmptyData()
$this->assertEquals($collection, $form->getNormData());
$this->assertEquals($collection, $form->getData());
}

public function testWithSameLoaderAndDifferentChoiceValueCallbacks()
{
$entity1 = new SingleIntIdEntity(1, 'Foo');
$entity2 = new SingleIntIdEntity(2, 'Bar');
$this->persist([$entity1, $entity2]);

$view = $this->factory->create(FormTypeTest::TESTED_TYPE)
->add('entity_one', self::TESTED_TYPE, [
'em' => 'default',
'class' => self::SINGLE_IDENT_CLASS,
])
->add('entity_two', self::TESTED_TYPE, [
'em' => 'default',
'class' => self::SINGLE_IDENT_CLASS,
'choice_value' => function ($choice) {
return $choice ? $choice->name : '';
},
])
->createView()
;

$this->assertSame('1', $view['entity_one']->vars['choices'][1]->value);
$this->assertSame('2', $view['entity_one']->vars['choices'][2]->value);

$this->assertSame('Foo', $view['entity_two']->vars['choices']['Foo']->value);
$this->assertSame('Bar', $view['entity_two']->vars['choices']['Bar']->value);
}
}
2 changes: 1 addition & 1 deletion src/Symfony/Bridge/Doctrine/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"symfony/stopwatch": "^3.4|^4.0|^5.0",
"symfony/config": "^4.2|^5.0",
"symfony/dependency-injection": "^3.4|^4.0|^5.0",
"symfony/form": "^4.4.11|^5.0.11",
"symfony/form": "^4.4.41|^5.0.11",
"symfony/http-kernel": "^4.3.7",
"symfony/messenger": "^4.4|^5.0",
"symfony/property-access": "^3.4|^4.0|^5.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ class CallbackChoiceLoader implements ChoiceLoaderInterface
private $callback;

/**
* The loaded choice list.
* The loaded choices.
*
* @var ArrayChoiceList
* @var array|null
*/
private $choiceList;
private $choices;

/**
* @param callable $callback The callable returning an array of choices
Expand All @@ -42,11 +42,11 @@ public function __construct(callable $callback)
*/
public function loadChoiceList($value = null)
{
if (null !== $this->choiceList) {
return $this->choiceList;
if (null === $this->choices) {
$this->choices = ($this->callback)();
}

return $this->choiceList = new ArrayChoiceList(($this->callback)(), $value);
return new ArrayChoiceList($this->choices, $value);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function testGetChoiceLoadersLoadsLoadedListOnFirstCall()

$this->assertSame(['RESULT'], $list->getChoices());
$this->assertSame(['RESULT'], $list->getChoices());
$this->assertSame(1, $calls);
$this->assertSame(2, $calls);
}

public function testGetValuesLoadsLoadedListOnFirstCall()
Expand All @@ -46,7 +46,7 @@ public function testGetValuesLoadsLoadedListOnFirstCall()

$this->assertSame(['RESULT'], $list->getValues());
$this->assertSame(['RESULT'], $list->getValues());
$this->assertSame(1, $calls);
$this->assertSame(2, $calls);
}

public function testGetStructuredValuesLoadsLoadedListOnFirstCall()
Expand All @@ -60,7 +60,7 @@ public function testGetStructuredValuesLoadsLoadedListOnFirstCall()

$this->assertSame(['RESULT'], $list->getStructuredValues());
$this->assertSame(['RESULT'], $list->getStructuredValues());
$this->assertSame(1, $calls);
$this->assertSame(2, $calls);
}

public function testGetOriginalKeysLoadsLoadedListOnFirstCall()
Expand All @@ -79,7 +79,7 @@ public function testGetOriginalKeysLoadsLoadedListOnFirstCall()

$this->assertSame(['foo' => 'a', 'bar' => 'b', 'baz' => 'c'], $list->getOriginalKeys());
$this->assertSame(['foo' => 'a', 'bar' => 'b', 'baz' => 'c'], $list->getOriginalKeys());
$this->assertSame(3, $calls);
$this->assertSame(6, $calls);
}

public function testGetChoicesForValuesForwardsCallIfListNotLoaded()
Expand All @@ -98,7 +98,7 @@ public function testGetChoicesForValuesForwardsCallIfListNotLoaded()

$this->assertSame(['foo', 'bar'], $list->getChoicesForValues(['a', 'b']));
$this->assertSame(['foo', 'bar'], $list->getChoicesForValues(['a', 'b']));
$this->assertSame(3, $calls);
$this->a F438 ssertSame(6, $calls);
}

public function testGetChoicesForValuesUsesLoadedList()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,18 @@ public function testLoadChoiceList()
$this->assertInstanceOf(ChoiceListInterface::class, self::$loader->loadChoiceList(self::$value));
}

public function testLoadChoiceListOnlyOnce()
public function testLoadChoicesOnlyOnce()
{
$loadedChoiceList = self::$loader->loadChoiceList(self::$value);
$calls = 0;
$loader = new CallbackChoiceLoader(function () use (&$calls) {
++$calls;

$this->assertSame($loadedChoiceList, self::$loader->loadChoiceList(self::$value));
return [1];
});
$loadedChoiceList = $loader->loadChoiceList();

$this->assertNotSame($loadedChoiceList, $loader->loadChoiceList());
$this->assertSame(1, $calls);
}

public function testLoadChoicesForValuesLoadsChoiceListOnFirstCall()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,19 @@ public function testLoadChoiceList()
$this->assertInstanceOf(ChoiceListInterface::class, self::$loader->loadChoiceList(self::$value));
}

public function testLoadChoiceListOnlyOnce()
public function testLoadChoicesOnlyOnce()
{
$loadedChoiceList = self::$loader->loadChoiceList(self::$value);
$calls = 0;
$loader = new IntlCallbackChoiceLoader(function () use (&$calls) {
++$calls;

$this->assertSame($loadedChoiceList, self::$loader->loadChoiceList(self::$value));
return self::$choices;
});

$loadedChoiceList = $loader->loadChoiceList(self::$value);

$this->assertNotSame($loadedChoiceList, $loader->loadChoiceList(self::$value));
$this->assertSame(1, $calls);
}

public function testLoadChoicesForValuesLoadsChoiceListOnFirstCall()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Form\Tests\Extension\Core\Type;

use Symfony\Component\Form\ChoiceList\Loader\CallbackChoiceLoader;
use Symfony\Component\Form\ChoiceList\View\ChoiceGroupView;
use Symfony\Component\Form\ChoiceList\View\ChoiceView;
use Symfony\Component\Form\Extension\Validator\ValidatorExtension;
Expand Down Expand Up @@ -2165,4 +2166,32 @@ public function expandedIsEmptyWhenNoRealChoiceIsSelectedProvider()
'Placeholder submitted / single / not required / with a placeholder -> should not be empty' => [false, '', false, false, 'ccc'], // The placeholder is a selected value
];
}

public function testWithSameLoaderAndDifferentChoiceValueCallbacks()
{
$choiceLoader = new CallbackChoiceLoader(function () {
return [1, 2, 3];
});

$view = $this->factory->create(FormTypeTest::TESTED_TYPE)
->add('choice_one', self::TESTED_TYPE, [
'choice_loader' => $choiceLoader,
])
->add('choice_two', self::TESTED_TYPE, [
'choice_loader' => $choiceLoader,
'choice_value' => function ($choice) {
return $choice ? (string) $choice * 10 : '';
},
])
->createView()
;

$this->assertSame('1', $view['choice_one']->vars['choices'][0]->value);
$this->assertSame('2', $view['choice_one']->vars['choices'][1]->value);
$this->assertSame('3', $view['choice_one']->vars['choices'][2]->value);

$this->assertSame('10', $view['choice_two']->vars['choices'][0]->value);
$this->assertSame('20', $view['choice_two']->vars['choices'][1]->value);
$this->assertSame('30', $view['choice_two']->vars['choices'][2]->value);
}
}
0