我尝试在 PhpUnit 的 WebTestCase 中发送原始数据,但它不起作用:
$jsonEvent = '{
"type": "invoice.payment_succeeded",
}';
$this->client->request(
'POST',
'/api/v1/stripe/webhook',
[],
[],
['CONTENT_TYPE' => 'application/json'],
$jsonEvent
);
我尝试获取这样的数据:
$input = file_get_contents("php://input");
var_dump($input);
但是
$input
是空的不确定,但也许不可能在 webtestcase 中获得这样的内容输入?
提前致谢。
最佳答案
我假设您正在使用 Symfony s WebTestCase .如果是这种情况,那么首先,从您的 json 中删除无效的逗号:
$jsonEvent = '{
"type": "invoice.payment_succeeded"
}';
其次,我假设您以这种方式创建客户端:
$this->client = static::createClient();
如果是这种情况,则问题是 请求 方法是 不做 http 请求 ,但它正在查找 Controller 和操作(内部使用 "Symfony\Component\HttpKernel\Controller\ControllerResolver" )并调用它传递参数、内容和在 中设置的标题请求方法 到适当的 symfony 对象。
这就是你不能使用的原因:
$input = file_get_contents("php://input");
因为您确实在运行相同的脚本。
要在您的操作中获取帖子正文,一种方法是:
public function myAction(Request $request)
{
$input = $request->getContent();
引用资料和示例
关于phpunit - WebTestCase Phpunit 发送原始数据不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48888742/