Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Jun 14, 2019

Conversation

sebdesign
Copy link
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
Member

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

@sebdesign
Copy link
Contributor Author

sebdesign commented Jun 14, 2019

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
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