Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select impossible value in select inputs with Symfony DomCrawler

I would like to test the behaviour of my application if I send wrong values in a select input in form.

This is my form in HTML :

<form (...)>
   (...)
<select name="select_input">
     <option value="1">text</option>
</select>

</select>

In my test, in get the form with the crawler and try to "select" an incorrect value :

$form['select_input'] = 9999999;
$client->submit($form);
/* EDIT */
/*I am expecting the user to not be redirected to the user page,
  and the server to respond with the same form, containing an error message */
$this->assertFalse($client->getResponse()->isRedirect('/success_page'));
$this->assertEquals(1, $client->getCrawler()->filter('input.error'));

But in the console, I get the message :

InvalidArgumentException: Input "user_profile[voteProfile]" cannot take "9999999" as a value (possible values: 1)

How can I choose an incorrect value for testing the answer ? It would be possible to send an incorrect value by a real server.

like image 501
Julien Fastré Avatar asked Apr 05 '14 17:04

Julien Fastré


1 Answers

The disableValidation function allow to select impossible values in options :

$form = $crawler->selectButton('button')->form();
$form->get('select_input')->disableValidation()->setValue(999999);

$client->submit($form);

And the job is done !

like image 154
Julien Fastré Avatar answered Oct 13 '22 16:10

Julien Fastré