本文介绍了我可以测试社交名流重定向和回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何测试Laravel社交名流

这在laravel 5.2上有效,但是在5.6中,我没有访问方法和get方法,没有重定向.

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

推荐答案

对于回调,在 https://laracasts.com/discuss/channels/testing/testing-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在setUp函数中定义为

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

这是使用的两条路线

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.

这篇关于我可以测试社交名流重定向和回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-13 15:48