Symfony2 Functional Test: Passing Form Data Directly

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')); } } 
+6
source share
3 answers

If you want to know how to inject POST datasets using a test client ...

In your testing method do something like

 $crawler = $client->request('POST', '/foo', array( 'animal_sounds' => array( 'cow' => 'moo', 'duck' => 'quack' ) ); // This would encode to '/foo?animal_sounds%5Bcow%5D=moo&animal_sounds%5Bduck%5D=quack' $this->assertTrue( ... ); 

In the controller, you will get access to your parameters as follows:

 $data = $request->request->get('animal_sounds'); $cowNoise = $data['cow']; $duckNoise = $data['duck']; 

Or you could just use the forms API if the testing method entered the correct form data ...

+5
source

Do you have the $request parameter in your action? this is why my request->get() was empty:

 //WRONG public function projectAction() { $request = Request::createFromGlobals(); $project = $request->request->get('timesheet[project]'); //$project will be empty } //CORRECT public function projectAction(Request $request) { $project = $request->request->get('timesheet[project]'); //$project is not empty } 

see How to create a functional test that includes POST on a page with parameters?

+2
source

Try using $form->bind($clientData) instead of $form->bindRequest($request) .

0
source

Source: https://habr.com/ru/post/906873/


All Articles