Skip to content

Commit

Permalink
Merge 8d0a8cb into e18c99c
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromegamez committed Feb 20, 2015
2 parents e18c99c + 8d0a8cb commit b7e06ef
Show file tree
Hide file tree
Showing 19 changed files with 197 additions and 252 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"license": "MIT",
"require": {
"php": ">=5.4.0",
"guzzle/guzzle": "~3.7"
"guzzle/guzzle": "~3.7",
"egeloen/http-adapter": "0.6.*"
},
"require-dev": {
"phpunit/phpunit": "~4.0",
Expand Down
43 changes: 20 additions & 23 deletions src/Provider/AbstractProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
namespace League\OAuth2\Client\Provider;

use Closure;
use Guzzle\Http\Exception\BadResponseException;
use Guzzle\Service\Client as GuzzleClient;
use Ivory\HttpAdapter\GuzzleHttpAdapter;
use Ivory\HttpAdapter\HttpAdapterException;
use Ivory\HttpAdapter\HttpAdapterInterface;
use League\OAuth2\Client\Exception\IDPException as IDPException;
use League\OAuth2\Client\Grant\GrantInterface;
use League\OAuth2\Client\Token\AccessToken as AccessToken;
Expand Down Expand Up @@ -34,7 +35,7 @@ abstract class AbstractProvider implements ProviderInterface
public $headers = null;

/**
* @var GuzzleClient
* @var HttpAdapterInterface
*/
protected $httpClient;

Expand All @@ -46,18 +47,18 @@ abstract class AbstractProvider implements ProviderInterface
*/
protected $httpBuildEncType = 1;

public function __construct($options = [])
public function __construct($options = [], HttpAdapterInterface $httpClient = null)
{
foreach ($options as $option => $value) {
if (property_exists($this, $option)) {
$this->{$option} = $value;
}
}

$this->setHttpClient(new GuzzleClient());
$this->setHttpClient($httpClient ?: new GuzzleHttpAdapter());
}

public function setHttpClient(GuzzleClient $client)
public function setHttpClient(HttpAdapterInterface $client)
{
$this->httpClient = $client;

Expand All @@ -66,7 +67,7 @@ public function setHttpClient(GuzzleClient $client)

public function getHttpClient()
{
$client = clone $this->httpClient;
$client = $this->httpClient;

return $client;
}
Expand Down Expand Up @@ -178,25 +179,25 @@ public function getAccessToken($grant = 'authorization_code', $params = [])
// @codeCoverageIgnoreStart
// No providers included with this library use get but 3rd parties may
$client = $this->getHttpClient();
$client->setBaseUrl($this->urlAccessToken() . '?' . $this->httpBuildQuery($requestParams, '', '&'));
$request = $client->get(null, null, $requestParams)->send();
$response = $request->getBody();
$httpResponse = $client->get(
$this->urlAccessToken() . '?' . $this->httpBuildQuery($requestParams, '', '&')
);
$response = (string) $httpResponse->getBody();
break;
// @codeCoverageIgnoreEnd
case 'POST':
$client = $this->getHttpClient();
$client->setBaseUrl($this->urlAccessToken());
$request = $client->post(null, null, $requestParams)->send();
$response = $request->getBody();
$httpResponse = $client->post($this->urlAccessToken(), [], $requestParams);
$response = (string) $httpResponse->getBody();
break;
// @codeCoverageIgnoreStart
default:
throw new \InvalidArgumentException('Neither GET nor POST is specified for request');
// @codeCoverageIgnoreEnd
}
} catch (BadResponseException $e) {
} catch (HttpAdapterException $e) {
// @codeCoverageIgnoreStart
$response = $e->getResponse()->getBody();
$response = (string) $e->getResponse()->getBody();
// @codeCoverageIgnoreEnd
}

Expand Down Expand Up @@ -317,17 +318,13 @@ protected function fetchProviderData($url)
{
try {
$client = $this->getHttpClient();
$client->setBaseUrl($url);

if ($this->headers) {
$client->setDefaultOption('headers', $this->headers);
}
$httpResponse = $client->get($url, $this->headers ?: []);

$request = $client->get()->send();
$response = $request->getBody();
} catch (BadResponseException $e) {
$response = (string) $httpResponse->getBody();
} catch (HttpAdapterException $e) {
// @codeCoverageIgnoreStart
$raw_response = explode("\n", $e->getResponse());
$raw_response = explode("\n", (string) $e->getResponse()->getBody());
throw new IDPException(end($raw_response));
// @codeCoverageIgnoreEnd
}
Expand Down
6 changes: 2 additions & 4 deletions src/Provider/Microsoft.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@ public function urlUserDetails(AccessToken $token)
public function userDetails($response, AccessToken $token)
{
$client = $this->getHttpClient();
$client->setBaseUrl('https://apis.live.net/v5.0/'.$response->id.'/picture');
$request = $client->get()->send();
$info = $request->getInfo();
$imageUrl = $info['url'];
$httpResponse = $client->get('https://apis.live.net/v5.0/'.$response->id.'/picture');
$imageUrl = $httpResponse->getHeader('location');

$user = new User();

Expand Down
2 changes: 1 addition & 1 deletion test/src/Exception/IDPExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ public function testAsStringWithCode()

$this->assertEquals('message: 404: message', (string)$exception);
}
}
}
3 changes: 2 additions & 1 deletion test/src/Grant/AuthorizationCodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

