Skip to content

Commit

Permalink
Merge pull request #340 from geodeveloper/patch-1
Browse files Browse the repository at this point in the history
Create Yahoo.php
  • Loading branch information
elliotchance committed Sep 24, 2015
2 parents 916cff8 + aadb013 commit 74a5a62
Show file tree
Hide file tree
Showing 2 changed files with 292 additions and 0 deletions.
78 changes: 78 additions & 0 deletions src/OAuth/OAuth2/Service/Yahoo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace OAuth\OAuth2\Service;

use OAuth\OAuth2\Token\StdOAuth2Token;
use OAuth\Common\Http\Exception\TokenResponseException;
use OAuth\Common\Http\Uri\Uri;

class Yahoo extends AbstractService
{

/**
* {@inheritdoc}
*/
public function getAuthorizationEndpoint()
{
return new Uri('https://api.login.yahoo.com/oauth2/request_auth');
}

/**
* {@inheritdoc}
*/
public function getAccessTokenEndpoint()
{
return new Uri('https://api.login.yahoo.com/oauth2/get_token');
}

/**
* {@inheritdoc}
*/
protected function getAuthorizationMethod()
{
return static::AUTHORIZATION_METHOD_HEADER_BEARER;
}

/**
* {@inheritdoc}
*/
protected function parseAccessTokenResponse($responseBody)
{
$data = json_decode($responseBody, true);

if (null === $data || !is_array($data))
{
throw new TokenResponseException('Unable to parse response.');
} elseif (isset($data['error']))
{
throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
}

$token = new StdOAuth2Token();
$token->setAccessToken($data['access_token']);
$token->setLifetime($data['expires_in']);

if (isset($data['refresh_token']))
{
$token->setRefreshToken($data['refresh_token']);
unset($data['refresh_token']);
}

unset($data['access_token']);
unset($data['expires_in']);

$token->setExtraParams($data);

return $token;
}

/**
* {@inheritdoc}
*/
protected function getExtraOAuthHeaders()
{
$encodedCredentials = base64_encode($this->credentials->getConsumerId() . ':' . $this->credentials->getConsumerSecret());
return array('Authorization' => 'Basic ' . $encodedCredentials);
}

}
214 changes: 214 additions & 0 deletions tests/Unit/OAuth2/Service/YahooTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
<?php

namespace OAuthTest\Unit\OAuth2\Service;

use OAuth\OAuth2\Service\Yahoo;
use OAuth\Common\Token\TokenInterface;

