Search code examples
phplaravelphpunitlaravel-testing

How to get Response variables in a Laravel PHPUnit test?


I am testing a controller method and I am accessing a route in a test.

Then I would like to make sure that the correct model was returned in the view and was loaded with all of the correct relationships.

I know that I can do this:

$this->assertViewHas("content");

But can how can I verify that the content model that was returned into the view has the correct, for example, category? i.e. how can I get the content model object and then do something like

$this->assertEquals($content->category->name, "category 1");

?


Solution

  • You can get your content from the response like this:

    $content = $response->getOriginalContent()->getData()['content'];
    

    getData() returns the data sent to the view as an array.