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 -> http://api.symfony.com/2.3/Symfony/Component/HttpKernel/Client.html#method_submit
Thanks
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')
);
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!'
)),
array(),
array(
'HTTP_X-Requested-With' => 'XMLHttpRequest',
)
);
}
Hope this help
Actually there is a way to take advantage of Client::submit
but you need to create new Client instance if you want to do non-ajax requests after that (for now, see GitHub issue link below).
$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');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With