我试图对我的Stripe webhook处理程序的实现进行单元测试.条带webhook数据作为POST请求正文中的原始JSON在线上传播,因此我捕获并解码数据:
public function store()
{
$input = @file_get_contents("php://input");
$request = json_decode($input);
return Json::encode($request);
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试对此代码进行单元测试,但我无法弄清楚如何在单元测试中发送原始JSON数据,以便我可以使用该file_get_contents("php:input//")函数检索它.这是我尝试过的(使用PHPUnit):
protected $testRoute = 'api/stripe/webhook';
protected $validWebhookJson = <<<EOT
{
"id": "ch_14qE2r49NugaZ1RWEgerzmUI",
"object": "charge",
// and a bunch of other fields too
}
EOT;
public function testWebhookDecdoesJsonIntoObject()
{
$response = $this->call('POST', $this->testRoute, $this->validWebhookJson); // fails because `$parameters` must be an array
$response = $this->call('POST', $this->testRoute, [], [], ['CONTENT_TYPE' => 'application/json'], $this->validWebhookJson);
dd($response->getData(true)); // array(0) {} BOOOO!!! Where for to my data …Run Code Online (Sandbox Code Playgroud)