slimphp / Slim-Skeleton

Slim Framework 4 Skeleton Application
http://www.slimframework.com
MIT License
1.59k stars 479 forks source link

Testing POST action #152

Closed antilef closed 4 years ago

antilef commented 4 years ago

This is a question, not an issue. How i can testing POST routes action

i can see that test GET routes is easy but i can't test other method

i try to do this

        $app = $this->getAppInstance();
        $request = $this->createRequest('POST','api/containers/1/1');
        $request = $request->withParsedBody(["param1"=>"param1value"]);
        $response = $app->handle($request)

but i receive this exception


Slim\Exception\HttpBadRequestException : Malformed JSON input.
 /home/antilef/uach/hidroponicaApi/src/Application/Actions/Action.php:89
 /home/antilef/uach/hidroponicaApi/src/Application/Actions/Container/CreateContainerRailAction.php:19
 /home/antilef/uach/hidroponicaApi/src/Application/Actions/Action.php:61
 /home/antilef/uach/hidroponicaApi/vendor/slim/slim/Slim/Handlers/Strategies/RequestResponse.php:43
 /home/antilef/uach/hidroponicaApi/vendor/slim/slim/Slim/Routing/Route.php:381
 /home/antilef/uach/hidroponicaApi/vendor/slim/slim/Slim/MiddlewareDispatcher.php:81
 /home/antilef/uach/hidroponicaApi/vendor/slim/slim/Slim/MiddlewareDispatcher.php:81
 /home/antilef/uach/hidroponicaApi/vendor/slim/slim/Slim/Routing/Route.php:341
 /home/antilef/uach/hidroponicaApi/vendor/slim/slim/Slim/Routing/RouteRunner.php:84
 /home/antilef/uach/hidroponicaApi/src/Application/Middleware/SessionMiddleware.php:23
 /home/antilef/uach/hidroponicaApi/vendor/slim/slim/Slim/MiddlewareDispatcher.php:188
 /home/antilef/uach/hidroponicaApi/vendor/tuupola/cors-middleware/src/CorsMiddleware.php:113
 /home/antilef/uach/hidroponicaApi/vendor/slim/slim/Slim/MiddlewareDispatcher.php:140
 /home/antilef/uach/hidroponicaApi/vendor/slim/slim/Slim/MiddlewareDispatcher.php:81
 /home/antilef/uach/hidroponicaApi/vendor/slim/slim/Slim/App.php:211
 /home/antilef/uach/hidroponicaApi/tests/Application/Actions/Container/CreateContainerActionTest.php:16

Thank You

l0gicgate commented 4 years ago

If your action is using Action::getFormData() from within your action to parse form data: https://github.com/slimphp/Slim-Skeleton/blob/master/src/Application/Actions/Action.php#L75-L84

You will need to modify that method to source its input from the request body instead of directly from php://input:

protected function getFormData()
{
    return $this->request->getParsedBody();
}