我有一个包含两个字段的表单。提交后,将重定向到显示该表单的同一个控制器。
方法如下:
public function chartsAction($path, Request $request)
{
// display form
if(isset($_POST))
{
// handle form submission
}
}
如果它不是POST请求,则网址为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());
}
首先断言给我假。
答案 0 :(得分:2)
我认为你应该跳过" 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());