在 symfony 'php://input' 中测试 PUT 为空

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

在 symfony 项目中,我有一个 PUT 方法,我尝试像这样读取数据:

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

当我使用 Postman 时它有效,请求在 form-data:

键:data

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

但是当我尝试在项目中使用 WebTestCase 时不起作用,PUT 方法中的 $data 为空。 我在测试中这样尝试:

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

我也试试

$data = array(
        'data' => json_encode(array(
            'es_title' => 'edit',
            'es_text' => 'edit'
        ))
    );

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

我怎样才能通过考试?

为了从 PUT 中获取数据,我在控制器中使用了这个:

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

为了从测试用例发出请求,我使用了这个:

    $params = [
        'es_title' => 'edit',
        'es_text' => 'edit',
    ];

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