1

TypeTestCase を実装する最後の 3 回の試みはすべて失敗したため、 docに記載されているTypeTestCasesの意図について疑問に思い始めました。

最後のものは次のようなエラーで失敗しました

  • タイプ「エンティティ」を読み込めませんでした
  • オプション「invalid_message」は存在しません。既知のオプションは次のとおりです: "a[...]
  • オプション「制約」は存在しません。既知のオプションは次のとおりです: "ac[...]

最後の 2 つのようなエラーは、TypeTestCaseを持つというアイデアがまだ本当に終わっていないのではないかと思いました。

だから私が知りたいのは、Custom 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()が正しく呼び出されていることをアサートする共通の単体テストを作成することです。何か案は?

4

0 に答える 0