Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

In symfony project I have a PUT method and I try to read data like this:

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

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

key: data

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

But when I try with WebTestCase in project not works, $data in PUT method is empty. I try like this in 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 can I do to pass the test?

like image 988
ximo12 Avatar asked Jun 15 '17 07:06

ximo12


1 Answers

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

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

To make the request from a 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)
    );
like image 107
Alessandro Minoccheri Avatar answered Sep 21 '22 10:09

Alessandro Minoccheri