bicccio
bicccio

Reputation: 394

Sending post parameter with Silex test framework

I have this problem when I try to do functional test on a form submission:

testSearch.php:

public function testFormSubmission()
{
   $client = $this->createClient();
   $client->request('POST', '/search', array('nome' => 'Jan'));
   ...
}

app.php:

$app->post('/search', function (Request $request)  use ($app)
{
    $post_data = $request->get('form');
    ...
});

... but $post_data is NULL.

If I do the submission using the submit button in the browser everything works well...

Upvotes: 1

Views: 1540

Answers (2)

igorw
igorw

Reputation: 28259

You are calling $request->get('form'), but you didn't set the form parameter to anything. Perhaps this is what you are looking for:

$client->request('POST', '/search', array('form' => array('nome' => 'Jan')));

If not, you'll need to provide more context.

Upvotes: 3

mpm
mpm

Reputation: 20155

 $client->request('POST', '/search', array('nome' => 'Jan'));

the third argument of the request method seems to be for request parameters (?&nome=Jan )

you should use the crawler to simulate a form submission :

from the doc :

$form = $crawler->selectButton('submit')->form();

// set some values
$form['name'] = 'Lucas';
$form['form_name[subject]'] = 'Hey there!';

// submit the form
$crawler = $client->submit($form);

http://symfony.com/doc/current/book/testing.html

or use the sixth parameter to send a raw request body.

Upvotes: 1

Related Questions