Symfony2 Custom form type using entity trying to test it

爱⌒轻易说出口 提交于 2019-12-01 05:43:50

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.

According to the symfony docs under Adding a Type your Form Depends on

To create your form correctly, you need to make the type available to the form factory in your test. The easiest way is to register it manually before creating the parent form using the PreloadedExtension class:

class TestedTypeTest extends TypeTestCase
{
    protected function getExtensions()
    {
        $childType = new TestChildType();
        return array(new PreloadedExtension(array(
            $childType->getName() => $childType,
        ), array()));
    }

    public function testSubmitValidData()
    {
        $type = new TestedType();
        $form = $this->factory->create($type);

        // ... your test
    }
}

Add null for second parameters in method create.

$type = new MyType();
$form = $this->factory->create($type, null);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!