简体   繁体   中英

Symfony: Functional Test with PRE_SUBMIT Form Event

i'm trying to test a form with a PRE_SUBMIT Form my FormType Class looks like this:

public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('productType', ChoiceType::class, [
                'choices' => [
                    'Software' => 'software',
                    'Television' => 'television',
                    'Giftcard' => 'giftcard',
                    'Bitte wählen' => '',
                ],
            ])
            ->add('productNumber', TextType::class)
            ->add('title', TextType::class)
            ->add('submit', SubmitType::class);

        $builder->addEventListener(
            FormEvents::PRE_SUBMIT,
            function (FormEvent $event)
            {
                $form = $event->getForm();
                $data = $event->getData();

                if ($data['productType'] === 'giftcard') {
                    $form->add('value', TextType::class);
                }
            }
        );
    } 

i have no clue how to do it, i only can reach the field value if i submit the form like this.

$client->submitForm('Submit', [
            'product[productType]' => 'giftcard',
            'product[productNumber]' => 'C123123',
            'product[title]' => 'TestCard',
        ]);

i cant do a second submit like this. But the form looks fine after the first submit. Hopefully someone can help me or give me some advice.

the form events PRE_SUBMIT, SUBMIT, POST_SUBMIT, are dispatched only when the form is submitted.

if you want that your listener get executed even when the form has data and there is no submit yet, you should make your listener listen for the PRE_SET_DATA event

and then, to test filling the value field, initialize the form with data (for example: $form = $this->createForm(yourFormClass::class, $data) in your controller)

and finally test the value field by adding it to your testing code

i did it like this with a pre submitted form. Now its working fine. If you want to add a new value with this event you shut take the crawler again after the first submit and add the value to the form.

    public function testRedirectAfterCreateGiftcard(): void
    {
        $client = static::createClient([
            'debug' => false,
        ]);
        $crawler = $client->request('GET', 'https://localhost/product/create');
        $client->followRedirects();

        $form = $crawler->selectButton('Submit')->form();
        $values = $form->getPhpValues();
        $values['product']['productType'] = 'giftcard';
        $values['product']['productNumber'] = 'B122222';
        $values['product']['title'] = 'Test';
        $client->request($form->getMethod(), $form->getUri(), $values, $form->getPhpFiles());
        $form->setValues($values);
        $client->submitForm('Submit');

        $form2 = $client->getCrawler()->selectButton('Submit')->form();
        $values2 = $form2->getPhpValues();
        $values2['product']['value'] = '25';
        $form2->setValues($values2);
        $client->submit($form2);

        $this->assertRouteSame('app_product_view', ['productType' => 'giftcard']);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM