Skip to content

[5.8] Assert that the session contains a given piece of data using a closure#28837

Merged
taylorotwell merged 1 commit into
laravel:5.8from
sebdesign:5.8
Jun 14, 2019
Merged

[5.8] Assert that the session contains a given piece of data using a closure#28837
taylorotwell merged 1 commit into
laravel:5.8from
sebdesign:5.8

Conversation

@sebdesign

Copy link
Copy Markdown
Contributor

Just like $response->assertViewHas() accepts a closure as a second parameter which is called to evaluate the contents of the view data, $response->assertSessionHas() could implement a similar behavior.

Just like `$response->assertViewHas()` accepts a closure as a second parameter which is called to evaluate the contents of the view data, `$response->assertSessionHas()` could implement a similar behavior.
@GrahamCampbell GrahamCampbell changed the title Assert that the session contains a given piece of data using a closure [5.8] Assert that the session contains a given piece of data using a closure Jun 14, 2019
@taylorotwell

Copy link
Copy Markdown
Member

What are you trying to test in your application that requires this? Sessions generally only contain simple values?

@sebdesign

sebdesign commented Jun 14, 2019

Copy link
Copy Markdown
Contributor Author

Good question. I am actually storing an eloquent model in the session, so I can later retrieve it in a controller that uses Socialite. This model then will be associated with the Socialite user.

The reason I'm not storing only the id of the model is that there are multiple models that can be associated with Socialite users. E.g. App\User, App\Team. I feel this is not the best way to achieve this, but I haven't found an elegant way for this use case:

Example A: As a User I want to connect my personal Github account for myself.
Example B: As a User I want to connect my company's Github account for my Team.

In my tests I want to assert that the model in the session is the one I expect, so would like to do something like this:

// Before
$response->assertSessionHas('authenticatable');
$this->assertTrue($user->is(session('authenticatable')));

// After
$response->assertSessionHas('authenticatable', function ($authenticatable) use ($user) {
    return $user->is($authenticatable);
});

@taylorotwell taylorotwell merged commit 1fb67cc into laravel:5.8 Jun 14, 2019
@sebdesign

Copy link
Copy Markdown
Contributor Author

Thank you for merging this! Do you have any better suggestions than putting eloquent models in the session for such cases?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants