Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 Custom form type using entity trying to test it

I am trying to test a form type I have creating that uses a field with class entity

here is the creation of the form

$builder
            ->add('name', 'text')
            ->add('description', 'textarea')
            ->add('services', 'entity', array('class' => 'MyBundle:Service', 'group_by' => 'category.name', 'property' => 'name', 'multiple' => true, 'required' => false));

This works very nice when I build the form, but then I am trying to unit test this type

Following this example on how to test my custom form types

I am getting this error

Symfony\Component\Form\Exception\Exception: Could not load type "entity"

The error is caused at the beginning of unit test at this command:

    $type = new MyType();
    $form = $this->factory->create($type);

any ideas on how to fix this error in order to test my custom form type using entities?

thanks in advance

like image 662
auslander Avatar asked May 02 '13 14:05

auslander


1 Answers

I guess you can't unit test form with entity types, because it's defined as a service. Have you tried adding it manually?

EDIT: IMHO you should mock the entity type, because it involves doctrine, which depends on an existing database connection and so on the full kernel loaded. So you're not unit testing any more. This would be a functional test. Maybe this the reason, why it's not available in the unit test.

like image 194
Emii Khaos Avatar answered Oct 08 '22 09:10

Emii Khaos