class AuthorizationCodeTest extends \PHPUnit_Framework_TestCase
{
/** @var \League\OAuth2\Client\Provider\AbstractProvider */
protected $provider;

protected function setUp()
Expand All @@ -30,7 +31,7 @@ public function testGetAccessToken()
}

/**
* @expectedException BadMethodCallException
* @expectedException \BadMethodCallException
*/
public function testInvalidRefreshToken()
{
Expand Down
12 changes: 7 additions & 5 deletions test/src/Grant/ClientCredentialsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace League\OAuth2\Client\Test\Grant;

use Ivory\HttpAdapter\Message\Stream\StringStream;
use Mockery as m;

class ClientCredentialsTest extends \PHPUnit_Framework_TestCase
{
/** @var \League\OAuth2\Client\Provider\AbstractProvider */
protected $provider;

protected function setUp()
Expand All @@ -25,12 +27,12 @@ public function tearDown()

public function testGetAccessToken()
{
$response = m::mock('Guzzle\Http\Message\Response');
$response->shouldReceive('getBody')->times(1)->andReturn('{"access_token": "mock_access_token", "expires": 3600, "refresh_token": "mock_refresh_token", "uid": 1}');
$response = m::mock('Ivory\HttpAdapter\Message\ResponseInterface');
$response->shouldReceive('getBody')->times(1)->andReturn(new StringStream('{"access_token": "mock_access_token", "expires": 3600, "refresh_token": "mock_refresh_token", "uid": 1}'));

$client = m::mock('Ivory\HttpAdapter\HttpAdapterInterface');
$client->shouldReceive('post')->times(1)->andReturn($response);

$client = m::mock('Guzzle\Service\Client');
$client->shouldReceive('setBaseUrl')->times(1);
$client->shouldReceive('post->send')->times(1)->andReturn($response);
$this->provider->setHttpClient($client);

$token = $this->provider->getAccessToken('client_credentials');
Expand Down
16 changes: 9 additions & 7 deletions test/src/Grant/PasswordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace League\OAuth2\Client\Test\Grant;

use Ivory\HttpAdapter\Message\Stream\StringStream;
use Mockery as m;

class PasswordTest extends \PHPUnit_Framework_TestCase
{
/** @var \League\OAuth2\Client\Provider\AbstractProvider */
protected $provider;

protected function setUp()
Expand All @@ -25,12 +27,12 @@ public function tearDown()

public function testGetAccessToken()
{
$response = m::mock('Guzzle\Http\Message\Response');
$response->shouldReceive('getBody')->times(1)->andReturn('{"access_token": "mock_access_token", "expires": 3600, "refresh_token": "mock_refresh_token", "uid": 1}');
$response = m::mock('Ivory\HttpAdapter\Message\ResponseInterface');
$response->shouldReceive('getBody')->times(1)->andReturn(new StringStream('{"access_token": "mock_access_token", "expires": 3600, "refresh_token": "mock_refresh_token", "uid": 1}'));

$client = m::mock('Ivory\HttpAdapter\HttpAdapterInterface');
$client->shouldReceive('post')->times(1)->andReturn($response);

$client = m::mock('Guzzle\Service\Client');
$client->shouldReceive('setBaseUrl')->times(1);
$client->shouldReceive('post->send')->times(1)->andReturn($response);
$this->provider->setHttpClient($client);

$token = $this->provider->getAccessToken('password', array('username' => 'mock_username', 'password' => 'mock_password'));
Expand All @@ -41,15 +43,15 @@ public function testGetAccessToken()
}

/**
* @expectedException BadMethodCallException
* @expectedException \BadMethodCallException
*/
public function testInvalidUsername()
{
$this->provider->getAccessToken('password', array('invalid_username' => 'mock_username', 'password' => 'mock_password'));
}

/**
* @expectedException BadMethodCallException
* @expectedException \BadMethodCallException
*/
public function testInvalidPassword()
{
Expand Down
23 changes: 12 additions & 11 deletions test/src/Grant/RefreshTokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

class RefreshTokenTest extends \PHPUnit_Framework_TestCase
{
/** @var \League\OAuth2\Client\Provider\AbstractProvider */
protected $provider;

protected function setUp()
Expand All @@ -25,12 +26,12 @@ public function tearDown()

public function testGetAccessToken()
{
$response = m::mock('Guzzle\Http\Message\Response');
$response->shouldReceive('getBody')->times(2)->andReturn('{"access_token": "mock_access_token", "expires": 3600, "refresh_token": "mock_refresh_token", "uid": 1}');
$response = m::mock('Ivory\HttpAdapter\Message\ResponseInterface');
$response->shouldReceive('getBody')->times(2)->andReturn(new \Ivory\HttpAdapter\Message\Stream\StringStream('{"access_token": "mock_access_token", "expires": 3600, "refresh_token": "mock_refresh_token", "uid": 1}'));

$client = m::mock('Ivory\HttpAdapter\HttpAdapterInterface');
$client->shouldReceive('post')->times(2)->andReturn($response);

$client = m::mock('Guzzle\Service\Client');
$client->shouldReceive('setBaseUrl')->times(2);
$client->shouldReceive('post->send')->times(2)->andReturn($response);
$this->provider->setHttpClient($client);

$token = $this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']);
Expand All @@ -44,16 +45,16 @@ public function testGetAccessToken()
}

/**
* @expectedException BadMethodCallException
* @expectedException \BadMethodCallException
*/
public function testInvalidRefreshToken()
{
$response = m::mock('Guzzle\Http\Message\Response');
$response->shouldReceive('getBody')->times(1)->andReturn('{"access_token": "mock_access_token", "expires": 3600, "refresh_token": "mock_refresh_token", "uid": 1}');
$response = m::mock('Ivory\HttpAdapter\Message\ResponseInterface');
$response->shouldReceive('getBody')->times(1)->andReturn(new \Ivory\HttpAdapter\Message\Stream\StringStream('{"access_token": "mock_access_token", "expires": 3600, "refresh_token": "mock_refresh_token", "uid": 1}'));

$client = m::mock('Ivory\HttpAdapter\HttpAdapterInterface');
$client->shouldReceive('post')->times(1)->andReturn($response);

$client = m::mock('Guzzle\Service\Client');
$client->shouldReceive('setBaseUrl')->times(1);
$client->shouldReceive('post->send')->times(1)->andReturn($response);
$this->provider->setHttpClient($client);

$token = $this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']);
Expand Down
12 changes: 10 additions & 2 deletions test/src/Provider/AbstractProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ public function tearDown()
}

/**
* @expectedException InvalidArgumentException
* @expectedException \InvalidArgumentException
*/
public function testInvalidGrantString()
{
$this->provider->getAccessToken('invalid_grant', ['invalid_parameter' => 'none']);
}

/**
* @expectedException InvalidArgumentException
* @expectedException \InvalidArgumentException
*/
public function testInvalidGrantObject()
{
Expand Down Expand Up @@ -78,6 +78,14 @@ public function testConstructorSetsProperties()
}
}

public function testConstructorSetsHttpAdapter()
{
$mockAdapter = m::mock('Ivory\HttpAdapter\HttpAdapterInterface');

$mockProvider = new MockProvider([], $mockAdapter);
$this->assertSame($mockAdapter, $mockProvider->getHttpClient());
}

public function testSetRedirectHandler()
{
$this->testFunction = false;
Expand Down
36 changes: 36 additions & 0 deletions test/src/Provider/ConcreteProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace League\OAuth2\Client\Test\Provider;

use Ivory\HttpAdapter\Message\Stream\StringStream;
use Mockery as m;

abstract class ConcreteProviderTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \League\OAuth2\Client\Provider\AbstractProvider
*/
protected $provider;

protected function tearDown()
{
m::close();
parent::tearDown();
}

protected function createMockHttpClient()
{
$client = m::mock('Ivory\HttpAdapter\HttpAdapterInterface');
$client->shouldReceive('getConfiguration')->andReturn(new \Ivory\HttpAdapter\Configuration());

return $client;
}

protected function createMockResponse($responseBody)
{
$response = m::mock('Ivory\HttpAdapter\Message\ResponseInterface');
$response->shouldReceive('getBody')->andReturn(new StringStream($responseBody));

return $response;
}
}
Loading

0 comments on commit b7e06ef

Please sign in to comment.