Submit a form using ajax in a functional test

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

It seems that the submit method does not accept an argument that indicates whether it is ajax for the request as opposed to the request method -> http://api.symfony.com/2.3/Symfony/Component/HttpKernel/Client.html#method_submit

thank

Update1

////////////////////////////////////////////////
// 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!!!!
$client->submit($form);

// Why can't we tell client to submit using ajax???
// Like we do here in the request méthod
$client->request(
    'GET',
    '/post/hello-world',
    array(),
    array(),
    array('HTTP_X-Requested-With' => 'XMLHttpRequest')
);
+4
source share
2 answers

Symfony XmlHttpRequest . , :

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!'
        )),
        array(),
        array(
            'HTTP_X-Requested-With' => 'XMLHttpRequest',
        )
    );
}

+7

Client::submit, , -- ( . GitHub ).

$client->setServerParameter('HTTP_X-Requested-With', 'XMLHttpRequest');
$client->submit($form);

// The following method doesn't exist yet.
// @see https://github.com/symfony/symfony/issues/20306
// If this method gets added then you won't need to create
// new Client instances for following non-ajax requests,
// you can just do this:
// $client->unsetServerParameter('HTTP_X-Requested-With');
+1

All Articles