Skip to content

Commit 7f04636

Browse files
Merge branch '5.4' into 6.0
* 5.4: Fix merge [Mime] Throw exception when body in Email attach method is not ok [VarDumper][VarExporter] Deal with DatePeriod->include_end_date on PHP 8.2 [Cache] Throw when "redis_sentinel" is used with a non-Predis "class" option fix merge Bootstrap 4 fieldset for row errors [Form] Fix same choice loader with different choice values [Filesystem] Safeguard (sym)link calls Fix dumping extension config without bundle [HttpClient] Honor "max_duration" when replacing requests with async decorators [HttpClient] Add missing HttpOptions::setMaxDuration() [HttpFoundation] [Session] Overwrite invalid session id
2 parents 2830a55 + 2ec86df commit 7f04636

File tree

5 files changed

+56
-24
lines changed

5 files changed

+56
-24
lines changed

ChoiceList/Loader/AbstractChoiceLoader.php

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@
1919
*/
2020
abstract class AbstractChoiceLoader implements ChoiceLoaderInterface
2121
{
22-
/**
23-
* The loaded choice list.
24-
*/
25-
private $choiceList;
22+
private ?iterable $choices;
2623

2724
/**
2825
* @final
@@ -31,7 +28,7 @@ abstract class AbstractChoiceLoader implements ChoiceLoaderInterface
3128
*/
3229
public function loadChoiceList(callable $value = null): ChoiceListInterface
3330
{
34-
return $this->choiceList ?? ($this->choiceList = new ArrayChoiceList($this->loadChoices(), $value));
31+
return new ArrayChoiceList($this->choices ??= $this->loadChoices(), $value);
3532
}
3633

3734
/**
@@ -43,10 +40,6 @@ public function loadChoicesForValues(array $values, callable $value = null): arr
4340
return [];
4441
}
4542

46-
if (isset($this->choiceList)) {
47-
return $this->choiceList->getChoicesForValues($values);
48-
}
49-
5043
return $this->doLoadChoicesForValues($values, $value);
5144
}
5245

@@ -64,10 +57,6 @@ public function loadValuesForChoices(array $choices, callable $value = null): ar
6457
return array_map($value, $choices);
6558
}
6659

67-
if (isset($this->choiceList)) {
68-
return $this->choiceList->getValuesForChoices($choices);
69-
}
70-
7160
return $this->doLoadValuesForChoices($choices);
7261
}
7362

Tests/ChoiceList/LazyChoiceListTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function testGetChoiceLoadersLoadsLoadedListOnFirstCall()
3232

3333
$this->assertSame(['RESULT'], $list->getChoices());
3434
$this->assertSame(['RESULT'], $list->getChoices());
35-
$this->assertSame(1, $calls);
35+
$this->assertSame(2, $calls);
3636
}
3737

3838
public function testGetValuesLoadsLoadedListOnFirstCall()
@@ -46,7 +46,7 @@ public function testGetValuesLoadsLoadedListOnFirstCall()
4646

4747
$this->assertSame(['RESULT'], $list->getValues());
4848
$this->assertSame(['RESULT'], $list->getValues());
49-
$this->assertSame(1, $calls);
49+
$this->assertSame(2, $calls);
5050
}
5151

5252
public function testGetStructuredValuesLoadsLoadedListOnFirstCall()
@@ -60,7 +60,7 @@ public function testGetStructuredValuesLoadsLoadedListOnFirstCall()
6060

6161
$this->assertSame(['RESULT'], $list->getStructuredValues());
6262
$this->assertSame(['RESULT'], $list->getStructuredValues());
63-
$this->assertSame(1, $calls);
63+
$this->assertSame(2, $calls);
6464
}
6565

6666
public function testGetOriginalKeysLoadsLoadedListOnFirstCall()
@@ -79,7 +79,7 @@ public function testGetOriginalKeysLoadsLoadedListOnFirstCall()
7979

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

8585
public function testGetChoicesForValuesForwardsCallIfListNotLoaded()
@@ -98,7 +98,7 @@ public function testGetChoicesForValuesForwardsCallIfListNotLoaded()
9898

9999
$this->assertSame(['foo', 'bar'], $list->getChoicesForValues(['a', 'b']));
100100
$this->assertSame(['foo', 'bar'], $list->getChoicesForValues(['a', 'b']));
101-
$this->assertSame(3, $calls);
101+
$this->assertSame(6, $calls);
102102
}
103103

104104
public function testGetChoicesForValuesUsesLoadedList()

Tests/ChoiceList/Loader/CallbackChoiceLoaderTest.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,18 @@ public function testLoadChoiceList()
6767
$this->assertInstanceOf(ChoiceListInterface::class, self::$loader->loadChoiceList(self::$value));
6868
}
6969

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

74-
$this->assertSame($loadedChoiceList, self::$loader->loadChoiceList(self::$value));
76+
return [1];
77+
});
78+
$loadedChoiceList = $loader->loadChoiceList();
79+
80+
$this->assertNotSame($loadedChoiceList, $loader->loadChoiceList());
81+
$this->assertSame(1, $calls);
7582
}
7683

7784
public function testLoadChoicesForValuesLoadsChoiceListOnFirstCall()

Tests/ChoiceList/Loader/IntlCallbackChoiceLoaderTest.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,19 @@ public function testLoadChoiceList()
6868
$this->assertInstanceOf(ChoiceListInterface::class, self::$loader->loadChoiceList(self::$value));
6969
}
7070

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

75-
$this->assertSame($loadedChoiceList, self::$loader->loadChoiceList(self::$value));
77+
return self::$choices;
78+
});
79+
80+
$loadedChoiceList = $loader->loadChoiceList(self::$value);
81+
82+
$this->assertNotSame($loadedChoiceList, $loader->loadChoiceList(self::$value));
83+
$this->assertSame(1, $calls);
7684
}
7785

7886
public function testLoadChoicesForValuesLoadsChoiceListOnFirstCall()

Tests/Extension/Core/Type/ChoiceTypeTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2247,4 +2247,32 @@ public function testFilteredChoiceLoader()
22472247
new ChoiceView('c', 'c', 'Kris'),
22482248
], $form->createView()->vars['choices']);
22492249
}
2250+
2251+
public function testWithSameLoaderAndDifferentChoiceValueCallbacks()
2252+
{
2253+
$choiceLoader = new CallbackChoiceLoader(function () {
2254+
return [1, 2, 3];
2255+
});
2256+
2257+
$view = $this->factory->create(FormTypeTest::TESTED_TYPE)
2258+
->add('choice_one', self::TESTED_TYPE, [
2259+
'choice_loader' => $choiceLoader,
2260+
])
2261+
->add('choice_two', self::TESTED_TYPE, [
2262+
'choice_loader' => $choiceLoader,
2263+
'choice_value' => function ($choice) {
2264+
return $choice ? (string) $choice * 10 : '';
2265+
},
2266+
])
2267+
->createView()
2268+
;
2269+
2270+
$this->assertSame('1', $view['choice_one']->vars['choices'][0]->value);
2271+
$this->assertSame('2', $view['choice_one']->vars['choices'][1]->value);
2272+
$this->assertSame('3', $view['choice_one']->vars['choices'][2]->value);
2273+
2274+
$this->assertSame('10', $view['choice_two']->vars['choices'][0]->value);
2275+
$this->assertSame('20', $view['choice_two']->vars['choices'][1]->value);
2276+
$this->assertSame('30', $view['choice_two']->vars['choices'][2]->value);
2277+
}
22502278
}

0 commit comments

Comments
 (0)