class YahooTest extends \PHPUnit_Framework_TestCase
{
/**
* @covers OAuth\OAuth2\Service\Yahoo::__construct
*/
public function testConstructCorrectInterfaceWithoutCustomUri()
{
$service = new Yahoo(
$this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
$this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
$this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
);

$this->assertInstanceOf('\\OAuth\\OAuth2\\Service\\ServiceInterface', $service);
}

/**
* @covers OAuth\OAuth2\Service\Yahoo::__construct
*/
public function testConstructCorrectInstanceWithoutCustomUri()
{
$service = new Yahoo(
$this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
$this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
$this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
);

$this->assertInstanceOf('\\OAuth\\OAuth2\\Service\\AbstractService', $service);
}

/**
* @covers OAuth\OAuth2\Service\Yahoo::__construct
*/
public function testConstructCorrectInstanceWithCustomUri()
{
$service = new Yahoo(
$this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
$this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
$this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
array(),
$this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface')
);

$this->assertInstanceOf('\\OAuth\\OAuth2\\Service\\AbstractService', $service);
}

/**
* @covers OAuth\OAuth2\Service\Yahoo::__construct
* @covers OAuth\OAuth2\Service\Yahoo::getAuthorizationEndpoint
*/
public function testGetAuthorizationEndpoint()
{
$service = new Yahoo(
$this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
$this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
$this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
);

$this->assertSame('https://api.login.yahoo.com/oauth2/request_auth', $service->getAuthorizationEndpoint()->getAbsoluteUri());
}

/**
* @covers OAuth\OAuth2\Service\Yahoo::__construct
* @covers OAuth\OAuth2\Service\Yahoo::getAccessTokenEndpoint
*/
public function testGetAccessTokenEndpoint()
{
$service = new Yahoo(
$this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
$this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
$this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
);

$this->assertSame('https://api.login.yahoo.com/oauth2/get_token', $service->getAccessTokenEndpoint()->getAbsoluteUri());
}

/**
* @covers OAuth\OAuth2\Service\Yahoo::__construct
* @covers OAuth\OAuth2\Service\Yahoo::getAuthorizationMethod
*/
public function testGetAuthorizationMethod()
{
$client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
$client->expects($this->once())->method('retrieveResponse')->will($this->returnArgument(2));

$token = $this->getMock('\\OAuth\\OAuth2\\Token\\TokenInterface');
$token->expects($this->once())->method('getEndOfLife')->will($this->returnValue(TokenInterface::EOL_NEVER_EXPIRES));
$token->expects($this->once())->method('getAccessToken')->will($this->returnValue('foo'));

$storage = $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface');
$storage->expects($this->once())->method('retrieveAccessToken')->will($this->returnValue($token));

$service = new Yahoo(
$this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
$client,
$storage
);

$headers = $service->request('https://pieterhordijk.com/my/awesome/path');

$this->assertTrue(array_key_exists('Authorization', $headers));
$this->assertTrue(in_array('Bearer foo', $headers, true));
}

/**
* @covers OAuth\OAuth2\Service\Yahoo::__construct
* @covers OAuth\OAuth2\Service\Yahoo::parseAccessTokenResponse
*/
public function testParseAccessTokenResponseThrowsExceptionOnNulledResponse()
{
$client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
$client->expects($this->once())->method('retrieveResponse')->will($this->returnValue(null));

$service = new Yahoo(
$this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
$client,
$this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
);

$this->setExpectedException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException');

$service->requestAccessToken('foo');
}

/**
* @covers OAuth\OAuth2\Service\Yahoo::__construct
* @covers OAuth\OAuth2\Service\Yahoo::parseAccessTokenResponse
*/
public function testParseAccessTokenResponseThrowsExceptionOnError()
{
$client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
$client->expects($this->once())->method('retrieveResponse')->will($this->returnValue('error=some_error'));

$service = new Yahoo(
$this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
$client,
$this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
);

$this->setExpectedException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException');

$service->requestAccessToken('foo');
}

/**
* @covers OAuth\OAuth2\Service\Yahoo::__construct
* @covers OAuth\OAuth2\Service\Yahoo::parseAccessTokenResponse
*/
public function testParseAccessTokenResponseValidWithoutRefreshToken()
{
$client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
$client->expects($this->once())->method('retrieveResponse')->will($this->returnValue('{"access_token":"foo","expires_in":"bar"}'));

$service = new Yahoo(
$this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
$client,
$this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
);

$this->assertInstanceOf('\\OAuth\\OAuth2\\Token\\StdOAuth2Token', $service->requestAccessToken('foo'));
}

/**
* @covers OAuth\OAuth2\Service\Yahoo::__construct
* @covers OAuth\OAuth2\Service\Yahoo::parseAccessTokenResponse
*/
public function testParseAccessTokenResponseValidWithRefreshToken()
{
$client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
$client->expects($this->once())->method('retrieveResponse')->will($this->returnValue('{"access_token":"foo","expires_in":"bar","refresh_token":"baz"}'));

$service = new Yahoo(
$this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
$client,
$this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
);

$this->assertInstanceOf('\\OAuth\\OAuth2\\Token\\StdOAuth2Token', $service->requestAccessToken('foo'));
}

/**
* @covers OAuth\OAuth2\Service\Yahoo::__construct
* @covers OAuth\OAuth2\Service\Yahoo::getExtraOAuthHeaders
*/
public function testGetExtraOAuthHeaders()
{
$client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
$client->expects($this->once())->method('retrieveResponse')->will($this->returnCallback(function($uri, $params, $extraHeaders) {
\PHPUnit_Framework_Assert::assertTrue(array_key_exists('Authorization', $extraHeaders));
\PHPUnit_Framework_Assert::assertSame('Basic ' . base64_encode('foo:bar'), $extraHeaders['Authorization']);

return '{"access_token":"foo","expires_in":"bar"}';
}));

$credentials = $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface');
$credentials->expects($this->any())->method('getConsumerId')->will($this->returnValue('foo'));
$credentials->expects($this->any())->method('getConsumerSecret')->will($this->returnValue('bar'));

$service = new Yahoo(
$credentials,
$client,
$this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
);

$this->assertInstanceOf('\\OAuth\\OAuth2\\Token\\StdOAuth2Token', $service->requestAccessToken('foo'));
}
}

0 comments on commit 74a5a62

Please sign in to comment.