0

I'm trying to perform unit test for Laravel socialite function. There are 2 functions for redirect and callback.

There is one problem that I couldn't test successful was the callback function.

Here is how I test the redirect function

AuthController.php

/**
 * @return mixed
 */
public function loginWithGoogle () {
    return Socialite::driver('google')
        ->scopes([
            'https://www.googleapis.com/auth/userinfo.email'
        ])
        ->with([
            'access_type' => 'offline',
            'prompt' => 'consent select_account'
        ])
        ->redirect();
}

AuthTest.php

public function testGoogleLoginForwardRequest()
{
    $response = $this->getJson('/oauths/forward/google');
    $response->assertStatus(302);
}

For the callback function

AuthController.php

/**
 * @return RedirectResponse
 */
public function loginRedirectFromGoogle (): RedirectResponse
{
    $user = Socialite::driver('google')->stateless()->user();
    $acUser = User::where('email', $user->getEmail())->first();

    if(empty($acUser)){
        return redirect('/oauth/login?'.http_build_query([
            'force_external_oauth_response' => 'true',
            'external_oauth_response_error' => 'User account not register'
        ]));
    }

    Auth::login($acUser);

    OAuthProvider::updateOrCreate(
        [
            'provider_name' => 'google',
            'provider_id' => $user->id,
            'user_id' => $acUser->id
        ],
        [
            'encrypted_token' => Crypt::encryptString($user->token),
            'encrypted_refreshed_token' => Crypt::encryptString($user->refreshToken),
            'expires_at' => time() + $user->expiresIn // time and expiresIn are all in seconds
        ]
    );

    return redirect('/oauth/login?'.http_build_query([
            'force_external_oauth_response' => 'true',
            'external_oauth_response_error' => ''
        ]));
}

AuthTest.php

public function testGoogleLoginRedirectRequest()
{
    Mockery::getConfiguration()->allowMockingNonExistentMethods(false);

    $abstractUser = Mockery::mock('Laravel\Socialite\Two\User');

    $abstractUser
        ->shouldReceive('getId')
        ->andReturn(rand())
        ->shouldReceive('getName')
        ->andReturn($this->faker->name())
        ->shouldReceive('getEmail')
        ->andReturn($this->faker->name() . '@gmail.com')
        ->shouldReceive('getAvatar')
        ->andReturn('https://en.gravatar.com/userimage');
    Socialite::shouldReceive('driver')->with('google')->andReturn($abstractUser);

    $response = $this->getJson('/oauths/forward/google');
    $response->assertStatus(302);
}

For this function it returns error the following

Mockery\Exception\BadMethodCallException: Method Mockery_0_Laravel_Socialite_Two_User::scopes() does not exist on this mock object in C:\xampp\htdocs\ac-dev\achievement-console\vendor\mockery\mockery\library\Mockery\Loader\EvalLoader.php(34) : eval()'d code:928

For the callback function, I followed this example How to Test Laravel Socialite

0 Answers0