在Symfony 3.3项目中测试表单(表单类型)

时间:2017-11-14 14:59:44

标签: unit-testing symfony

在Symfony 3.3项目中,我尝试测试这个简单的形式:

class FooFormType extends AbstractType
{
    private $fooService;

    public function __construct(FooService $fooService)
    {
        $this->fooService = $fooService;
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add(
                'bar',
                EntityType::class,
                [
                    'class' => Bar::class,
                    'choice_label' => 'title',
                    'placeholder' => 'Please select a bar',
                ]
            )
            ->add(
                'baz',
                ChoiceType::class,
                [
                    'choices' => $this->fooService->lorem(),
                ]
            )
        ;
    }

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

我按照我在http://symfony.com/doc/current/form/unit_testing.html#testings-types-from-the-service-container找到的指令编写了这个(简化的)测试:

class FooFormTypeTest extends TypeTestCase
{
    protected function getExtensions()
    {
        $fooServiceDummy = $this->createMock(FooService::class);
        $fooFormType = new FooFormType($fooServiceDummy);

        $managerRegistryDummy = $this->createMock(ManagerRegistry::class);
        $entityFormType = new EntityType($managerRegistryDummy);

        return [
            new PreloadedExtension([$fooFormType, $entityFormType], []),
        ];
    }

    /**
     * @test
     */
    public function submitValidData()
    {
        $form = $this->factory->create(FooFormType::class);
    }
}

不幸的是,抛出了这个异常:

  

Symfony \ Component \ Form \ Exception \ RuntimeException:Class" AppBundle \ Entity \ Bar"似乎不是一个托管的Doctrine实体。你忘了映射吗?

这里有什么问题?

0 个答案:

没有答案