Select impossible value in select inputs with Symfony DomCrawler

梦想的初衷 提交于 2019-12-12 14:13:36

问题


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.


回答1:


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 !




回答2:


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



来源:https://stackoverflow.com/questions/22884366/select-impossible-value-in-select-inputs-with-symfony-domcrawler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!