Testing PUT in symfony 'php: // input' empty

In a symfony project, I have a PUT method, and I'm trying to read such data:

$data = file_get_contents('php://input'); 

When I use Postman, it works, the request is in form-data :

: data

value: {"es_title":"edit","es_text":"text edit"}

But when I try to work with WebTestCase in the project does not work, $data in the PUT method is empty. I try this in a test:

 $data = array( "data" => '{"es_title":"edit","es_text":"edit"}'); $this->client->request('PUT', $url, $data, array(), array('HTTP_apikey' => $apikey)); 

Also i try

 $data = array( 'data' => json_encode(array( 'es_title' => 'edit', 'es_text' => 'edit' )) ); $this->client->request('PUT', $url, $data, array(), array('HTTP_apikey' => $apikey)); 

How to do to pass the test?

+5
source share
1 answer

To get data from PUT, I use this inside the controller:

 $putData = json_decode($request->getContent(), true); 

To make a request from testCase, I use this:

  $params = [ 'es_title' => 'edit', 'es_text' => 'edit', ]; $this->client->request( 'PUT', $url, [], [], [ 'CONTENT_TYPE' => 'application/json', 'HTTP_X-Requested-With' => 'XMLHttpRequest' ], json_encode($params) ); 
+4
source

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


All Articles