I use phpunit to run functional tests, but I have a problem with multiple forms. The problem is that phpunit does not know JS, and I have a form with a dynamically populated selection field that requires jQuery.
Therefore, I need to submit the form data directly. The "book" gives an example:
// Directly submit a form (but using the Crawler is easier!) $client->request('POST', '/submit', array('name' => 'Fabien'));
When I used this example, the controller did not receive any form data. Inside, I saw that passing the key "name" of the array was wrong in my situation, since I need the name of the form, which was the "schedule" in my code. So I tried something like:
$client->request('POST', '/timesheet/create', array('timesheet[project]' => '100'));
But that still didn't work. In the controller, I tried to understand what was going on and what if something happened:
$postData = $request->request->get('timesheet'); $project = $postData['project'];
This did not work, and $ project remained empty. However, if I used the following code, I got the value:
$project = $request->request->get('timesheet[project]');
But it is clear that not what I want. At least though I see that there is some POST data. My last attempt was to try the following in a testing method:
$this->crawler = $this->client->request('POST', '/timesheet/create/', array('timesheet' => array(project => '100'));
So, I am trying to pass an array of "schedules" as the first element of an array of query parameters. But with this I get the error:
Symfony\Component\Form\Exception\UnexpectedTypeException: Expected argument of type "array", "string" given (uncaught exception) at /mnt/hgfs/pmt/src/vendor/symfony/src/Symfony/Component/Form/Form.php line 489
I would be very happy if someone could expand what is in the "book" about how I should work.
Linking the form in the controller:
if ($request->getMethod() == 'POST') { $form->bindRequest($request); if ($form->isValid()) { $postData = $request->request->get('timesheet'); $project = $postData['project']; $timesheetmanager = $this->get('wlp_pmt.timesheet_db_access'); $timesheetmanager->editTimesheet($timesheet); return $this->redirect($this->generateUrl('timesheet_list')); } }