Jul*_*tré 3 php phpunit symfony
如果我select在表单输入中发送错误的值,我想测试我的应用程序的行为.
这是我在HTML中的表单:
<form (...)>
(...)
<select name="select_input">
<option value="1">text</option>
</select>
</select>
Run Code Online (Sandbox Code Playgroud)
在我的测试中,在使用爬虫获取表单并尝试"选择"不正确的值:
$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'));
Run Code Online (Sandbox Code Playgroud)
但在控制台中,我收到消息:
InvalidArgumentException: Input "user_profile[voteProfile]" cannot take "9999999" as a value (possible values: 1)
Run Code Online (Sandbox Code Playgroud)
如何选择不正确的值来测试答案?真实服务器可能会发送错误的值.
该disableValidation功能允许在选项中选择不可能的值:
$form = $crawler->selectButton('button')->form();
$form->get('select_input')->disableValidation()->setValue(999999);
$client->submit($form);
Run Code Online (Sandbox Code Playgroud)
工作完成了!