How can I make and do submit a functional test of EntityType in Symfony3?

I am making a functional test of a common form page, I want to submit its form and assert if it was submitted successfully. but the problem is in my formType class exists a child that has EntityType. how can I set a random value to $form of crawler and assert a form child if it was submitted successfully?

I tried read the public documentation of EntityType but didn’t understand about its functional test.

<?php

namespace CustomizeTests;

use FakerFactory;
use xxxxxxTestsEccubeControllerAdminAbstractTestCase;

class CustomerRankControllerTest extends AbstractTestCase
{

    public function testNewPageFormSend()
    {
        $faker = Factory::create('ja_JP');

        $formData = [
            'customer_rank_register[name]' => $faker->name,
            'customer_rank_register[rate]' => rand(1, 10000),
            'customer_rank_register[code]' => $this->makeRandStr(rand(1, 255)),
            'customer_rank_register[deliveryFeeFlg]' => (bool)Rand(0, 1),
            'customer_rank_register[customerChannel]' => ,
            'customer_rank_register[visible]' => (bool)rand(0, 1),
        ];

        $client = $this->makeLoginClient();

        $crawler = $client->request('GET', self::ADMIN_ROUTE.'/customer/rank/new');
        $this->isSuccessful($client->getResponse());

        $form = $crawler->filter('form#form1')->form();
        $this->assertNotEmpty($form);

        $form->setValues($formData);
        $crawler = $client->submit($form);


    }


    function makeRandStr(int $length) :string {
        $str = array_merge(range('a', 'z'), range('0', '9'), range('A', 'Z'));
        $r_str = null;
        for ($i = 0; $i < $length; $i++) {
            $r_str .= $str[rand(0, count($str) - 1)];
        }
        return $r_str;
    }
}



    }

customerChannel is the name of the EntityTyped child

<?php

namespace CustomizeForm;

use CustomizeEntityCustomerChannel;
use CustomizeEntityCustomerRank;
use SymfonyBridgeDoctrineFormTypeEntityType;
use SymfonyComponentFormAbstractType;
use SymfonyComponentFormExtensionCoreTypeChoiceType;
use SymfonyComponentFormExtensionCoreTypeIntegerType;
use SymfonyComponentFormExtensionCoreTypeTextType;
use SymfonyComponentFormFormBuilderInterface;
use SymfonyComponentOptionsResolverOptionsResolver;
use SymfonyComponentValidatorConstraints as Assert;

class CustomerRankRegisterType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name', TextType::class, [
                'required' => true,
                'constraints' => [
                    new AssertNotBlank(),
                    new AssertLength(['max' => 255]),
                ],
            ])
            ->add('rate', IntegerType::class, [
                'required' => true,
                'constraints' => [
                    new AssertNotBlank(),
                    new AssertGreaterThanOrEqual(1),
                ],
            ])
            ->add('code', TextType::class, [
                'required' => true,
                'constraints' => [
                    new AssertNotBlank(),
                    new AssertLength(['max' => 255]),
                ],
            ])
            ->add('deliveryFeeFlg', ChoiceType::class, [
                'required' => true,
                'expanded' => true,
                'choices' => [
                    'ON' => true,
                    'OFF' => false,
                ],
                'constraints' => [
                    new AssertNotNull(),
                ],
            ])
            ->add('customerChannel', EntityType::class, [
                'class' => CustomerChannel::class,
                'choice_label' => 'name',
                'required' => true,
                'constraints' => [
                    new AssertNotBlank(),
                ],
            ])
            ->add('visible', ChoiceType::class, [
                'label' => false,
                'choices' => ['admin.common.show' => true, 'admin.common.hide' => false],
                'required' => true,
                'expanded' => false,
            ])
        ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => CustomerRank::class,
        ]);
    }
}

this project is made on a CMS called EC-Cube4 but never mind this is almost Symfony3.

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *