[5.8] Assert that the session contains a given piece of data using a closure#28837
Conversation
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.
|
What are you trying to test in your application that requires this? Sessions generally only contain simple values? |
|
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 Example A: As a 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);
}); |
|
Thank you for merging this! Do you have any better suggestions than putting eloquent models in the session for such cases? |
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.