How to Test Laravel Socialite

前端 未结 4 829
北恋
北恋 2020-12-31 15:14

I have an application that makes use of socialite, I want to create test for Github authentication, So I used Socialite Facade to mock call to the Socialite driver

4条回答
  •  盖世英雄少女心
    2020-12-31 15:57

    $provider = Mockery::mock('Laravel\Socialite\Contracts\Provider');
    $provider->shouldReceive('redirect')->andReturn('Redirected');
    $providerName = class_basename($provider);
    //Call your model factory here
    $socialAccount = factory('LearnCast\User')->create(['provider' => $providerName]);
    
    $abstractUser = Mockery::mock('Laravel\Socialite\Two\User');
    // Get the api user object here
    $abstractUser->shouldReceive('getId') 
                 ->andReturn($socialAccount->provider_user_id)
                 ->shouldReceive('getEmail')
                 ->andReturn(str_random(10).'@noemail.app')
                 ->shouldReceive('getNickname')
                 ->andReturn('Laztopaz')
                 ->shouldReceive('getAvatar')
                 ->andReturn('https://en.gravatar.com/userimage');
    
    $provider = Mockery::mock('Laravel\Socialite\Contracts\Provider');
    $provider->shouldReceive('user')->andReturn($abstractUser);
    
    Socialite::shouldReceive('driver')->with('facebook')->andReturn($provider);
    
    // After Oauth redirect back to the route
    $this->visit('/auth/facebook/callback')
    // See the page that the user login into
    ->seePageIs('/');
    

    Note: use the socialite package at the top of your class

    use Laravel\Socialite\Facades\Socialite;

    I had the same problem, but I was able to solve it using the technique above; @ceejayoz. I hope this helps.

提交回复
热议问题