WebTestCase Phpunit发送原始数据不起作用

问题描述 投票:8回答:1

我尝试在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中获得类似的内容输入?

提前致谢。

phpunit file-get-contents raw-data
1个回答
3
投票

我假设您正在使用Symfony的WebTestCase。如果是这种情况,那么首先,从json中删除无效的逗号:

$jsonEvent = '{
  "type": "invoice.payment_succeeded"
}';

其次,我假设你是这样创建客户端的:

$this->client = static::createClient();

如果是这种情况,那么问题是请求方法没有执行http请求,但它正在查找控制器和操作(内部使用“Symfony \ Component \ HttpKernel \ Controller \ ControllerResolver”)并调用它传递参数,在请求方法中为相应的symfony对象设置的内容和标头。

这就是你不能使用的原因:

$input = file_get_contents("php://input");

因为你真的在运行相同的脚本。

要在您的操作中获取帖子正文,一种方法是:

public function myAction(Request $request)
{
    $input = $request->getContent();

参考和例子

  1. Symfony working with client
  2. Symfony client error
© www.soinside.com 2019 - 2024. All rights reserved.