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

cyt*_*nny 3 php phpunit unit-testing laravel lumen

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

目前,为了模拟 JSON 输入,从官方文档中,我可以:

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

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

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

有没有办法将原始正文内容插入到发布请求中?(或者至少是一个带有XML输入的请求?这是一个从微信接收回调的服务器,我们别无选择,只能按照微信想要使用的方式使用XML。)

Rem*_*mul 5

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

如果您想向应用程序发出自定义 HTTP 请求并获取完整的 Illuminate\Http\Response 对象,您可以使用 call 方法:

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

    $this->assertEquals(200, $response->status());
}
Run Code Online (Sandbox Code Playgroud)

下面是调用方法:

public function call($method, $uri, $parameters = [], $cookies = [], $files = [], $server = [], $content = null)
Run Code Online (Sandbox Code Playgroud)

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

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

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

use Illuminate\Http\Request;

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