|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * This file is part of the Zikula package. |
| 7 | + * |
| 8 | + * Copyright Zikula - https://ziku.la/ |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view the LICENSE |
| 11 | + * file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace Zikula\Bundle\DynamicFormBundle\Tests\Form\Type; |
| 15 | + |
| 16 | +use Symfony\Component\EventDispatcher\EventDispatcher; |
| 17 | +use Symfony\Component\Form\FormExtensionInterface; |
| 18 | +use Symfony\Component\Form\PreloadedExtension; |
| 19 | +use Symfony\Component\Form\Test\TypeTestCase; |
| 20 | +use Symfony\Component\Translation\IdentityTranslator; |
| 21 | +use Zikula\Bundle\DynamicFormBundle\Entity\AbstractFormSpecification; |
| 22 | +use Zikula\Bundle\DynamicFormBundle\EventSubscriber\FormTypeChoiceEventSubscriber; |
| 23 | +use Zikula\Bundle\DynamicFormBundle\Form\Type\FormSpecificationType; |
| 24 | +use Zikula\Bundle\DynamicFormBundle\Form\Type\TranslationCollectionType; |
| 25 | +use Zikula\Bundle\DynamicFormBundle\Provider\LocaleProviderInterface; |
| 26 | + |
| 27 | +class FormSpecificationTypeTranslatedTest extends TypeTestCase |
| 28 | +{ |
| 29 | + /** |
| 30 | + * @return FormExtensionInterface[] |
| 31 | + */ |
| 32 | + protected function getExtensions(): array |
| 33 | + { |
| 34 | + $dispatcher = new EventDispatcher(); |
| 35 | + $translator = new IdentityTranslator(); |
| 36 | + $localeProvider = new class() implements LocaleProviderInterface { |
| 37 | + public function getSupportedLocales(): array |
| 38 | + { |
| 39 | + return ['en', 'de']; |
| 40 | + } |
| 41 | + |
| 42 | + public function getSupportedLocaleNames(string $displayLocale = null): array |
| 43 | + { |
| 44 | + return ['English' => 'en', 'German' => 'de']; |
| 45 | + } |
| 46 | + }; |
| 47 | + |
| 48 | + $dispatcher->addSubscriber(new FormTypeChoiceEventSubscriber($translator)); |
| 49 | + $formSpecificationType = new FormSpecificationType($dispatcher, true); |
| 50 | + $translationCollectionType = new TranslationCollectionType($localeProvider); |
| 51 | + |
| 52 | + return [ |
| 53 | + new PreloadedExtension([$formSpecificationType, $translationCollectionType], []), |
| 54 | + ]; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * @covers \Zikula\Bundle\DynamicFormBundle\Entity\AbstractFormSpecification |
| 59 | + */ |
| 60 | + public function testFormCreation(): void |
| 61 | + { |
| 62 | + $formData = new class() extends AbstractFormSpecification { |
| 63 | + }; |
| 64 | + $form = $this->factory->create(FormSpecificationType::class, $formData); |
| 65 | + $this->assertInstanceOf(TranslationCollectionType::class, $form->get('labels')->getConfig()->getType()->getInnerType()); |
| 66 | + $this->assertInstanceOf(TranslationCollectionType::class, $form->get('groups')->getConfig()->getType()->getInnerType()); |
| 67 | + $this->assertCount(2, $form->get('labels')); |
| 68 | + $this->assertCount(2, $form->get('groups')); |
| 69 | + $this->assertTrue($form->get('labels')->has('en')); |
| 70 | + $this->assertTrue($form->get('labels')->has('de')); |
| 71 | + $this->assertTrue($form->get('groups')->has('en')); |
| 72 | + $this->assertTrue($form->get('groups')->has('de')); |
| 73 | + } |
| 74 | +} |
0 commit comments