user3494047
user3494047

Reputation: 1693

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");

?

Upvotes: 5

Views: 7073

Answers (4)

Ali Obeid
Ali Obeid

Reputation: 141

You can use

$your_desired_data = $response->assertSee('var_tag');

and if it's an array of data you can access its data by:

$first_name = $your_desired_data['first_name'];

Upvotes: 1

Vito Meuli
Vito Meuli

Reputation: 132

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.

Upvotes: 5

Connor Leech
Connor Leech

Reputation: 18833

You can use the following to get the array that's passed to the view:

$response->original->getData()

This comes from Illuminate/Http/ResponseTrait (link to docs).

Upvotes: 3

Gabriel Caruso
Gabriel Caruso

Reputation: 869

Use assertSee():

$response->assertSee("category 1");

Upvotes: 3

Related Questions