diff --git a/composer.json b/composer.json index 72ce5cc..16b7ae1 100644 --- a/composer.json +++ b/composer.json @@ -4,7 +4,8 @@ "license": "MIT", "require": { "php": ">=5.4.0", - "guzzlehttp/guzzle": "~5.0" + "ext-curl": "*", + "egeloen/http-adapter": "0.6.*" }, "require-dev": { "phpunit/phpunit": "~4.0", diff --git a/src/Provider/AbstractProvider.php b/src/Provider/AbstractProvider.php index 90474b3..aa3a928 100644 --- a/src/Provider/AbstractProvider.php +++ b/src/Provider/AbstractProvider.php @@ -3,8 +3,9 @@ namespace League\OAuth2\Client\Provider; use Closure; -use GuzzleHttp\Client as GuzzleClient; -use GuzzleHttp\Exception\BadResponseException; +use Ivory\HttpAdapter\CurlHttpAdapter; +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; @@ -34,7 +35,7 @@ abstract class AbstractProvider implements ProviderInterface public $headers = null; /** - * @var GuzzleClient + * @var HttpAdapterInterface */ protected $httpClient; @@ -46,7 +47,7 @@ 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)) { @@ -54,10 +55,10 @@ abstract class AbstractProvider implements ProviderInterface } } - $this->setHttpClient(new GuzzleClient()); + $this->setHttpClient($httpClient ?: new CurlHttpAdapter()); } - public function setHttpClient(GuzzleClient $client) + public function setHttpClient(HttpAdapterInterface $client) { $this->httpClient = $client; @@ -66,7 +67,7 @@ abstract class AbstractProvider implements ProviderInterface public function getHttpClient() { - $client = clone $this->httpClient; + $client = $this->httpClient; return $client; } @@ -178,28 +179,25 @@ abstract class AbstractProvider implements ProviderInterface // @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(); - $url = $this->urlAccessToken(); - $options = [ - 'body' => $requestParams - ]; - $request = $client->post($url, $options); - $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 } @@ -335,17 +333,12 @@ abstract class AbstractProvider implements ProviderInterface try { $client = $this->getHttpClient(); - $options = []; + $httpResponse = $client->get($url, $this->headers ?: []); - if ($this->headers) { - $options['headers'] = $this->headers; - } - - $request = $client->get($url, $options); - $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 } @@ -353,7 +346,6 @@ abstract class AbstractProvider implements ProviderInterface return $response; } - public function setRedirectHandler(Closure $handler) { $this->redirectHandler = $handler; diff --git a/src/Provider/Microsoft.php b/src/Provider/Microsoft.php index ea37656..118abd1 100644 --- a/src/Provider/Microsoft.php +++ b/src/Provider/Microsoft.php @@ -28,10 +28,8 @@ class Microsoft extends AbstractProvider public function userDetails($response, AccessToken $token) { $client = $this->getHttpClient(); - $url = 'https://apis.live.net/v5.0/' . $response->id . '/picture'; - $request = $client->get($url); - $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(); diff --git a/test/src/Exception/IDPExceptionTest.php b/test/src/Exception/IDPExceptionTest.php index 3debc62..e31fdd5 100644 --- a/test/src/Exception/IDPExceptionTest.php +++ b/test/src/Exception/IDPExceptionTest.php @@ -41,4 +41,4 @@ class IDPExceptionTest extends \PHPUnit_Framework_TestCase $this->assertEquals('message: 404: message', (string)$exception); } -} \ No newline at end of file +} diff --git a/test/src/Grant/AuthorizationCodeTest.php b/test/src/Grant/AuthorizationCodeTest.php index 6ad4ad4..969685b 100644 --- a/test/src/Grant/AuthorizationCodeTest.php +++ b/test/src/Grant/AuthorizationCodeTest.php @@ -6,6 +6,7 @@ use Mockery as m; class AuthorizationCodeTest extends \PHPUnit_Framework_TestCase { + /** @var \League\OAuth2\Client\Provider\AbstractProvider */ protected $provider; protected function setUp() @@ -30,7 +31,7 @@ class AuthorizationCodeTest extends \PHPUnit_Framework_TestCase } /** - * @expectedException BadMethodCallException + * @expectedException \BadMethodCallException */ public function testInvalidRefreshToken() { diff --git a/test/src/Grant/ClientCredentialsTest.php b/test/src/Grant/ClientCredentialsTest.php index f34e121..115fbd7 100644 --- a/test/src/Grant/ClientCredentialsTest.php +++ b/test/src/Grant/ClientCredentialsTest.php @@ -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() @@ -25,11 +27,12 @@ class ClientCredentialsTest extends \PHPUnit_Framework_TestCase public function testGetAccessToken() { - $response = m::mock('GuzzleHttp\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('GuzzleHttp\Client'); + $client = m::mock('Ivory\HttpAdapter\HttpAdapterInterface'); $client->shouldReceive('post')->times(1)->andReturn($response); + $this->provider->setHttpClient($client); $token = $this->provider->getAccessToken('client_credentials'); diff --git a/test/src/Grant/PasswordTest.php b/test/src/Grant/PasswordTest.php index 311a046..bf6725a 100644 --- a/test/src/Grant/PasswordTest.php +++ b/test/src/Grant/PasswordTest.php @@ -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() @@ -25,11 +27,12 @@ class PasswordTest extends \PHPUnit_Framework_TestCase public function testGetAccessToken() { - $response = m::mock('GuzzleHttp\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('GuzzleHttp\Client'); + $client = m::mock('Ivory\HttpAdapter\HttpAdapterInterface'); $client->shouldReceive('post')->times(1)->andReturn($response); + $this->provider->setHttpClient($client); $token = $this->provider->getAccessToken('password', array('username' => 'mock_username', 'password' => 'mock_password')); @@ -40,7 +43,7 @@ class PasswordTest extends \PHPUnit_Framework_TestCase } /** - * @expectedException BadMethodCallException + * @expectedException \BadMethodCallException */ public function testInvalidUsername() { @@ -48,7 +51,7 @@ class PasswordTest extends \PHPUnit_Framework_TestCase } /** - * @expectedException BadMethodCallException + * @expectedException \BadMethodCallException */ public function testInvalidPassword() { diff --git a/test/src/Grant/RefreshTokenTest.php b/test/src/Grant/RefreshTokenTest.php index 16e4fe8..75b3bdb 100644 --- a/test/src/Grant/RefreshTokenTest.php +++ b/test/src/Grant/RefreshTokenTest.php @@ -6,6 +6,7 @@ use Mockery as m; class RefreshTokenTest extends \PHPUnit_Framework_TestCase { + /** @var \League\OAuth2\Client\Provider\AbstractProvider */ protected $provider; protected function setUp() @@ -25,11 +26,12 @@ class RefreshTokenTest extends \PHPUnit_Framework_TestCase public function testGetAccessToken() { - $response = m::mock('GuzzleHttp\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('GuzzleHttp\Client'); + $client = m::mock('Ivory\HttpAdapter\HttpAdapterInterface'); $client->shouldReceive('post')->times(2)->andReturn($response); + $this->provider->setHttpClient($client); $token = $this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']); @@ -43,15 +45,16 @@ class RefreshTokenTest extends \PHPUnit_Framework_TestCase } /** - * @expectedException BadMethodCallException + * @expectedException \BadMethodCallException */ public function testInvalidRefreshToken() { - $response = m::mock('GuzzleHttp\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('GuzzleHttp\Client'); + $client = m::mock('Ivory\HttpAdapter\HttpAdapterInterface'); $client->shouldReceive('post')->times(1)->andReturn($response); + $this->provider->setHttpClient($client); $token = $this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']); diff --git a/test/src/Provider/AbstractProviderTest.php b/test/src/Provider/AbstractProviderTest.php index f14e991..a6f48a0 100644 --- a/test/src/Provider/AbstractProviderTest.php +++ b/test/src/Provider/AbstractProviderTest.php @@ -29,7 +29,7 @@ class AbstractProviderTest extends \PHPUnit_Framework_TestCase } /** - * @expectedException InvalidArgumentException + * @expectedException \InvalidArgumentException */ public function testInvalidGrantString() { @@ -37,7 +37,7 @@ class AbstractProviderTest extends \PHPUnit_Framework_TestCase } /** - * @expectedException InvalidArgumentException + * @expectedException \InvalidArgumentException */ public function testInvalidGrantObject() { @@ -78,6 +78,14 @@ class AbstractProviderTest extends \PHPUnit_Framework_TestCase } } + public function testConstructorSetsHttpAdapter() + { + $mockAdapter = m::mock('Ivory\HttpAdapter\HttpAdapterInterface'); + + $mockProvider = new MockProvider([], $mockAdapter); + $this->assertSame($mockAdapter, $mockProvider->getHttpClient()); + } + public function testSetRedirectHandler() { $this->testFunction = false; diff --git a/test/src/Provider/ConcreteProviderTest.php b/test/src/Provider/ConcreteProviderTest.php new file mode 100644 index 0000000..3db0d99 --- /dev/null +++ b/test/src/Provider/ConcreteProviderTest.php @@ -0,0 +1,36 @@ +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; + } +} diff --git a/test/src/Provider/EventbriteTest.php b/test/src/Provider/EventbriteTest.php index a0fee2e..edb7847 100644 --- a/test/src/Provider/EventbriteTest.php +++ b/test/src/Provider/EventbriteTest.php @@ -4,10 +4,8 @@ namespace League\OAuth2\Client\Test\Provider; use Mockery as m; -class EventbriteTest extends \PHPUnit_Framework_TestCase +class EventbriteTest extends ConcreteProviderTest { - protected $provider; - protected function setUp() { $this->provider = new \League\OAuth2\Client\Provider\Eventbrite([ @@ -17,12 +15,6 @@ class EventbriteTest extends \PHPUnit_Framework_TestCase ]); } - public function tearDown() - { - m::close(); - parent::tearDown(); - } - public function testAuthorizationUrl() { $url = $this->provider->getAuthorizationUrl(); @@ -48,11 +40,9 @@ class EventbriteTest extends \PHPUnit_Framework_TestCase public function testGetAccessToken() { - $response = m::mock('GuzzleHttp\Message\Response'); - $response->shouldReceive('getBody')->times(1)->andReturn('{"access_token": "mock_access_token", "expires": 3600, "refresh_token": "mock_refresh_token", "uid": 1}'); + $client = $this->createMockHttpClient(); + $client->shouldReceive('post')->times(1)->andReturn($this->createMockResponse('{"access_token": "mock_access_token", "expires": 3600, "refresh_token": "mock_refresh_token", "uid": 1}')); - $client = m::mock('GuzzleHttp\Client'); - $client->shouldReceive('post')->times(1)->andReturn($response); $this->provider->setHttpClient($client); $token = $this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']); @@ -71,13 +61,11 @@ class EventbriteTest extends \PHPUnit_Framework_TestCase public function testUserData() { - $postResponse = m::mock('GuzzleHttp\Message\Response'); - $postResponse->shouldReceive('getBody')->times(1)->andReturn('{"access_token": "mock_access_token", "expires": 3600, "refresh_token": "mock_refresh_token", "uid": 1}'); + $client = $this->createMockHttpClient(); - $getResponse = m::mock('GuzzleHttp\Message\Response'); - $getResponse->shouldReceive('getBody')->times(4)->andReturn('{"user": {"user_id": 12345, "email": "mock_email"}}'); + $postResponse = $this->createMockResponse('{"access_token": "mock_access_token", "expires": 3600, "refresh_token": "mock_refresh_token", "uid": 1}'); + $getResponse = $this->createMockResponse('{"user": {"user_id": 12345, "email": "mock_email"}}'); - $client = m::mock('GuzzleHttp\Client'); $client->shouldReceive('post')->times(1)->andReturn($postResponse); $client->shouldReceive('get')->times(4)->andReturn($getResponse); diff --git a/test/src/Provider/GithubTest.php b/test/src/Provider/GithubTest.php index 19699a9..69a3ed3 100644 --- a/test/src/Provider/GithubTest.php +++ b/test/src/Provider/GithubTest.php @@ -4,8 +4,11 @@ namespace League\OAuth2\Client\Test\Provider; use Mockery as m; -class GithubTest extends \PHPUnit_Framework_TestCase +class GithubTest extends ConcreteProviderTest { + /** + * @var \League\OAuth2\Client\Provider\Github + */ protected $provider; protected function setUp() @@ -17,12 +20,6 @@ class GithubTest extends \PHPUnit_Framework_TestCase ]); } - public function tearDown() - { - m::close(); - parent::tearDown(); - } - public function testAuthorizationUrl() { $url = $this->provider->getAuthorizationUrl(); @@ -48,11 +45,9 @@ class GithubTest extends \PHPUnit_Framework_TestCase public function testGetAccessToken() { - $response = m::mock('GuzzleHttp\Message\Response'); - $response->shouldReceive('getBody')->times(1)->andReturn('access_token=mock_access_token&expires=3600&refresh_token=mock_refresh_token&uid=1'); + $client = $this->createMockHttpClient(); + $client->shouldReceive('post')->times(1)->andReturn($this->createMockResponse('access_token=mock_access_token&expires=3600&refresh_token=mock_refresh_token&uid=1')); - $client = m::mock('GuzzleHttp\Client'); - $client->shouldReceive('post')->times(1)->andReturn($response); $this->provider->setHttpClient($client); $token = $this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']); @@ -68,11 +63,9 @@ class GithubTest extends \PHPUnit_Framework_TestCase { $this->provider->uidKey = 'otherKey'; - $response = m::mock('GuzzleHttp\Message\Response'); - $response->shouldReceive('getBody')->times(1)->andReturn('access_token=mock_access_token&expires=3600&refresh_token=mock_refresh_token&otherKey={1234}'); + $client = $this->createMockHttpClient(); + $client->shouldReceive('post')->times(1)->andReturn($this->createMockResponse('access_token=mock_access_token&expires=3600&refresh_token=mock_refresh_token&otherKey={1234}')); - $client = m::mock('GuzzleHttp\Client'); - $client->shouldReceive('post')->times(1)->andReturn($response); $this->provider->setHttpClient($client); $token = $this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']); @@ -92,15 +85,14 @@ class GithubTest extends \PHPUnit_Framework_TestCase public function testUserData() { - $postResponse = m::mock('GuzzleHttp\Message\Response'); - $postResponse->shouldReceive('getBody')->times(1)->andReturn('access_token=mock_access_token&expires=3600&refresh_token=mock_refresh_token&uid=1'); + $client = $this->createMockHttpClient(); - $getResponse = m::mock('GuzzleHttp\Message\Response'); - $getResponse->shouldReceive('getBody')->times(4)->andReturn('{"id": 12345, "login": "mock_login", "name": "mock_name", "email": "mock_email"}'); + $postResponse = $this->createMockResponse('access_token=mock_access_token&expires=3600&refresh_token=mock_refresh_token&uid=1'); + $getResponse = $this->createMockResponse('{"id": 12345, "login": "mock_login", "name": "mock_name", "email": "mock_email"}'); - $client = m::mock('GuzzleHttp\Client'); $client->shouldReceive('post')->times(1)->andReturn($postResponse); $client->shouldReceive('get')->times(4)->andReturn($getResponse); + $this->provider->setHttpClient($client); $token = $this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']); @@ -114,11 +106,9 @@ class GithubTest extends \PHPUnit_Framework_TestCase public function testGithubDomainUrls() { - $client = m::mock('GuzzleHttp\Client'); - $response = m::mock('GuzzleHttp\Message\Response'); - $response->shouldReceive('getBody')->times(1)->andReturn('access_token=mock_access_token&expires=3600&refresh_token=mock_refresh_token&otherKey={1234}'); + $client = $this->createMockHttpClient(); + $client->shouldReceive('post')->times(1)->andReturn($this->createMockResponse('access_token=mock_access_token&expires=3600&refresh_token=mock_refresh_token&otherKey={1234}')); - $client->shouldReceive('post')->times(1)->andReturn($response); $this->provider->setHttpClient($client); $token = $this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']); @@ -132,11 +122,9 @@ class GithubTest extends \PHPUnit_Framework_TestCase { $this->provider->domain = 'https://github.company.com'; - $client = m::mock('GuzzleHttp\Client'); - $response = m::mock('GuzzleHttp\Message\Response'); - $response->shouldReceive('getBody')->times(1)->andReturn('access_token=mock_access_token&expires=3600&refresh_token=mock_refresh_token&otherKey={1234}'); + $client = $this->createMockHttpClient(); + $client->shouldReceive('post')->times(1)->andReturn($this->createMockResponse('access_token=mock_access_token&expires=3600&refresh_token=mock_refresh_token&otherKey={1234}')); - $client->shouldReceive('post')->times(1)->andReturn($response); $this->provider->setHttpClient($client); $token = $this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']); @@ -148,15 +136,14 @@ class GithubTest extends \PHPUnit_Framework_TestCase public function testUserEmails() { - $postResponse = m::mock('GuzzleHttp\Message\Response'); - $postResponse->shouldReceive('getBody')->times(1)->andReturn('access_token=mock_access_token&expires=3600&refresh_token=mock_refresh_token&uid=1'); + $client = $this->createMockHttpClient(); - $getResponse = m::mock('GuzzleHttp\Message\Response'); - $getResponse->shouldReceive('getBody')->times(1)->andReturn('[{"email":"mock_email_1","primary":false,"verified":true},{"email":"mock_email_2","primary":false,"verified":true},{"email":"mock_email_3","primary":true,"verified":true}]'); + $postResponse = $this->createMockResponse('access_token=mock_access_token&expires=3600&refresh_token=mock_refresh_token&uid=1'); + $getResponse = $this->createMockResponse('[{"email":"mock_email_1","primary":false,"verified":true},{"email":"mock_email_2","primary":false,"verified":true},{"email":"mock_email_3","primary":true,"verified":true}]'); - $client = m::mock('GuzzleHttp\Client'); $client->shouldReceive('post')->times(1)->andReturn($postResponse); $client->shouldReceive('get')->times(1)->andReturn($getResponse); + $this->provider->setHttpClient($client); $token = $this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']); diff --git a/test/src/Provider/GoogleTest.php b/test/src/Provider/GoogleTest.php index 8fca5f9..5d3e9d5 100644 --- a/test/src/Provider/GoogleTest.php +++ b/test/src/Provider/GoogleTest.php @@ -4,8 +4,11 @@ namespace League\OAuth2\Client\Test\Provider; use Mockery as m; -class GoogleTest extends \PHPUnit_Framework_TestCase +class GoogleTest extends ConcreteProviderTest { + /** + * @var \League\OAuth2\Client\Provider\Google + */ protected $provider; protected function setUp() @@ -19,12 +22,6 @@ class GoogleTest extends \PHPUnit_Framework_TestCase ]); } - public function tearDown() - { - m::close(); - parent::tearDown(); - } - public function testAuthorizationUrl() { $url = $this->provider->getAuthorizationUrl(); @@ -52,11 +49,9 @@ class GoogleTest extends \PHPUnit_Framework_TestCase public function testGetAccessToken() { - $response = m::mock('GuzzleHttp\Message\Response'); - $response->shouldReceive('getBody')->times(1)->andReturn('{"access_token": "mock_access_token", "expires": 3600, "refresh_token": "mock_refresh_token", "uid": 1}'); + $client = $this->createMockHttpClient(); + $client->shouldReceive('post')->times(1)->andReturn($this->createMockResponse('{"access_token": "mock_access_token", "expires": 3600, "refresh_token": "mock_refresh_token", "uid": 1}')); - $client = m::mock('GuzzleHttp\Client'); - $client->shouldReceive('post')->times(1)->andReturn($response); $this->provider->setHttpClient($client); $token = $this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']); @@ -77,15 +72,14 @@ class GoogleTest extends \PHPUnit_Framework_TestCase public function testUserData() { - $postResponse = m::mock('GuzzleHttp\Message\Response'); - $postResponse->shouldReceive('getBody')->times(1)->andReturn('{"access_token": "mock_access_token", "expires": 3600, "refresh_token": "mock_refresh_token", "uid": 1}'); + $client = $this->createMockHttpClient(); - $getResponse = m::mock('GuzzleHttp\Message\Response'); - $getResponse->shouldReceive('getBody')->times(4)->andReturn('{"emails": [{"value": "mock_email"}],"id": "12345","displayName": "mock_name","name": {"familyName": "mock_last_name","givenName": "mock_first_name"},"image": {"url": "mock_image_url"}}'); + $postResponse = $this->createMockResponse('{"access_token": "mock_access_token", "expires": 3600, "refresh_token": "mock_refresh_token", "uid": 1}'); + $getResponse = $this->createMockResponse('{"emails": [{"value": "mock_email"}],"id": "12345","displayName": "mock_name","name": {"familyName": "mock_last_name","givenName": "mock_first_name"},"image": {"url": "mock_image_url"}}'); - $client = m::mock('GuzzleHttp\Client'); $client->shouldReceive('post')->times(1)->andReturn($postResponse); $client->shouldReceive('get')->times(4)->andReturn($getResponse); + $this->provider->setHttpClient($client); $token = $this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']); diff --git a/test/src/Provider/InstagramTest.php b/test/src/Provider/InstagramTest.php index 01407b2..defd7e4 100644 --- a/test/src/Provider/InstagramTest.php +++ b/test/src/Provider/InstagramTest.php @@ -4,10 +4,8 @@ namespace League\OAuth2\Client\Test\Provider; use Mockery as m; -class InstagramTest extends \PHPUnit_Framework_TestCase +class InstagramTest extends ConcreteProviderTest { - protected $provider; - protected function setUp() { $this->provider = new \League\OAuth2\Client\Provider\Instagram([ @@ -17,12 +15,6 @@ class InstagramTest extends \PHPUnit_Framework_TestCase ]); } - public function tearDown() - { - m::close(); - parent::tearDown(); - } - public function testAuthorizationUrl() { $url = $this->provider->getAuthorizationUrl(); @@ -48,11 +40,9 @@ class InstagramTest extends \PHPUnit_Framework_TestCase public function testGetAccessToken() { - $response = m::mock('GuzzleHttp\Message\Response'); - $response->shouldReceive('getBody')->times(1)->andReturn('{"access_token": "mock_access_token", "expires": 3600, "refresh_token": "mock_refresh_token", "uid": 1}'); + $client = $this->createMockHttpClient(); + $client->shouldReceive('post')->times(1)->andReturn($this->createMockResponse('{"access_token": "mock_access_token", "expires": 3600, "refresh_token": "mock_refresh_token", "uid": 1}')); - $client = m::mock('GuzzleHttp\Client'); - $client->shouldReceive('post')->times(1)->andReturn($response); $this->provider->setHttpClient($client); $token = $this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']); @@ -73,15 +63,14 @@ class InstagramTest extends \PHPUnit_Framework_TestCase public function testUserData() { - $postResponse = m::mock('GuzzleHttp\Message\Response'); - $postResponse->shouldReceive('getBody')->times(1)->andReturn('{"access_token": "mock_access_token", "expires": 3600, "refresh_token": "mock_refresh_token", "uid": 1}'); + $client = $this->createMockHttpClient(); - $getResponse = m::mock('GuzzleHttp\Message\Response'); - $getResponse->shouldReceive('getBody')->times(4)->andReturn('{"data": {"id": "12345", "username": "mock_username", "full_name": "mock_full_name", "bio": "mock_bio", "profile_picture": "mock_profile_picture"}}'); + $postResponse = $this->createMockResponse('{"access_token": "mock_access_token", "expires": 3600, "refresh_token": "mock_refresh_token", "uid": 1}'); + $getResponse = $this->createMockResponse('{"data": {"id": "12345", "username": "mock_username", "full_name": "mock_full_name", "bio": "mock_bio", "profile_picture": "mock_profile_picture"}}'); - $client = m::mock('GuzzleHttp\Client'); $client->shouldReceive('post')->times(1)->andReturn($postResponse); $client->shouldReceive('get')->times(4)->andReturn($getResponse); + $this->provider->setHttpClient($client); $token = $this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']); diff --git a/test/src/Provider/LinkedInTest.php b/test/src/Provider/LinkedInTest.php index ff350c0..673f122 100644 --- a/test/src/Provider/LinkedInTest.php +++ b/test/src/Provider/LinkedInTest.php @@ -4,10 +4,8 @@ namespace League\OAuth2\Client\Test\Provider; use Mockery as m; -class LinkedInTest extends \PHPUnit_Framework_TestCase +class LinkedInTest extends ConcreteProviderTest { - protected $provider; - protected function setUp() { $this->provider = new \League\OAuth2\Client\Provider\LinkedIn([ @@ -17,12 +15,6 @@ class LinkedInTest extends \PHPUnit_Framework_TestCase ]); } - public function tearDown() - { - m::close(); - parent::tearDown(); - } - public function testAuthorizationUrl() { $url = $this->provider->getAuthorizationUrl(); @@ -48,11 +40,11 @@ class LinkedInTest extends \PHPUnit_Framework_TestCase public function testGetAccessToken() { - $response = m::mock('GuzzleHttp\Message\Response'); - $response->shouldReceive('getBody')->times(1)->andReturn('{"access_token": "mock_access_token", "expires": 3600, "refresh_token": "mock_refresh_token", "uid": 1}'); + $client = $this->createMockHttpClient(); + $response = $this->createMockResponse('{"access_token": "mock_access_token", "expires": 3600, "refresh_token": "mock_refresh_token", "uid": 1}'); - $client = m::mock('GuzzleHttp\Client'); $client->shouldReceive('post')->times(1)->andReturn($response); + $this->provider->setHttpClient($client); $token = $this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']); @@ -73,15 +65,14 @@ class LinkedInTest extends \PHPUnit_Framework_TestCase public function testUserData() { - $postResponse = m::mock('GuzzleHttp\Message\Response'); - $postResponse->shouldReceive('getBody')->times(1)->andReturn('{"access_token": "mock_access_token", "expires": 3600, "refresh_token": "mock_refresh_token", "uid": 1}'); + $client = $this->createMockHttpClient(); - $getResponse = m::mock('GuzzleHttp\Message\Response'); - $getResponse->shouldReceive('getBody')->times(4)->andReturn('{"id": 12345, "firstName": "mock_first_name", "lastName": "mock_last_name", "emailAddress": "mock_email", "location": { "name": "mock_location" }, "headline": "mock_headline", "pictureUrl": "mock_picture_url", "publicProfileUrl": "mock_profile_url"}'); + $postResponse = $this->createMockResponse('{"access_token": "mock_access_token", "expires": 3600, "refresh_token": "mock_refresh_token", "uid": 1}'); + $getResponse = $this->createMockResponse('{"id": 12345, "firstName": "mock_first_name", "lastName": "mock_last_name", "emailAddress": "mock_email", "location": { "name": "mock_location" }, "headline": "mock_headline", "pictureUrl": "mock_picture_url", "publicProfileUrl": "mock_profile_url"}'); - $client = m::mock('GuzzleHttp\Client'); $client->shouldReceive('post')->times(1)->andReturn($postResponse); $client->shouldReceive('get')->times(4)->andReturn($getResponse); + $this->provider->setHttpClient($client); $token = $this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']); diff --git a/test/src/Provider/MicrosoftTest.php b/test/src/Provider/MicrosoftTest.php index 4c3533b..c050464 100644 --- a/test/src/Provider/MicrosoftTest.php +++ b/test/src/Provider/MicrosoftTest.php @@ -4,10 +4,8 @@ namespace League\OAuth2\Client\Test\Provider; use Mockery as m; -class MicrosoftTest extends \PHPUnit_Framework_TestCase +class MicrosoftTest extends ConcreteProviderTest { - protected $provider; - protected function setUp() { $this->provider = new \League\OAuth2\Client\Provider\Microsoft([ @@ -17,12 +15,6 @@ class MicrosoftTest extends \PHPUnit_Framework_TestCase ]); } - public function tearDown() - { - m::close(); - parent::tearDown(); - } - public function testAuthorizationUrl() { $url = $this->provider->getAuthorizationUrl(); @@ -56,11 +48,11 @@ class MicrosoftTest extends \PHPUnit_Framework_TestCase public function testGetAccessToken() { - $response = m::mock('GuzzleHttp\Message\Response'); - $response->shouldReceive('getBody')->times(1)->andReturn('{"access_token": "mock_access_token", "expires": 3600, "refresh_token": "mock_refresh_token", "uid": 1}'); + $client = $this->createMockHttpClient(); + $response = $this->createMockResponse('{"access_token": "mock_access_token", "expires": 3600, "refresh_token": "mock_refresh_token", "uid": 1}'); - $client = m::mock('GuzzleHttp\Client'); $client->shouldReceive('post')->times(1)->andReturn($response); + $this->provider->setHttpClient($client); $token = $this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']); @@ -81,18 +73,12 @@ class MicrosoftTest extends \PHPUnit_Framework_TestCase public function testUserData() { - $postResponse = m::mock('GuzzleHttp\Message\Response'); - $postResponse->shouldReceive('getBody')->times(1)->andReturn('{"access_token": "mock_access_token", "expires": 3600, "refresh_token": "mock_refresh_token", "uid": 1}'); - - $getResponse = m::mock('GuzzleHttp\Message\Response'); - - $getResponse->shouldReceive('getBody')->times(4)->andReturn('{"id": 12345, "name": "mock_name", "first_name": "mock_first_name", "last_name": "mock_last_name", "emails": {"preferred": "mock_email"}, "link": "mock_link"}'); - $getResponse->shouldReceive('getInfo')->andReturn(['url' => 'mock_image_url']); - - $client = m::mock('GuzzleHttp\Client'); + $client = $this->createMockHttpClient(); + $postResponse = $this->createMockResponse('{"access_token": "mock_access_token", "expires": 3600, "refresh_token": "mock_refresh_token", "uid": 1}'); + $getResponse = $this->createMockResponse('{"id": 12345, "name": "mock_name", "first_name": "mock_first_name", "last_name": "mock_last_name", "emails": {"preferred": "mock_email"}, "link": "mock_link"}'); + $getResponse->shouldReceive('getHeader')->with('location')->andReturn('mock_image_url'); $client->shouldReceive('post')->times(1)->andReturn($postResponse); $client->shouldReceive('get')->times(5)->andReturn($getResponse); - $this->provider->setHttpClient($client); $token = $this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']); diff --git a/test/src/Token/AccessTokenTest.php b/test/src/Token/AccessTokenTest.php index 7fe2752..2c4c5d4 100644 --- a/test/src/Token/AccessTokenTest.php +++ b/test/src/Token/AccessTokenTest.php @@ -5,7 +5,7 @@ namespace League\OAuth2\Client\Test\Token; class AccessTokenTest extends \PHPUnit_Framework_TestCase { /** - * @expectedException InvalidArgumentException + * @expectedException \InvalidArgumentException */ public function testInvalidRefreshToken() {