【问题标题】:WebTestCase Phpunit send raw data not workingWebTestCase Phpunit 发送原始数据不起作用
【发布时间】:2018-07-31 00:29:52
【问题描述】:

我尝试在我的 WebTestCase 中的 PhpUnit 中发送原始数据,但它不起作用:

$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】:

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

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

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

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

    如果是这种情况,那么问题是 request 方法是 不执行 http 请求,但它正在寻找控制器和操作(内部使用 "Symfony\Component\HttpKernel\Controller\ControllerResolver") 并调用它,将 request 方法 中设置的参数、内容和标头传递给适当的 symfony 对象。

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

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

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

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

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

    参考和示例

    1. Symfony working with client
    2. Symfony client error

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-23
      • 2017-03-24
      • 2013-04-25
      • 2019-04-17
      • 2022-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多