使用具有动态生成的字段的表单测试控制器
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用具有动态生成的字段的表单测试控制器相关的知识,希望对你有一定的参考价值。
我有一个具有动态字段的表单:
<?php
namespace AppBundleForm;
//uses ...
class AnnonceType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('titre')
->add('description')
->add('groupeCompetence', EntityType::class, [
'class' => 'AppBundleEntityGroupeCompetences',
'choice_label' => 'nom',
'placeholder' => 'Sélectionnez votre type de service',
])
->add('prix')
->add('serviceADistance')
->add('ajouter', SubmitType::class);
$formModifier = function (FormInterface $form, GroupeCompetences $groupeCompetences=null){
$competences = null === $groupeCompetences ? array() : $groupeCompetences->getCompetences();
$form->add('competence', EntityType::class, array(
'class' => 'AppBundleEntityCompetence',
'choice_label' => 'nom',
'placeholder' => 'Choisir une compétence',
'choices' => $competences,
));
};
$builder->addEventListener(
FormEvents::PRE_SET_DATA,
function (FormEvent $event) use ($formModifier) {
// this would be your entity, i.e. CompetenceChoisie
$data = $event->getData();
//var_dump($data);
//die();
$formModifier($event->getForm(), $data->getGroupeCompetence());
}
);
$builder->get('groupeCompetence')->addEventListener(
FormEvents::POST_SUBMIT,
function (FormEvent $event) use ($formModifier) {
$groupeCompetences = $event->getForm()->getData();
$formModifier($event->getForm()->getParent(), $groupeCompetences);
}
);
}
}
我在ajax中有这个代码:
<script>
var $groupeCompetence = $('#requete_prestataire_groupeCompetence');
// When sport gets selected ...
$groupeCompetence.change(function() {
// ... retrieve the corresponding form.
var $form = $(this).closest('form');
// Simulate form data, but only include the selected sport value.
var data = {};
data[$groupeCompetence.attr('name')] = $groupeCompetence.val();
// Submit data via AJAX to the form's action path.
$.ajax({
url : $form.attr('action'),
type: $form.attr('method'),
data : data,
success: function(html) {
// Replace current position field ...
$('#requete_prestataire_competence').replaceWith(
// ... with the returned one from the AJAX response.
$(html).find('#requete_prestataire_competence')
);
// Position field now displays the appropriate positions.
}
});
});
</script>
实际上,能力是根据群体能力动态生成的。
我想在PHPUnit中测试它。
我试过这个:
public function testIndexRechercheUtilisateurNonConnecte()
{
$crawler = $this->client->request('GET', '/');
$form = $crawler->selectButton('requete_prestataire_Rechercher')->form();
$form['requete_prestataire[groupeCompetence]'] = 2;
$form['requete_prestataire[competence]'] = "";
$crawler = $this->client->submit($form);
$this->assertTrue($this->client->getResponse()->isRedirect());
$client->followRedirect();
/*$this->assertEquals(3, $crawler->filter('a [class = "btn-sm btn-primary"]')->count());*/
}
问题是:$ form ['requete prestataire [compe]']是动态生成的,就像我说的那样。
我希望能够在测试中执行ajax请求,然后测试输出。
我该怎么办?
提前致谢
答案
我找到了解决方案:
您必须禁用验证,并在生成时发送表单:
$crawler = $this->client->request('GET', '/');
$form = $crawler->selectButton('requete_prestataire_Rechercher')->form();
$form['requete_prestataire[groupeCompetence]'] = 2;
$form['requete_prestataire[competence]']->disableValidation()->select(50);
$crawler = $this->client->submit($form);
以上是关于使用具有动态生成的字段的表单测试控制器的主要内容,如果未能解决你的问题,请参考以下文章
Tableview 控制器在其静态单元格中具有动态 tableview
仅使用纯javascript在单击按钮时动态生成表单输入字段递增和递减