WebTestCase Phpunit send raw data not working
Asked Answered
F

1

8

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.

Filial answered 20/2, 2018 at 15:30 Comment(0)
T
4

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
Trail answered 24/2, 2018 at 23:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.