Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Setting the body on a Request object
  • I want to make unit tests (using phpunit) for my controllers, and I found that I can make requests internally using the \Fuel\Core\Request class, but it doesn't seem to support putting anything in its body (in my case, a json object). Have I missed something? Is using the Request object perhaps not the way to do unit testing? Is there another way?
  • Any request needs to return a Response object, this is true for both interactive use (your actions), REST calls or HMVC calls. This can be as simple as
    return \Response::forge(json_encode($array));
    

    If your controller extends Controller_REST, this will be done for you automatically in the parent after() method, you action methods need to return an array. If your controller extends Controller_Template, things are going to be a bit more complex as these always return views. But again the parent after() method will make sure a response object is returned. Once you have the Response object, you can access the body using $responseobject->body().
  • Thanks for your answer, but it seems you misunderstood me. Perhaps I didn't explain my problem clearly enough. My problem isn't returning a response with a body, but setting the body of the initial request object, so I can simulate someone doing a put request with a json object in the body. Or perhaps I am misunderstanding your answer?
  • Ah, ok. You want to simulate an ajax post with a json payload? That isn't supported yet. If you need this, please create a feature request on http://github.com/fuel/core/issues so it can be picked up.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion