symfony 2 phpunit中的测试表格
我有一个有两个字段的表单。提交表单后,会重定向到显示该表单的同一控制器 方法如下:symfony 2 phpunit中的测试表格,php,forms,unit-testing,symfony,Php,Forms,Unit Testing,Symfony,我有一个有两个字段的表单。提交表单后,会重定向到显示该表单的同一控制器 方法如下: public function chartsAction($path, Request $request) { // display form if(isset($_POST)) { // handle form submission } } 如果不是POST请求,则URL为charts/ 当它是POST请求时,URL是例如charts/dateFrom/2013-08/d
public function chartsAction($path, Request $request)
{
// display form
if(isset($_POST))
{
// handle form submission
}
}
如果不是POST请求,则URL为charts/
当它是POST请求时,URL是例如charts/dateFrom/2013-08/dateTo/2014-02
,因此这是该方法的$path参数,两个变量取决于表单
现在我想测试一下。问题是表单重定向给我的是相同的图表/网站。它只是没有向路由添加$path参数。发生什么事?我的测试方法:
public function testChartsAction()
{
$crawler = $this->client->request('GET', '/charts', [], [], $this->credintials);
$form = $crawler->selectButton('dateChooser[Choose]')->form([
'dateChooser[dateFrom]' => '2014-08',
'dateChooser[dateTo]' => '2014-12',
]);
$this->client->submit($form);
$crawler = $this->client->followRedirect();
$this->assertTrue($this->client->getResponse()->isRedirect('/charts/dateFrom/2014-08/dateTo/2014-12'));
$this->assertTrue($this->client->getResponse()->isSuccessful());
}
第一个断言为false。我认为您应该跳过“isRedirect”断言。在表单处理程序中,您可以设置一条成功flash消息,该消息将在响应页面中呈现:
$client = $this->makeClient(true);
$client->followRedirects();
$crawler = $client->request('GET', $url);
$form = $crawler->filter('#formID')->form();
$form->setValues(array(
"formsample[firstname]" => "test firstname",
"formsample[lastname]" => "test lastname"
));
$client->setServerParameter("HTTP_X-Requested-With" , "XMLHttpRequest");
$client->submit($form);
$response = $client->getResponse();
$this->assertContains('Your data has been saved!', $response->getContent());
在followRedirect方法之前尝试testisRedirect,它仍然提供/charts重定向,而不从表单获取参数。