1

How to Test Laravel Socialite

This works on laravel 5.2, but in 5.6 I dont have visit method and method get, doesnt redirect.

1 Answers1

1

For Callback there is a good example at https://laracasts.com/discuss/channels/testing/testing-socialite which points to How to Test Laravel Socialite.

For the redirect bit I have used the following function

protected function guest_users_can_try_to_login_with_social_platform_login(string $provider)
{
    $providerMock = \Mockery::mock('Laravel\Socialite\Contracts\Provider');

    $providerMock->shouldReceive('redirect')->andReturn(new RedirectResponse($this->socialLoginRedirects[$provider]));

    Socialite::shouldReceive('driver')->with($provider)->andReturn($providerMock);

    //Check that the user is redirected to the Social Platform Login Page
    $loginResponse = $this->call('GET', route('social-login', ['provider' => $provider]));

    $loginResponse->assertStatus(302);

    $redirectLocation = $loginResponse->headers->get('Location');

    $this->assertContains(
        $this->socialLoginRedirects[$provider],
        $redirectLocation,
        sprintf(
            'The Social Login Redirect does not match the expected value for the provider %s. Expected to contain %s but got %s',
            $provider,
            $this->socialLoginRedirects[$provider],
            $redirectLocation
        )
    );
}

$this->socialLognRedirects is defined in the setUp function as

$this->socialLoginRedirects = [
        'facebook' => 'https://www.facebook.com/v3.0/dialog/oauth',
        'google'   => 'https://accounts.google.com/o/oauth2/auth',
        'github'   => 'https://github.com/login/oauth/authorize',
        'twitter'  => 'https://api.twitter.com/oauth/authenticate'
];

Here are the two routes used

Route::get('login/{provider}', 'Auth\LoginController@redirectToProvider')->middleware('guest')->name('social-login');
Route::get('login/{provider}/callback', 'Auth\LoginController@handleProviderCallback')->middleware('guest')->name('social-login-callback');

Any question please let me know.