Symfony表单元测试

时间:2015-09-30 12:11:17

标签: symfony phpunit

如何对具有实体父级的FormType进行单元测试?或者是phpUnit应该忽略该类的情况?代码包含在下面。

$

1) Audit\ReceiptBundle\Tests\Form\Type\ContactTypeTest::testSubmitValidData
Symfony\Component\OptionsResolver\Exception\MissingOptionsException: The required option "class" is missing.

测试代码:

class ContactType extends AbstractType
{

    public function __construct()
    {
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'empty_value' => 'Choose a Contact',
        ));
    }

    public function getParent()
    {
        return 'entity';
    }

    public function getName()
    {
        return 'contact';
    }

}

mockFieldEntityType()取自How to load form types in tests

中提供的答案
protected function getExtensions()
{
    return array(new PreloadedExtension(array(
            'entity' => $this->mockFieldEntityType(),
                ), array()));
}

public function testSubmitValidData()
{

    $formData = [
    ];

    $type = new ContactType();
    $resolve = new \Symfony\Component\OptionsResolver\OptionsResolver();
    $resolve->setDefaults([
        'class' => 'foo:bar'
    ]);
    $type->setDefaultOptions($resolve);
    $form = $this->factory->create($type);

    // submit the data to the form directly
    $form->submit($formData);

    $this->assertTrue($form->isSynchronized());
    $this->assertEquals($formData, $form->getData());

    $view = $form->createView();
    $children = $view->children;

    foreach (array_keys($formData) as $key) {
        $this->assertArrayHasKey($key, $children);
    }
}

0 个答案:

没有答案