|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Bridge\Twig\Tests\Extension; |
| 13 | + |
| 14 | +use Symfony\Bridge\Twig\Extension\FormExtension; |
| 15 | +use Symfony\Bridge\Twig\Tests\Extension\Fixtures\StubTranslator; |
| 16 | +use Symfony\Component\Form\Extension\Core\Type\ChoiceType; |
| 17 | +use Symfony\Component\Form\Extension\Core\Type\FormType; |
| 18 | +use Symfony\Component\Form\Extension\Core\Type\TextType; |
| 19 | +use Symfony\Component\Form\FormError; |
| 20 | +use Symfony\Component\Form\FormView; |
| 21 | +use Symfony\Component\Form\Test\FormIntegrationTestCase; |
| 22 | + |
| 23 | +class FormExtensionFieldHelpersTest extends FormIntegrationTestCase |
| 24 | +{ |
| 25 | + /** |
| 26 | + * @var FormExtension |
| 27 | + */ |
| 28 | + private $rawExtension; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var FormExtension |
| 32 | + */ |
| 33 | + private $translatorExtension; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var FormView |
| 37 | + */ |
| 38 | + private $view; |
| 39 | + |
| 40 | + protected function getTypes() |
| 41 | + { |
| 42 | + return [new TextType(), new ChoiceType()]; |
| 43 | + } |
| 44 | + |
| 45 | + protected function setUp(): void |
| 46 | + { |
| 47 | + parent::setUp(); |
| 48 | + |
| 49 | + $this->rawExtension = new FormExtension(); |
| 50 | + $this->translatorExtension = new FormExtension(new StubTranslator()); |
| 51 | + |
| 52 | + $form = $this->factory->createNamedBuilder('register', FormType::class, ['username' => 'tgalopin']) |
| 53 | + ->add('username', TextType::class, [ |
| 54 | + 'label' => 'base.username', |
| 55 | + 'label_translation_parameters' => ['%label_brand%' => 'Symfony'], |
| 56 | + 'help' => 'base.username_help', |
| 57 | + 'help_translation_parameters' => ['%help_brand%' => 'Symfony'], |
| 58 | + 'translation_domain' => 'forms', |
| 59 | + ]) |
| 60 | + ->add('choice_flat', ChoiceType::class, [ |
| 61 | + 'choices' => [ |
| 62 | + 'base.yes' => 'yes', |
| 63 | + 'base.no' => 'no', |
| 64 | + ], |
| 65 | + 'choice_translation_domain' => 'forms', |
| 66 | + ]) |
| 67 | + ->add('choice_grouped', ChoiceType::class, [ |
| 68 | + 'choices' => [ |
| 69 | + 'base.europe' => [ |
| 70 | + 'base.fr' => 'fr', |
| 71 | + 'base.de' => 'de', |
| 72 | + ], |
| 73 | + 'base.asia' => [ |
| 74 | + 'base.cn' => 'cn', |
| 75 | + 'base.jp' => 'jp', |
| 76 | + ], |
| 77 | + ], |
| 78 | + 'choice_translation_domain' => 'forms', |
| 79 | + ]) |
| 80 | + ->getForm() |
| 81 | + ; |
| 82 | + |
| 83 | + $form->get('username')->addError(new FormError('username.max_length')); |
| 84 | + |
| 85 | + $this->view = $form->createView(); |
| 86 | + } |
| 87 | + |
| 88 | + public function testFieldName() |
| 89 | + { |
| 90 | + $this->assertFalse($this->view->children['username']->isRendered()); |
| 91 | + $this->assertSame('register[username]', $this->rawExtension->getFieldName($this->view->children['username'])); |
| 92 | + $this->assertTrue($this->view->children['username']->isRendered()); |
| 93 | + } |
| 94 | + |
| 95 | + public function testFieldValue() |
| 96 | + { |
| 97 | + $this->assertSame('tgalopin', $this->rawExtension->getFieldValue($this->view->children['username'])); |
| 98 | + } |
| 99 | + |
| 100 | + public function testFieldLabel() |
| 101 | + { |
| 102 | + $this->assertSame('base.username', $this->rawExtension->getFieldLabel($this->view->children['username'])); |
| 103 | + } |
| 104 | + |
| 105 | + public function testFieldTranslatedLabel() |
| 106 | + { |
| 107 | + $this->assertSame('[trans]base.username[/trans]', $this->translatorExtension->getFieldLabel($this->view->children['username'])); |
| 108 | + } |
| 109 | + |
| 110 | + public function testFieldHelp() |
| 111 | + { |
| 112 | + $this->assertSame('base.username_help', $this->rawExtension->getFieldHelp($this->view->children['username'])); |
| 113 | + } |
| 114 | + |
| 115 | + public function testFieldTranslatedHelp() |
| 116 | + { |
| 117 | + $this->assertSame('[trans]base.username_help[/trans]', $this->translatorExtension->getFieldHelp($this->view->children['username'])); |
| 118 | + } |
| 119 | + |
| 120 | + public function testFieldErrors() |
| 121 | + { |
| 122 | + $errors = $this->rawExtension->getFieldErrors($this->view->children['username']); |
| 123 | + $this->assertSame(['username.max_length'], iterator_to_array($errors)); |
| 124 | + } |
| 125 | + |
| 126 | + public function testFieldTranslatedErrors() |
| 127 | + { |
| 128 | + $errors = $this->translatorExtension->getFieldErrors($this->view->children['username']); |
| 129 | + $this->assertSame(['username.max_length'], iterator_to_array($errors)); |
| 130 | + } |
| 131 | + |
| 132 | + public function testFieldChoicesFlat() |
| 133 | + { |
| 134 | + $choices = $this->rawExtension->getFieldChoices($this->view->children['choice_flat']); |
| 135 | + |
| 136 | + $choicesArray = []; |
| 137 | + foreach ($choices as $label => $value) { |
| 138 | + $choicesArray[] = ['label' => $label, 'value' => $value]; |
| 139 | + } |
| 140 | + |
| 141 | + $this->assertCount(2, $choicesArray); |
| 142 | + |
| 143 | + $this->assertSame('yes', $choicesArray[0]['value']); |
| 144 | + $this->assertSame('base.yes', $choicesArray[0]['label']); |
| 145 | + |
| 146 | + $this->assertSame('no', $choicesArray[1]['value']); |
| 147 | + $this->assertSame('base.no', $choicesArray[1]['label']); |
| 148 | + } |
| 149 | + |
| 150 | + public function testFieldTranslatedChoicesFlat() |
| 151 | + { |
| 152 | + $choices = $this->translatorExtension->getFieldChoices($this->view->children['choice_flat']); |
| 153 | + |
| 154 | + $choicesArray = []; |
| 155 | + foreach ($choices as $label => $value) { |
| 156 | + $choicesArray[] = ['label' => $label, 'value' => $value]; |
| 157 | + } |
| 158 | + |
| 159 | + $this->assertCount(2, $choicesArray); |
| 160 | + |
| 161 | + $this->assertSame('yes', $choicesArray[0]['value']); |
| 162 | + $this->assertSame('[trans]base.yes[/trans]', $choicesArray[0]['label']); |
| 163 | + |
| 164 | + $this->assertSame('no', $choicesArray[1]['value']); |
| 165 | + $this->assertSame('[trans]base.no[/trans]', $choicesArray[1]['label']); |
| 166 | + } |
| 167 | + |
| 168 | + public function testFieldChoicesGrouped() |
| 169 | + { |
| 170 | + $choices = $this->rawExtension->getFieldChoices($this->view->children['choice_grouped']); |
| 171 | + |
| 172 | + $choicesArray = []; |
| 173 | + foreach ($choices as $groupLabel => $groupChoices) { |
| 174 | + $groupChoicesArray = []; |
| 175 | + foreach ($groupChoices as $label => $value) { |
| 176 | + $groupChoicesArray[] = ['label' => $label, 'value' => $value]; |
| 177 | + } |
| 178 | + |
| 179 | + $choicesArray[] = ['label' => $groupLabel, 'choices' => $groupChoicesArray]; |
| 180 | + } |
| 181 | + |
| 182 | + $this->assertCount(2, $choicesArray); |
| 183 | + |
| 184 | + $this->assertCount(2, $choicesArray[0]['choices']); |
| 185 | + $this->assertSame('base.europe', $choicesArray[0]['label']); |
| 186 | + |
| 187 | + $this->assertSame('fr', $choicesArray[0]['choices'][0]['value']); |
| 188 | + $this->assertSame('base.fr', $choicesArray[0]['choices'][0]['label']); |
| 189 | + |
| 190 | + $this->assertSame('de', $choicesArray[0]['choices'][1]['value']); |
| 191 | + $this->assertSame('base.de', $choicesArray[0]['choices'][1]['label']); |
| 192 | + |
| 193 | + $this->assertCount(2, $choicesArray[1]['choices']); |
| 194 | + $this->assertSame('base.asia', $choicesArray[1]['label']); |
| 195 | + |
| 196 | + $this->assertSame('cn', $choicesArray[1]['choices'][0]['value']); |
| 197 | + $this->assertSame('base.cn', $choicesArray[1]['choices'][0]['label']); |
| 198 | + |
| 199 | + $this->assertSame('jp', $choicesArray[1]['choices'][1]['value']); |
| 200 | + $this->assertSame('base.jp', $choicesArray[1]['choices'][1]['label']); |
| 201 | + } |
| 202 | + |
| 203 | + public function testFieldTranslatedChoicesGrouped() |
| 204 | + { |
| 205 | + $choices = $this->translatorExtension->getFieldChoices($this->view->children['choice_grouped']); |
| 206 | + |
| 207 | + $choicesArray = []; |
| 208 | + foreach ($choices as $groupLabel => $groupChoices) { |
| 209 | + $groupChoicesArray = []; |
| 210 | + foreach ($groupChoices as $label => $value) { |
| 211 | + $groupChoicesArray[] = ['label' => $label, 'value' => $value]; |
| 212 | + } |
| 213 | + |
| 214 | + $choicesArray[] = ['label' => $groupLabel, 'choices' => $groupChoicesArray]; |
| 215 | + } |
| 216 | + |
| 217 | + $this->assertCount(2, $choicesArray); |
| 218 | + |
| 219 | + $this->assertCount(2, $choicesArray[0]['choices']); |
| 220 | + $this->assertSame('[trans]base.europe[/trans]', $choicesArray[0]['label']); |
| 221 | + |
| 222 | + $this->assertSame('fr', $choicesArray[0]['choices'][0]['value']); |
| 223 | + $this->assertSame('[trans]base.fr[/trans]', $choicesArray[0]['choices'][0]['label']); |
| 224 | + |
| 225 | + $this->assertSame('de', $choicesArray[0]['choices'][1]['value']); |
| 226 | + $this->assertSame('[trans]base.de[/trans]', $choicesArray[0]['choices'][1]['label']); |
| 227 | + |
| 228 | + $this->assertCount(2, $choicesArray[1]['choices']); |
| 229 | + $this->assertSame('[trans]base.asia[/trans]', $choicesArray[1]['label']); |
| 230 | + |
| 231 | + $this->assertSame('cn', $choicesArray[1]['choices'][0]['value']); |
| 232 | + $this->assertSame('[trans]base.cn[/trans]', $choicesArray[1]['choices'][0]['label']); |
| 233 | + |
| 234 | + $this->assertSame('jp', $choicesArray[1]['choices'][1]['value']); |
| 235 | + $this->assertSame('[trans]base.jp[/trans]', $choicesArray[1]['choices'][1]['label']); |
| 236 | + } |
| 237 | +} |
0 commit comments