|
19 | 19 | use Symfony\Component\Form\FormError;
|
20 | 20 | use Symfony\Component\Form\FormView;
|
21 | 21 | use Symfony\Component\Form\Test\FormIntegrationTestCase;
|
| 22 | +use Symfony\Component\Translation\TranslatableMessage; |
22 | 23 |
|
23 | 24 | class FormExtensionFieldHelpersTest extends FormIntegrationTestCase
|
24 | 25 | {
|
@@ -81,6 +82,28 @@ protected function setUp(): void
|
81 | 82 | 'expanded' => true,
|
82 | 83 | 'label' => false,
|
83 | 84 | ])
|
| 85 | + ->add('parametrized_choice_label', ChoiceType::class, [ |
| 86 | + 'choices' => [ |
| 87 | + (object) ['value' => 'yes', 'label' => 'parametrized.%yes%'], |
| 88 | + (object) ['value' => 'no', 'label' => 'parametrized.%no%'], |
| 89 | + ], |
| 90 | + 'choice_value' => 'value', |
| 91 | + 'choice_label' => 'label', |
| 92 | + 'choice_translation_domain' => 'forms', |
| 93 | + 'choice_translation_parameters' => [ |
| 94 | + ['%yes%' => 'YES'], |
| 95 | + ['%no%' => 'NO'], |
| 96 | + ], |
| 97 | + ]) |
| 98 | + ->add('translatable_choice_label', ChoiceType::class, [ |
| 99 | + 'choices' => [ |
| 100 | + 'yes', |
| 101 | + 'no', |
| 102 | + ], |
| 103 | + 'choice_label' => static function (string $choice) { |
| 104 | + return new TranslatableMessage('parametrized.%value%', ['%value%' => $choice], 'forms'); |
| 105 | + }, |
| 106 | + ]) |
84 | 107 | ->getForm()
|
85 | 108 | ;
|
86 | 109 |
|
@@ -290,4 +313,40 @@ public function testFieldTranslatedChoicesMultiple()
|
290 | 313 | $this->assertSame('salt', $choicesArray[1]['value']);
|
291 | 314 | $this->assertSame('[trans]base.salt[/trans]', $choicesArray[1]['label']);
|
292 | 315 | }
|
| 316 | + |
| 317 | + public function testChoiceParametrizedLabel() |
| 318 | + { |
| 319 | + $choices = $this->translatorExtension->getFieldChoices($this->view->children['parametrized_choice_label']); |
| 320 | + |
| 321 | + $choicesArray = []; |
| 322 | + foreach ($choices as $label => $value) { |
| 323 | + $choicesArray[] = ['label' => $label, 'value' => $value]; |
| 324 | + } |
| 325 | + |
| 326 | + $this->assertCount(2, $choicesArray); |
| 327 | + |
| 328 | + $this->assertSame('yes', $choicesArray[0]['value']); |
| 329 | + $this->assertSame('[trans]parametrized.YES[/trans]', $choicesArray[0]['label']); |
| 330 | + |
| 331 | + $this->assertSame('no', $choicesArray[1]['value']); |
| 332 | + $this->assertSame('[trans]parametrized.NO[/trans]', $choicesArray[1]['label']); |
| 333 | + } |
| 334 | + |
| 335 | + public function testChoiceTranslatableLabel() |
| 336 | + { |
| 337 | + $choices = $this->translatorExtension->getFieldChoices($this->view->children['translatable_choice_label']); |
| 338 | + |
| 339 | + $choicesArray = []; |
| 340 | + foreach ($choices as $label => $value) { |
| 341 | + $choicesArray[] = ['label' => $label, 'value' => $value]; |
| 342 | + } |
| 343 | + |
| 344 | + $this->assertCount(2, $choicesArray); |
| 345 | + |
| 346 | + $this->assertSame('yes', $choicesArray[0]['value']); |
| 347 | + $this->assertSame('[trans]parametrized.yes[/trans]', $choicesArray[0]['label']); |
| 348 | + |
| 349 | + $this->assertSame('no', $choicesArray[1]['value']); |
| 350 | + $this->assertSame('[trans]parametrized.no[/trans]', $choicesArray[1]['label']); |
| 351 | + } |
293 | 352 | }
|
0 commit comments