I have a form with two fields. After submitting it, there's a redirection to the same controller that displayed that form.
The method is as follows:
public function chartsAction($path, Request $request)
{
// display form
if(isset($_POST))
{
// handle form submission
}
}
When it's not a POST request, the URL is charts/.
When it's a POST request, the URL is for example charts/dateFrom/2013-08/dateTo/2014-02, so that's the $path argument of the method, and two variables depending on the form.
Now I want to test this. The problem is that the form redirection gives me the same, charts/ website. It just doesn't add $path parameter to the route. What's going on? My test method:
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());
}
First assert gives me false.
I think you should skip "isRedirect" assert. In the form handler, you can set a success flash message, that will be rendered in the response page:
$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());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With