6

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.

3
  • Did you ever get round solving this issue??? and if you did HOW??? It's been bugging me for a couple of days. -Thanks
    – Drmjo
    Commented Jan 21, 2015 at 19:49
  • answer here stackoverflow.com/a/28075941/1258172
    – Drmjo
    Commented Jan 22, 2015 at 3:55
  • I added a simple answer, thanks for reminding my question :-) Commented Jan 24, 2015 at 21:00

2 Answers 2

10

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 !

0

Your error message says "(possible values: 1)", which matches the one possible value in your form. So, really I think you are getting an exception that you should expect. If a real server were to send an incorrect value, I would suspect this same exception would occur.

In your test, try adding an annotation, like this:

/**
* @expectedException InvalidArgumentException
*/
public function testForm()
{

//test logic here

}

Or:

public function testForm()
{

$this->setExpectedException('InvalidArgumentException');

}

More information here

1
  • Thanks for your answer, but I am not expecting an Exception from the crawler, but an answer from the server. I edited my post because it was not obvious. Commented Apr 8, 2014 at 11:32

Not the answer you're looking for? Browse other questions tagged or ask your own question.