smarber smarber - 1 year ago 97
PHP Question

submit a form using ajax in functional test

I'm creating a functional test for the inscription part of my project and I need to know how to test it if the form needs to go in an ajax request, otherwise the server will always return an empty inscription form.

Looks like the submit method doesn't take an argument that specifies whether it's an ajax a request unlike request method ->



// My functional test looks exactly like this //
$form = $buttonCrawlerNode->form(array(
'name' => 'Fabien',
'my_form[subject]' => 'Symfony rocks!',
// There is no way here I can tell client to submit using ajax!!!!

// Why can't we tell client to submit using ajax???
// Like we do here in the request m├ęthod
array('HTTP_X-Requested-With' => 'XMLHttpRequest')

Answer Source

The Symfony Request Object intercept an XmlHttpRequest in the header of the request. So simply add the correct header to your request in the test class, as example:

class FooFunctionalTest extends WebTestCase
    $client = static::CreateClient();
    $url = '/post/hello-world';
    // makes the POST request
    $crawler = $client->request('POST', $url, array(
        'my_form' => array(
            'subject' => 'Symfony rocks!'
            'HTTP_X-Requested-With' => 'XMLHttpRequest',

Hope this help

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download