我可以测试社交网站重定向和回调

时间:2018-04-24 00:04:40

标签: laravel laravel-5.6

How to Test Laravel Socialite

这适用于laravel 5.2,但在5.6中我没有访问方法和方法获取,不重定向。

1 个答案:

答案 0 :(得分:0)

对于回调,在https://laracasts.com/discuss/channels/testing/testing-socialite有一个很好的例子,它指向How to Test Laravel Socialite

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

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在setUp函数中定义为

$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}', 'Auth\LoginController@redirectToProvider')->middleware('guest')->name('social-login');
Route::get('login/{provider}/callback', 'Auth\LoginController@handleProviderCallback')->middleware('guest')->name('social-login-callback');

任何问题,请让我知道。