PHPUnit:如何将原始数据提交到 post 请求链接以在 Lumen 中进行测试?

PHPUnit: how to submit raw data to post request linking for testing in Lumen?

我使用的是 Lumen 自带的默认 PHPUnit。虽然我能够创建对 link 的模拟 post 调用,但我无法找到向其提供原始数据的方法。

目前,要从 official document 模拟 JSON 输入,我可以:

     $this->json('POST', '/user', ['name' => 'Sally'])
         ->seeJson([
            'created' => true,
         ]);

或者如果我想要简单的表单输入,我可以:

    $this->post('/user', ['name' => 'Sally'])
         ->seeJsonEquals([
            'created' => true,
         ]);

有什么方法可以将原始正文内容插入 post 请求? (或者至少是一个带有 XML 输入的请求?这是一个接收微信回调的服务器,我们别无选择,只能被迫使用 XML 作为微信想要使用的。)

documentation 中所述,如果您想创建自定义 HTTP 请求,您可以使用 call 方法:

If you would like to make a custom HTTP request into your application and get the full Illuminate\Http\Response object, you may use the call method:

public function testApplication()
{
    $response = $this->call('GET', '/');

    $this->assertEquals(200, $response->status());
}

这里是 call 方法:

public function call($method, $uri, $parameters = [], $cookies = [], $files = [], $server = [], $content = null)

所以在你的情况下它会是这样的:

$this->call('POST', '/user', [], [], [], ['Content-Type' => 'text/xml; charset=UTF8'], $xml);

要访问控制器中的数据,您可以使用以下命令:

use Illuminate\Http\Request;

public function store(Request $request)
{
    $xml = $request->getContent();
    // Or you can use the global request helper
    $xml = request()->getContent();
}