简体   繁体   中英

Trying to mock Laravel Socialite Google Login for integration testing

I am trying to mock Laravel Socialite to test Google oAUTH Login using the guide here .

use Laravel\Socialite\Facades\Socialite;
use Laravel\Socialite\Two\GoogleProvider;
use Laravel\Socialite\Two\User as SocialUser;

public function mock_socialite ($email = 'foo@gmail.com', $token = 'foo', $id = 1)
    {
        // create a mock user
        $socialiteUser = $this->createMock(SocialUser::class);
        $socialiteUser->token = $token;
        $socialiteUser->id = $id;
        $socialiteUser->email = $email;

        // create a mock provider of the user
        $provider = $this->createMock(GoogleProvider::class);
        $provider->expects($this->any())
            ->method('user')
            ->willReturn($socialiteUser);

        // create a mock Socialite instance
        $stub = $this->createMock(Socialite::class);
        $stub->expects($this->any())
            ->method('driver')
            //->with('google')
            ->willReturn($provider);

        // Replace Socialite Instance with our mock
        $this->app->instance(Socialite::class, $stub);
    }

However, I am getting the below error:

Trying to configure method "driver" which cannot be configured 
because it does not exist, has not been specified, is final, or is static

I checked and found that driver() method does exist in Illuminate\\Support\\Manager (from where Socialite is extended) and this method is a public method. Not sure why am I getting this error.

Socialite is a facade and often their integration is far away from how you would use it, therefor it does not have the driver method. You are including that facade to mock it, this is not the approach he has in the guide. So replacing the facade with the use statement he uses, would probably solved your problems.

use Laravel\Socialite\Contracts\Factory as Socialite;

Instead of.

use Laravel\Socialite\Facades\Socialite;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM