Testing for post requests (solved)

Hi Devs. I’m writing some tests for a Slim 4 based app.

I want to test an action who receives a POST request, but the function createRequest in TestCase.php can’t handle POST data. What is the right way to test POST actions?

You can add some POST form data like this:

$request = $this->createRequest('POST', '/path');
$request = $request->withParsedBody($data);
$request = $request->withHeader('Content-Type', 'application/x-www-form-urlencoded');
1 Like

Oh, I found a way. I’ll let this here for future support. :smiley:

$postData   = [
        'email'      => $user->getEmail(),
        'first_name' => $user->getFirstName(),
        'last_name'  => $user->getLastName(),
        'password'   => $faker->password(10).'A'
$request = $this->createRequest('POST', '/account/create');
$request = $request->withParsedBody($postData); 

First create you array of POST info, create the request and override or create a new variable with the return of $request->withParsedBody($postData) because this method will clone the current request object.

I wrote at the same time dude. Thank you! :smiley: A LOT