我可以测试社会名流重定向和回调

问题描述 投票:1回答:1

How to Test Laravel Socialite

这适用于laravel 5.2,但在5.6我没有访问方法和手段得到,那并不重定向。

laravel laravel-5.6
1个回答
1
投票

回调有在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
        )
    );
}

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

有任何问题请让我知道。

© www.soinside.com 2019 - 2024. All rights reserved.