我可以使用Laravel的`call`方法在单元测试中发送原始JSON数据吗?

 四木子1982 发布于 2022-12-15 14:27

我试图对我的Stripe webhook处理程序的实现进行单元测试.条带webhook数据作为POST请求正文中的原始JSON在线上传播,因此我捕获并解码数据:

public function store()
{
    $input = @file_get_contents("php://input");
    $request = json_decode($input);
    return Json::encode($request);
}

我正在尝试对此代码进行单元测试,但我无法弄清楚如何在单元测试中发送原始JSON数据,以便我可以使用该file_get_contents("php:input//")函数检索它.这是我尝试过的(使用PHPUnit):

protected $testRoute = 'api/stripe/webhook';

protected $validWebhookJson = <<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 go?
}

我也尝试了,curl但这会产生一个外部请求,从单元测试的角度来看,这对我没有意义.如何使用我的store方法拾取的正文中的原始JSON数据来模拟POST请求?

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有