【问题标题】:How to unit test a repeated password in a form in Symfony?如何在 Symfony 中对表单中的重复密码进行单元测试?
【发布时间】:2016-01-06 19:06:01
【问题描述】:

我正在尝试在我的 Symfony 应用程序中对表单进行单元测试。普通表单可以正常工作,但是当我有重复的表单类型时,我无法测试。

测试:

class MoniteurCreationTypeTest extends TypeTestCase
{
    public function testSubmitValidData()
    {
        $formData = array(
            'username' => 'user',
            'plainPassword' => 'pass',
        );

        $type = new \AppBundle\Form\MoniteurCreationType();
        $form = $this->factory->create($type);
        $object = new Moniteur();

        $object->setUsername($formData['username']);
        $object->setPlainPassword($formData['password']);

        $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);
        }
    }
}

我的表单类型:

class MoniteurCreationType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('username', 'text', array('label' => 'Nom d\'utilisateur', 'attr' => array('placeholder' => 'Insérez ici votre nom d\'utilisateur')))
            ->add('plainPassword', 'repeated', array(
                'type' => 'password',
                'first_options' => array('label' => 'Mot de passe', 'attr' => array('placeholder' => 'Choisissez un mot de passe')),
                'second_options' => array('label' => 'Répéter le mot de passe', 'attr' => array('placeholder' => 'Veuillez entrer encore une fois le mot de passe choisit')),
                    )
            )
            ->add('envoyer', 'submit', array('attr' => array('class' => 'col-lg-12  col-xs-12 btn btn-primary submit')))
        ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'AppBundle\Entity\Moniteur'
        ));
    }

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

phpunit 日志:

There was 1 failure:

1) Tests\AppBundle\Form\MoniteurCreationTypeTest::testSubmitValidData
Failed asserting that two objects are equal.
--- Expected
+++ Actual
@@ @@
     'plainPassword' => null
-    'password' => 'pass'
+    'password' => null
     'email' => null
     'isActive' => null
     'derniereConnexion' => null
     'dateRecuperationMotDePasse' => null
 )

C:\web\www\testDoctrine\src\AppBundle\Tests\Form\MoniteurCreationTypeTest.php:29

FAILURES!
Tests: 8, Assertions: 60, Failures: 1.

所有其他表单类型单词,但在这里我在表单的单元测试中变成了一个空值。你知道为什么吗?

谢谢 最好的问候

【问题讨论】:

  • 一个注释 - 你的表单中没有名为 password 的字段 - 你不应该检查它,你应该检查 plainPassword
  • 是的,我也试过这个,但是还是一样的错误

标签: php forms unit-testing symfony


【解决方案1】:

这里发生了一些事情。

  • 您的 $formData 变量正在使用 password 的键,而键应该是 plainPassword,这是您在 MoniteurCreationType 中的字段名称
  • 这可能不是测试对象中的值是否正确设置的正确位置 - 这似乎更适合功能测试。表单单元测试只是为了测试表单的操作能力,而不是设置值
  • RepeatedType 由另外两个字段组成,通常命名为 firstsecond。如果您愿意,可以通过 first_namesecond_name 选项更改这些选项。

如果你真的想向你的表单提交正确的数据,然后测试它是否已经设置(如果你dump($form->getData());你会看到它),那么你需要按如下方式创建表单数据:

$formData = array(
    'username' => 'user',
    'plainPasword' => array('first' => 'pass', 'second' => 'pass),
);

【讨论】:

  • 感谢您的回答,但我仍然收到错误:断言两个对象相等失败。 --- 预期 +++ 实际 @@ @@ 'username' => 'user' - 'plainPassword' => 'user' + 'plainPassword' => null 'password' => null 'email' => null 'isActive ' => null 'derniereConnexion' => null 'dateRecuperationMotDePasse' => null
  • 我认为您正在测试错误的东西 - 您不会使用此单元测试来测试您的实体是否已使用传递到您的表单中的数据进行了更新(请参阅我的第二个要点)。这是为了对表单本身进行单元测试,而不是它与对象的交互方式。
  • 阅读您的评论,我明白了。我认为我做错了测试,但我不知道如何做一个更好的测试?
  • Symfony 食谱文章中的 TestObject - 我在任何地方都找不到,而且这篇文章所基于的原始博客文章也没有提及它。如果你愿意,你可以简单地做$this->assertEquals($formData, $form->getData());,这可能会测试你需要的一切。从那里你可以完全摆脱你的 $object 设置和断言。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-09-19
  • 2018-09-27
  • 2016-08-17
  • 2023-01-23
  • 2014-09-21
  • 2021-10-07
  • 2013-05-08
相关资源
最近更新 更多