Symfony2:TypeTestCase的意图

时间:2013-10-21 15:50:40

标签: forms unit-testing symfony testing symfony-forms

由于我最近3次尝试实现TypeTestCase都失败了,我开始怀疑 TypeTestCases 的意图,就像它们在doc中描述的那样。

最后一个因

之类的错误而失败
  • 无法加载“实体”类型
  • 选项“invalid_message”不存在。已知的选项是:“a [...]
  • 选项“约束”不存在。已知的选项是:“ac [...]

像最后两个错误一样的错误让我想知道 TypeTestCase 的想法是否还没有真正完成。

所以我想知道的是,如何最有效地测试自定义FormTypes。 (例如以下表格)

namespace Acme\UserBundle\Form\Type;

use Acme\UserBundle\Entity\Legalform;
use Acme\UserBundle\Entity\User;

use Acme\UserBundle\Repository\LegalformRepository;
use Symfony\Component\Form\FormBuilderInterface;
use FOS\UserBundle\Form\Type\RegistrationFormType as BaseType;
use Symfony\Component\Validator\Constraints\NotBlank;

class RegistrationFormType extends BaseType
{
   [...] // constructor  + dep. Injection etc.

    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add(
            'salutation',
            'choice',
            array(
                'empty_value' => 'Anrede',
                'constraints' => new NotBlank(),
                'choices' => array(
                    User::ENUM_FRAU => User::ENUM_FRAU,
                    User::ENUM_HERR => User::ENUM_HERR
                )
            )
        );
        [...]
        $builder->add(
            'password',
            'repeated',
            array(
                'required' => true,
                'type' => 'password',
                'first_options' => array('label' => 'fkjgkdgfskdhfsg'),
                'second_options' => array('label' => false),
                'invalid_message' => 'Die Passwörter stimmen nicht überein.'
            )
        );
        [...]

        $siteId = getMeSomeSiteId();
        $builder->add(
            'companyLegalForm',
            'entity',
            array(
                'class' => 'MyHammerUserBundle:LegalForm',
                'query_builder' => function (LegalformRepository $repository) use ($siteId) {
                    $queryBuilder = $repository->createQueryBuilder('l');
                    $queryBuilder->where('l.site = :site');
                    $queryBuilder->setParameter('site', $siteId);

                    return $queryBuilder;
                },
                'property' => 'title',
                'empty_value' => 'Rechtsform',
                'required' => true
            )
        );
    }
}

我的 TypeTestCase

namespace Acme\UserBundle\Form;

use Acme\UserBundle\Entity\Legalform;
use Acme\UserBundle\Entity\User;
use Acme\UserBundle\Form\Type\RegistrationFormType;
use Symfony\Bridge\Doctrine\Tests\DoctrineOrmTestCase;
use Symfony\Component\Form\Test\TypeTestCase;

class RegistrationFormTypeTest extends TypeTestCase
{
    public function testSubmitValidData()
    {
        $formData = array(
            'companyLegalForm' => $this->getLegalformFake(),
            'salutation' => 'Herr',
            'password' => array(
                'first' => 'esellese14',
                'second' => 'esellese14'
            )
        );

        $query = $this->getMockBuilder('Doctrine\ORM\AbstractQuery')
            ->disableOriginalConstructor()
            ->getMockForAbstractClass();
        $query->expects($this->once())
            ->method('execute')
            ->will($this->returnValue(array($this->getLegalformFake())));

        $queryBuilder = $this->getMockBuilder('Doctrine\ORM\QueryBuilder')
            ->disableOriginalConstructor()
            ->getMock();
        $queryBuilder->expects($this->once())
            ->method('getQuery')
            ->will($this->returnValue($query));

        $legalformRepository = $this->getMockBuilder('Acme\UserBundle\Repository\LegalformRepository')
            ->disableOriginalConstructor()
            ->getMock();
        $legalformRepository->expects($this->once())
            ->method('getQueryBuilderForRegistration')
            ->with(1)
            ->will($this->returnValue($queryBuilder));

        $type = new RegistrationFormType('Acme\UserBundle\Entity\User', $legalformRepository);
        $form = $this->factory->create($type);

        $object = new User();
        $object->fromArray($formData);

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

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

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

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

    /**
     * @return Legalform
     */
    private function getLegalformFake()
    {
        $legalform = new Legalform();
        $legalform->setId(1);
        $legalform->setTitle('wuff');
        return $legalform;
    }
}

我现在的解决方案是使用断言编写一个公共单元测试,已经为我的所有表单域正确调用了 $ builder-> add()。任何想法?

0 个答案:

没有答案