Skip to content

Instantly share code, notes, and snippets.

@xiCO2k
Created October 14, 2022 12:41
Show Gist options
  • Save xiCO2k/25283075b005a1f4166580f8eeca594b to your computer and use it in GitHub Desktop.
Save xiCO2k/25283075b005a1f4166580f8eeca594b to your computer and use it in GitHub Desktop.
Testing Socialite
<?php
it('redirects to the correct Google sign in url', function () {
$driver = Mockery::mock('Laravel\Socialite\Two\GoogleProvider');
$driver->shouldReceive('redirect')
->andReturn(new RedirectResponse('https://redirect.url'));
Socialite::shouldReceive('driver')->andReturn($driver);
$this->get(route('oauth.redirect', 'google'))
->assertRedirect('https://redirect.url');
});
it('signs in with Google', function () {
$user = createUser();
$socialiteUser = Mockery::mock('Laravel\Socialite\Two\User');
$socialiteUser
->shouldReceive('getId')->andReturn($googleId = '12345654321345')
->shouldReceive('getName')->andReturn($user->name)
->shouldReceive('getEmail')->andReturn($user->email)
->shouldReceive('getAvatar')->andReturn($avatarUrl = 'https://en.gravatar.com/userimage');
Socialite::shouldReceive('driver->user')->andReturn($socialiteUser);
$this->get(route('oauth.callback', 'google'))->assertRedirect(route('dashboard'));
expect(auth()->check())->toBeTrue();
expect($user->refresh())
->avatar_path->toBe($avatarUrl)
->oauth_provider->toBe('google')
->oauth_id->toBe($googleId)
->oauth_data->not->toBeNull();
});
it('does not sign-in with Google if the user does not exist', function () {
$abstractUser = Mockery::mock('Laravel\Socialite\Two\User');
$abstractUser->shouldReceive('getEmail')->andReturn('john@doe.com');
Socialite::shouldReceive('driver->user')->andReturn($abstractUser);
$this
->get(route('oauth.callback', 'google'))
->assertRedirect(route('login'))
->assertSessionHasErrors('email');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment