Skip to content

Conversation

@stevebauman
Copy link
Contributor

@stevebauman stevebauman commented Dec 8, 2025

Description

This PR introduces a testing Fake system, making it easy to test OAuth authentication flows without hitting real OAuth providers, and without having to use Mockery.

Usage

Consider the Socialite controller below:

class SocialiteController extends Controller
{
    public function redirect(string $provider)
    {
        return Socialite::driver($provider)->redirect();
    }

    public function callback(string $provider)
    {
        $socialiteUser = Socialite::driver($provider)->user();

        $user = User::firstOrCreate(
            ['email' => $socialiteUser->getEmail()],
            [
                'name' => $socialiteUser->getName(),
                "{$provider}_id" => $socialiteUser->getId(),
            ]
        );

        Auth::login($user);

        return redirect('/dashboard');
    }
}

Then, in our tests, we can utilize Socialite::fake($provider, $user) to initialize the fake and perform assertions:

use Laravel\Socialite\Socialite;
use Laravel\Socialite\Two\User;

test('user is redirected to github', function () {
    Socialite::fake('github');

    get('/auth/github/redirect')->assertRedirect('https://socialite.fake/github/authorize');
});

test('user can login with github', function () {
    $user = (new User)->map([
        'id' => 'github-123',
        'name' => 'John Doe',
        'email' => '[email protected]',
    ]);

    Socialite::fake('github', $fakeUser);

    get('/auth/github/callback')->assertRedirect('/dashboard');
    
    assertDatabaseHas(User::class, [
        'email' => $user->getEmail(),
        'name' => $user->getName(),
        'github_id' => $user->getId(),
    ]);
});

Let me know your thoughts!

@taylorotwell taylorotwell merged commit 1d19358 into laravel:5.x Dec 9, 2025
27 checks passed
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