Skip to content

Instantly share code, notes, and snippets.

@MolloKhan
Created April 9, 2018 15:12
Show Gist options
  • Save MolloKhan/dc5fd127e96ed345caacb945e7f55d45 to your computer and use it in GitHub Desktop.
Save MolloKhan/dc5fd127e96ed345caacb945e7f55d45 to your computer and use it in GitHub Desktop.
Fake HWIOauth resource owner
class FakeGenericResourceOwner extends GenericOAuth2ResourceOwner
{
/**
* Paths needed by UserResponse - which are used by our UserProvider
*/
protected $paths = array(
'identifier' => 'id',
'nickname' => 'login',
'realname' => 'name',
'email' => 'email',
'profilepicture' => 'avatar_url',
'display' => null
);
/**
* This method creates the UserResponse and set its data
*
* Overridden so we can pass everything we need and avoid http calls
*/
public function getUserInformation(array $accessToken, array $extraParameters = array())
{
//Email and ServiceID are injected in our tests calls separated by ":" (code parameter e.g. url?code=email:serviceID)
//so we can just explode access_token parameter to retrieve them
list($email, $id) = explode(':', $accessToken['access_token']);
$content = array(
'id' => $id,
'login' => 'fakeLogin',
'name' => 'fakeName',
'email' => $email,
'avatar_url' => 'https://www.fake.url',
);
$response = $this->getUserResponse();
$response->setData($this->getResponse($content)->getContent());
$response->setResourceOwner($this);
$response->setOAuthToken(new OAuthToken($accessToken));
return $response;
}
protected function doGetTokenRequest($url, array $parameters = array())
{
//$parameters['code'] is our URL query parameter with the email and serviceId
return $this->getResponse(array('access_token' => $parameters['code']));
}
protected function httpRequest($url, $content = null, $headers = array(), $method = null)
{
// If you end up here, you need to find which method is calling this and override it
// Just like doGetTokenRequest method
// or implement a fake logic
throw new \Exception("You should not be doing http requests while in test mode");
}
/**
* Some ResourceOwners might complain about missing default/required fields,
* just check for them and add them to the list (You can do "login/check-{service}?code=email:id" to test it)
*
* If you have an exception while rendering a template (specially while using "hwi_oauth_login_url" function)
* just add the missing fields to the resolver defaults
*
* @param OptionsResolver $resolver
*/
protected function configureOptions(OptionsResolver $resolver)
{
parent::configureOptions($resolver);
$resolver->setDefaults(array(
'authorization_url' => 'https://fake.com/login/oauth/authorize',
'access_token_url' => 'https://fake.com/login/oauth/access_token',
'infos_url' => 'https://api.fake.com/user',
'client_id' => null,
'client_secret' => null,
'display' => null,
));
}
/**
* A helper method to construct fake responses.
*
* @param array $content
*
* @return Response
*/
private function getResponse(array $content)
{
$response = new Response();
$response->addHeader('Content-Type: application/json');
$response->setContent(json_encode($content));
return $response;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment