From 3c43ca535ea549c8e7813e5ec5dc841271ea89b2 Mon Sep 17 00:00:00 2001 From: Silas Joisten Date: Sun, 8 Dec 2024 10:27:21 +0100 Subject: [PATCH] Adds test for FormType --- .../Unit/Form/Type/MultiStepTypeTest.php | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 src/LiveComponent/tests/Unit/Form/Type/MultiStepTypeTest.php diff --git a/src/LiveComponent/tests/Unit/Form/Type/MultiStepTypeTest.php b/src/LiveComponent/tests/Unit/Form/Type/MultiStepTypeTest.php new file mode 100644 index 00000000000..5163352f7d8 --- /dev/null +++ b/src/LiveComponent/tests/Unit/Form/Type/MultiStepTypeTest.php @@ -0,0 +1,81 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\UX\LiveComponent\Tests\Unit\Form\Type; + +use Symfony\Component\Form\Extension\Core\Type\TextType; +use Symfony\Component\Form\FormBuilderInterface; +use Symfony\Component\Form\Test\TypeTestCase; +use Symfony\Component\OptionsResolver\Exception\MissingOptionsException; +use Symfony\UX\LiveComponent\Form\Type\MultiStepType; + +/** + * @author Silas Joisten + */ +final class MultiStepTypeTest extends TypeTestCase +{ + public function testConfigureOptionsWithoutStepsThrowsException(): void + { + self::expectException(MissingOptionsException::class); + + $this->factory->create(MultiStepType::class); + } + + public function testConfigureOptionsWithStepsSetsDefaultForCurrentStepName(): void + { + $form = $this->factory->create(MultiStepType::class, [], [ + 'steps' => [ + 'general' => static function (): void {}, + 'contact' => static function (): void {}, + 'newsletter' => static function (): void {}, + ], + ]); + + self::assertSame('general', $form->createView()->vars['current_step_name']); + } + + public function testBuildViewHasStepNames(): void + { + $form = $this->factory->create(MultiStepType::class, [], [ + 'steps' => [ + 'general' => static function (): void {}, + 'contact' => static function (): void {}, + 'newsletter' => static function (): void {}, + ], + ]); + + self::assertSame(['general', 'contact', 'newsletter'], $form->createView()->vars['steps_names']); + } + + public function testFormOnlyHasCurrentStepForm(): void + { + $form = $this->factory->create(MultiStepType::class, [], [ + 'steps' => [ + 'general' => static function (FormBuilderInterface $builder): void { + $builder + ->add('firstName', TextType::class) + ->add('lastName', TextType::class); + }, + 'contact' => static function (FormBuilderInterface $builder): void { + $builder + ->add('address', TextType::class) + ->add('city', TextType::class); + }, + 'newsletter' => static function (): void {}, + ], + ]); + + self::assertArrayHasKey('firstName', $form->createView()->children); + self::assertArrayHasKey('lastName', $form->createView()->children); + self::assertArrayNotHasKey('address', $form->createView()->children); + self::assertArrayNotHasKey('city', $form->createView()->children); + } +}