2017-12-27 1091 views
1

我有具有動態場的一種形式:測試與具有動態地產生的場的形式的控制器

<?php 

namespace AppBundle\Form; 

//uses ... 
class AnnonceType extends AbstractType 
{ 

    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder 
     ->add('titre') 
     ->add('description') 
     ->add('groupeCompetence', EntityType::class, [ 
      'class'  => 'AppBundle\Entity\GroupeCompetences', 
      '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' => 'AppBundle\Entity\Competence', 
       '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> 

事實上,能力是根據GroupeCompetence動態生成的。

我想在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());*/ 


    } 

的問題是:$形式[ 'requete_prestataire [能力]']如我說的是動態地生成的。

我希望能夠在測試中做ajax請求,然後測試輸出。

我該怎麼辦?

在此先感謝

回答

0

我找到了解決辦法:

必須禁用驗證,併發送的形式,因爲它是產生:

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