我可以测试社交名流重定向和回调吗?



如何测试Laravel Socialite

这在Laravel 5.2上起作用,但是在5.6中,我没有访问方法和方法,没有重定向。

用于回调,在https://laracasts.com/discuss/channels/testing/testing-socialite上有一个很好的例子,该示例指出了如何测试Laravel Socialite。

对于重定向位,我使用了以下功能

protected function guest_users_can_try_to_login_with_social_platform_login(string $provider)
{
    $providerMock = Mockery::mock('LaravelSocialiteContractsProvider');
    $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在设置函数中定义为

$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'
];

这是使用的两条路线

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

任何问题请告诉我。

最新更新