I try to send raw data in my WebTestCase in PhpUnit and it doesn't work :
$jsonEvent = '{
"type": "invoice.payment_succeeded",
}';
$this->client->request(
'POST',
'/api/v1/stripe/webhook',
[],
[],
['CONTENT_TYPE' => 'application/json'],
$jsonEvent
);
and i try to get data like this :
$input = file_get_contents("php://input");
var_dump($input);
But $input
is empty
Not sure but perhaps it's not possible to get content input like that in webtestcase ?
Thanks in advance.
I assume you are using Symfony s WebTestCase. If this is the case, then first of all, remove the invalid comma from your json:
$jsonEvent = '{
"type": "invoice.payment_succeeded"
}';
Second, I assume you are creating the client this way:
$this->client = static::createClient();
If this is the case, then the issue is the request method is not doing an http request, but it is finding the controller and action(internally using "Symfony\Component\HttpKernel\Controller\ControllerResolver") and calls it passing the parameters, content and headers set in request method to the appropriate symfony objects.
That is the reason you cannot use:
$input = file_get_contents("php://input");
as you are really running the same script.
To get the post body in your action, one way to do it is:
public function myAction(Request $request)
{
$input = $request->getContent();
References & examples
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With