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

时间:2018-02-20 15:30:45

标签: phpunit file-get-contents raw-data

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

提前致谢。

1 个答案:

答案 0 :(得分:3)

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

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

其次,我假设您正在以这种方式创建客户端:

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

如果是这种情况,则问题是请求方法没有执行http请求,但它正在查找控制器和操作(内部使用&#34; Symfony \ Component \ HttpKernel \ Controller \ ControllerResolver&#34; )并调用它将请求方法中设置的参数,内容和标题传递给相应的symfony对象。< / p>

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

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

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

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

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

参考文献&amp;实例

  1. Symfony working with client
  2. Symfony client error