Laravel 使用 XML 测试 POST 路线

Laravel test POST routes with XML

我看过 this question,但这不是我想要的。

我在接受 XML 的 Lumen 应用程序中有一个 API 端点。在控制器中,我通过这样做读取数据:$request->getContent();.

我正在尝试编写 posts XML 到路由并检索响应的单元测试。我试过这个$response = $this->call('POST', '/api', $xml);,但是第三个参数必须是一个数组,而不是一个字符串。

我如何 post 一个 XML 字符串到单元测试中的端点?

您可以尝试将 xml 字符串转换为数组,如 here on Whosebug 所述,或者如果您只想通过调用 $request->getContent() 返回整个 xml,那么你可以使用 xml 字符串作为数组,如 $response = $this->call('POST', '/api', [$xml]);

The signature of the call is:

   /**
     * Call the given URI and return the Response.
     *
     * @param  string  $method
     * @param  string  $uri
     * @param  array  $parameters
     * @param  array  $cookies
     * @param  array  $files
     * @param  array  $server
     * @param  string|null  $content
     * @return \Illuminate\Foundation\Testing\TestResponse
     */
    public function call($method, $uri, $parameters = [], $cookies = [], $files = [], $server = [], $content = null)

因此,您可以将 xml 作为最后一个参数传递:

$this->call('POST', '/api', [], [], [], [], $xml);