jeudi 28 décembre 2017

Testing an AJAX call in phpunit / symfony, using client or crawler

i want to test a conroller which generates a page with a field that changes dynamicly with ajax.

Here is the code of 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>

How can i call this code from phpunit using client or crawler ?

I tried :

$this->client->request(
                'POST',
                '/',
                array('requete_prestataire[groupeCompetence]' =>2),
                array(),
                array(),
                array('HTTP_X-Requested-With' => 'XMLHttpRequest',
                    ));

But it doesnt work.

Thanks a lot !

Aucun commentaire:

Enregistrer un commentaire