【发布时间】: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