Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebTestCase Phpunit send raw data not working

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.

like image 461
psylo66 Avatar asked Feb 20 '18 15:02

psylo66


1 Answers

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

  1. Symfony working with client
  2. Symfony client error
like image 184
Jannes Botis Avatar answered Oct 03 '22 09:10

Jannes Botis