|
| 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\Component\Form\Tests\Extension\Core\Type; |
| 13 | + |
| 14 | +use Symfony\Component\Form\Extension\Core\CoreExtension; |
| 15 | +use Symfony\Component\Form\Test\TypeTestCase; |
| 16 | +use Symfony\Contracts\Translation\TranslatorInterface; |
| 17 | + |
| 18 | +class ChoiceTypeTranslationTest extends TypeTestCase |
| 19 | +{ |
| 20 | + public const TESTED_TYPE = 'Symfony\Component\Form\Extension\Core\Type\ChoiceType'; |
| 21 | + |
| 22 | + private $choices = [ |
| 23 | + 'Bernhard' => 'a', |
| 24 | + 'Fabien' => 'b', |
| 25 | + 'Kris' => 'c', |
| 26 | + 'Jon' => 'd', |
| 27 | + 'Roman' => 'e', |
| 28 | + ]; |
| 29 | + |
| 30 | + protected function getExtensions() |
| 31 | + { |
| 32 | + $translator = $this->createMock(TranslatorInterface::class); |
| 33 | + $translator->expects($this->any())->method('trans') |
| 34 | + ->willReturnCallback(function ($key, $params) { |
| 35 | + return strtr(sprintf('Translation of: %s', $key), $params); |
| 36 | + } |
| 37 | + ); |
| 38 | + |
| 39 | + return array_merge(parent::getExtensions(), [new CoreExtension(null, null, $translator)]); |
| 40 | + } |
| 41 | + |
| 42 | + public function testInvalidMessageAwarenessForMultiple() |
| 43 | + { |
| 44 | + $form = $this->factory->create(static::TESTED_TYPE, null, [ |
| 45 | + 'multiple' => true, |
| 46 | + 'expanded' => false, |
| 47 | + 'choices' => $this->choices, |
| 48 | + 'invalid_message' => 'You are not able to use value "{{ value }}"', |
| 49 | + ]); |
| 50 | + |
| 51 | + $form->submit(['My invalid choice']); |
| 52 | + $this->assertEquals( |
| 53 | + "ERROR: Translation of: You are not able to use value \"My invalid choice\"\n", |
| 54 | + (string) $form->getErrors(true) |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + public function testInvalidMessageAwarenessForMultipleWithoutScalarOrArrayViewData() |
| 59 | + { |
| 60 | + $form = $this->factory->create(static::TESTED_TYPE, null, [ |
| 61 | + 'multiple' => true, |
| 62 | + 'expanded' => false, |
| 63 | + 'choices' => $this->choices, |
| 64 | + 'invalid_message' => 'You are not able to use value "{{ value }}"', |
| 65 | + ]); |
| 66 | + |
| 67 | + $form->submit(new \stdClass()); |
| 68 | + $this->assertEquals( |
| 69 | + "ERROR: Translation of: You are not able to use value \"stdClass\"\n", |
| 70 | + (string) $form->getErrors(true) |
| 71 | + ); |
| 72 | + } |
| 73 | +} |
0 commit comments