Skip to content

Add actingAsGuest method for clearing actingAs calls #56517

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

Merged
merged 1 commit into from
Aug 3, 2025

Conversation

fritz-c
Copy link
Contributor

@fritz-c fritz-c commented Aug 1, 2025

This PR adds an actingAsGuest method to be a complement to the actingAs method in tests. When called, it makes the default or specified guard forget the user that was set to it.

My particular use case was in a test that simulated a webhook call coming in after a user request. While the initial user request would be authenticated, I wanted to be sure the webhook request did not have any user associated with it, as it would not in reality.

// Simulate initial user request
$this->actingAs($myUser)->post(route('buy-thing'));

// Simulate webhook
$this->actingAsGuest()->post(route('handle-webhook'), ['payload' => 'foo']);

// Simulating another step from the user's perspective
$this->actingAs($myUser)->get(route('after-purchase-redirect'));

@fritz-c
Copy link
Contributor Author

fritz-c commented Aug 1, 2025

In my research leading up to settling on this enhancement proposal I found a couple other examples of people needing this functionality:

https://stackoverflow.com/questions/60874682/laravel-actingas-guest
https://laracasts.com/discuss/channels/laravel/how-clear-actingas-user-in-testcase

@taylorotwell taylorotwell merged commit 9bab563 into laravel:12.x Aug 3, 2025
62 checks passed
@MrPunyapal
Copy link
Contributor

Awesome

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.

3 participants