lundi 14 décembre 2015

Testing Laravel Socialite with phpunit

I am pretty new to testing and, in particular, mocks. I have a controller (albeit incomplete)

class RegistrationController extends Controller
{

public function __construct(Identity $identity, User $user){
    $this->identity = $identity;
    $this->user = $user;
}

/**
 * Redirect the user to the social provider ie facebook/google etc
 *
 * @param string $provider
 * @return \Illuminate\Http\RedirectResponse
 */
public function redirectToSocialProvider($provider)
{
    // Check to see if the provider is supported, if not
    // redirect the user back to where they came from
    if(!array_key_exists($provider, Config::get('social'))) return redirect('/');

    return Socialite::driver($provider)->fields([
            'first_name',
            'last_name',
            'email',
            'gender',
            'birthday',
        ])->redirect();
}

/**
 * Handle the call back from social providers
 *
 * @param string $provider
 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|void
 */
public function handleSocialProviderCallback($provider)
{
    // Check to see if the provider is supported, if not
    // abort the signup
    if(!array_key_exists($provider, Config::get('social'))) return redirect('/');

    $method = 'handle'.studly_case($provider . "Callback");

    if (method_exists($this, $method)) {
        return $this->{$method}();
    } else {
        return $this->handleMissingCallbackMethod();
    }

}

/**
 * Handle Facebook callback
 */
public function handleFacebookCallback()
{
    // Get the user information from facebook
    $providerUser = Socialite::driver('facebook')->fields([
            'first_name',
            'last_name',
            'email',
            'gender',
            'birthday'
        ])->scopes([
            'email', 'user_birthday'
        ])->user();

}

/**
 * Handle any missing call back methods
 */
private function handleMissingCallbackMethod()
{
    //
}

}

with routes

Route::get('auth/register/{provider}', 'RegistrationController@redirectToSocialProvider'); Route::get('auth/register/{provider}/callback', ['as' => 'users.provider.callback', 'uses' => 'RegistrationController@handleSocialProviderCallback']);

How would I test the callback route with phpunit to ensure that the user() method is being called on laravel socialite in the "handleFacebookCallback" method for example.

Aucun commentaire:

Enregistrer un commentaire