From d51cd1cad26a913db57415a270b8c8bb6ad204d2 Mon Sep 17 00:00:00 2001 From: Steven Maguire Date: Wed, 23 Sep 2015 21:40:03 -0500 Subject: [PATCH] init --- .gitignore | 5 + .scrutinizer.yml | 35 +++++ .travis.yml | 20 +++ CHANGELOG.md | 19 +++ CONTRIBUTING.md | 42 ++++++ LICENSE | 21 +++ README.md | 94 ++++++++++++ composer.json | 44 ++++++ phpunit.xml.dist | 37 +++++ src/Provider/Paypal.php | 127 ++++++++++++++++ src/Provider/PaypalResourceOwner.php | 193 ++++++++++++++++++++++++ tests/src/Provider/PaypalTest.php | 214 +++++++++++++++++++++++++++ 12 files changed, 851 insertions(+) create mode 100644 .gitignore create mode 100644 .scrutinizer.yml create mode 100644 .travis.yml create mode 100644 CHANGELOG.md create mode 100644 CONTRIBUTING.md create mode 100644 LICENSE create mode 100644 README.md create mode 100644 composer.json create mode 100644 phpunit.xml.dist create mode 100644 src/Provider/Paypal.php create mode 100644 src/Provider/PaypalResourceOwner.php create mode 100644 tests/src/Provider/PaypalTest.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9c9c8f2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +/build +/vendor +composer.phar +composer.lock +.DS_Store diff --git a/.scrutinizer.yml b/.scrutinizer.yml new file mode 100644 index 0000000..a903494 --- /dev/null +++ b/.scrutinizer.yml @@ -0,0 +1,35 @@ +filter: + excluded_paths: [tests/*] +checks: + php: + code_rating: true + remove_extra_empty_lines: true + remove_php_closing_tag: true + remove_trailing_whitespace: true + fix_use_statements: + remove_unused: true + preserve_multiple: false + preserve_blanklines: true + order_alphabetically: true + fix_php_opening_tag: true + fix_linefeed: true + fix_line_ending: true + fix_identation_4spaces: true + fix_doc_comments: true +tools: + external_code_coverage: + timeout: 600 + runs: 2 + php_analyzer: true + php_code_coverage: false + php_code_sniffer: + config: + standard: PSR2 + filter: + paths: ['src'] + php_loc: + enabled: true + excluded_dirs: [vendor, tests] + php_cpd: + enabled: true + excluded_dirs: [vendor, tests] diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..f20c368 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,20 @@ +language: php + +php: + - 5.5 + - 5.6 + - 7.0 + - hhvm + +before_script: + - travis_retry composer self-update + - travis_retry composer install --no-interaction --prefer-source --dev + - travis_retry phpenv rehash + +script: + - ./vendor/bin/phpcs --standard=psr2 src/ + - ./vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover + +after_script: + - if [ "$TRAVIS_PHP_VERSION" != "hhvm" ] && [ "$TRAVIS_PHP_VERSION" != "7.0" ]; then wget https://scrutinizer-ci.com/ocular.phar; fi + - if [ "$TRAVIS_PHP_VERSION" != "hhvm" ] && [ "$TRAVIS_PHP_VERSION" != "7.0" ]; then php ocular.phar code-coverage:upload --format=php-clover coverage.clover; fi diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..120b222 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,19 @@ +# Changelog +All Notable changes to `oauth2-paypal` will be documented in this file + +## 0.1.0 - 2015-09-24 + +### Added +- Initial release! + +### Deprecated +- Nothing + +### Fixed +- Nothing + +### Removed +- Nothing + +### Security +- Nothing diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..c60756a --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,42 @@ +# Contributing + +Contributions are **welcome** and will be fully **credited**. + +We accept contributions via Pull Requests on [Github](https://github.com/stevenmaguire/oauth2-paypal). + + +## Pull Requests + +- **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](http://pear.php.net/package/PHP_CodeSniffer). + +- **Add tests!** - Your patch won't be accepted if it doesn't have tests. + +- **Document any change in behaviour** - Make sure the README and any other relevant documentation are kept up-to-date. + +- **Consider our release cycle** - We try to follow SemVer. Randomly breaking public APIs is not an option. + +- **Create topic branches** - Don't ask us to pull from your master branch. + +- **One pull request per feature** - If you want to do more than one thing, send multiple pull requests. + +- **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please squash them before submitting. + +- **Ensure tests pass!** - Please run the tests (see below) before submitting your pull request, and make sure they pass. We won't accept a patch until all tests pass. + +- **Ensure no coding standards violations** - Please run PHP Code Sniffer using the PSR-2 standard (see below) before submitting your pull request. A violation will cause the build to fail, so please make sure there are no violations. We can't accept a patch if the build fails. + + +## Running Tests + +``` bash +$ ./vendor/bin/phpunit +``` + + +## Running PHP Code Sniffer + +``` bash +$ ./vendor/bin/phpcs src --standard=psr2 -sp +``` + +**Happy coding**! diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..51455e2 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2015 Steven Maguire + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..28284bf --- /dev/null +++ b/README.md @@ -0,0 +1,94 @@ +# Paypal Provider for OAuth 2.0 Client + +[![Latest Version](https://img.shields.io/github/release/stevenmaguire/oauth2-paypal.svg?style=flat-square)](https://github.com/stevenmaguire/oauth2-paypal/releases) +[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md) +[![Build Status](https://img.shields.io/travis/stevenmaguire/oauth2-paypal/master.svg?style=flat-square)](https://travis-ci.org/stevenmaguire/oauth2-paypal) +[![Coverage Status](https://img.shields.io/scrutinizer/coverage/g/stevenmaguire/oauth2-paypal.svg?style=flat-square)](https://scrutinizer-ci.com/g/stevenmaguire/oauth2-paypal/code-structure) +[![Quality Score](https://img.shields.io/scrutinizer/g/stevenmaguire/oauth2-paypal.svg?style=flat-square)](https://scrutinizer-ci.com/g/stevenmaguire/oauth2-paypal) +[![Total Downloads](https://img.shields.io/packagist/dt/stevenmaguire/oauth2-paypal.svg?style=flat-square)](https://packagist.org/packages/stevenmaguire/oauth2-paypal) + +This package provides Paypal OAuth 2.0 support for the PHP League's [OAuth 2.0 Client](https://github.com/thephpleague/oauth2-client). + +## Installation + +To install, use composer: + +``` +composer require stevenmaguire/oauth2-paypal +``` + +## Usage + +Usage is the same as The League's OAuth client, using `\Stevenmaguire\OAuth2\Client\Provider\Paypal` as the provider. + +### Authorization Code Flow + +```php +$provider = new Stevenmaguire\OAuth2\Client\Provider\Paypal([ + 'clientId' => '{paypal-client-id}', + 'clientSecret' => '{paypal-client-secret}', + 'redirectUri' => 'https://example.com/callback-url', + 'isSandbox' => true, // Optional, defaults to false. When true, client uses sandbox urls. +]); +``` + +For further usage of this package please refer to the [core package documentation on "Authorization Code Grant"](https://github.com/thephpleague/oauth2-client#usage). + +#### Managing scopes with your authorization request + +```php +$options = [ + 'scope' => ['openid', 'profile', 'email', 'phone', 'address'] +]; + +$authorizationUrl = $provider->getAuthorizationUrl($options); +``` + +>The value passed must always include `openid` at minimum + +You can review a [full list of scopes](https://developer.paypal.com/docs/integration/direct/identity/attributes/) on PayPal's website. + +### Refreshing a Token + +```php +$provider = new Stevenmaguire\OAuth2\Client\Provider\Paypal([ + 'clientId' => '{paypal-client-id}', + 'clientSecret' => '{paypal-client-secret}', + 'redirectUri' => 'https://example.com/callback-url' +]); + +$existingAccessToken = getAccessTokenFromYourDataStore(); + +if ($existingAccessToken->hasExpired()) { + $newAccessToken = $provider->getAccessToken('refresh_token', [ + 'refresh_token' => $existingAccessToken->getRefreshToken() + ]); + + // Purge old access token and store new access token to your data store. +} +``` + +For further usage of this package please refer to the [core package documentation on "Refreshing a Token"](https://github.com/thephpleague/oauth2-client#refreshing-a-token). + + + +## Testing + +``` bash +$ ./vendor/bin/phpunit +``` + +## Contributing + +Please see [CONTRIBUTING](https://github.com/stevenmaguire/oauth2-paypal/blob/master/CONTRIBUTING.md) for details. + + +## Credits + +- [Steven Maguire](https://github.com/stevenmaguire) +- [All Contributors](https://github.com/stevenmaguire/oauth2-paypal/contributors) + + +## License + +The MIT License (MIT). Please see [License File](https://github.com/stevenmaguire/oauth2-paypal/blob/master/LICENSE) for more information. diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..a3f2351 --- /dev/null +++ b/composer.json @@ -0,0 +1,44 @@ +{ + "name": "stevenmaguire/oauth2-paypal", + "description": "Paypal OAuth 2.0 Client Provider for The PHP League OAuth2-Client", + "license": "MIT", + "authors": [ + { + "name": "Steven Maguire", + "email": "stevenmaguire@gmail.com", + "homepage": "https://github.com/stevenmaguire" + } + ], + "keywords": [ + "oauth", + "oauth2", + "client", + "authorization", + "authorisation", + "paypal" + ], + "require": { + "php": ">=5.5.0", + "league/oauth2-client": "~1.0" + }, + "require-dev": { + "phpunit/phpunit": "~4.0", + "mockery/mockery": "~0.9", + "squizlabs/php_codesniffer": "~2.0" + }, + "autoload": { + "psr-4": { + "Stevenmaguire\\OAuth2\\Client\\": "src/" + } + }, + "autoload-dev": { + "psr-4": { + "Stevenmaguire\\OAuth2\\Client\\Test\\": "tests/src/" + } + }, + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + } +} diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..b486c71 --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,37 @@ + + + + + + + + + ./tests/ + + + + + ./ + + ./vendor + ./tests + + + + diff --git a/src/Provider/Paypal.php b/src/Provider/Paypal.php new file mode 100644 index 0000000..e28b191 --- /dev/null +++ b/src/Provider/Paypal.php @@ -0,0 +1,127 @@ +isSandbox ? 'https://api.sandbox.paypal.com' : 'https://api.paypal.com'; + } + + /** + * Creates and returns web app base url base on client configuration. + * + * @return string + */ + protected function getWebAppUrl() + { + return (bool) $this->isSandbox ? 'https://www.sandbox.paypal.com' : 'https://www.paypal.com'; + } + + /** + * Get authorization url to begin OAuth flow + * + * @return string + */ + public function getBaseAuthorizationUrl() + { + return $this->getWebAppUrl().'/webapps/auth/protocol/openidconnect/v1/authorize'; + } + + /** + * Get access token url to retrieve token + * + * @return string + */ + public function getBaseAccessTokenUrl(array $params) + { + return $this->getApiUrl().'/v1/identity/openidconnect/tokenservice'; + } + + /** + * Get provider url to fetch user details + * + * @param AccessToken $token + * + * @return string + */ + public function getResourceOwnerDetailsUrl(AccessToken $token) + { + return $this->getApiUrl().'/v1/identity/openidconnect/userinfo?schema=openid'; + } + + /** + * Get the default scopes used by this provider. + * + * This should not be a complete list of all scopes, but the minimum + * required for the provider user interface! + * + * @return array + */ + protected function getDefaultScopes() + { + return ['openid']; + } + + /** + * Returns the string that should be used to separate scopes when building + * the URL for requesting an access token. + * + * @return string Scope separator, defaults to ',' + */ + protected function getScopeSeparator() + { + return ' '; + } + + /** + * Check a provider response for errors. + * + * @throws IdentityProviderException + * @param ResponseInterface $response + * @param string $data Parsed response data + * @return void + */ + protected function checkResponse(ResponseInterface $response, $data) + { + $statusCode = $response->getStatusCode(); + if ($statusCode > 400) { + throw new IdentityProviderException( + $data['message'] ?: $response->getReasonPhrase(), + $statusCode, + $response + ); + } + } + + /** + * Generate a user object from a successful user details request. + * + * @param object $response + * @param AccessToken $token + * @return League\OAuth2\Client\Provider\ResourceOwnerInterface + */ + protected function createResourceOwner(array $response, AccessToken $token) + { + return new PaypalResourceOwner($response); + } +} diff --git a/src/Provider/PaypalResourceOwner.php b/src/Provider/PaypalResourceOwner.php new file mode 100644 index 0000000..8167ab3 --- /dev/null +++ b/src/Provider/PaypalResourceOwner.php @@ -0,0 +1,193 @@ +response = $response; + } + + /** + * Get user id + * + * @return string|null + */ + public function getId() + { + return $this->response['user_id'] ?: null; + } + + /** + * Get user name + * + * @return string|null + */ + public function getName() + { + return $this->response['name'] ?: null; + } + + /** + * Get user given name + * + * @return string|null + */ + public function getGivenName() + { + return $this->response['given_name'] ?: null; + } + + /** + * Get user family name + * + * @return string|null + */ + public function getFamilyName() + { + return $this->response['family_name'] ?: null; + } + + /** + * Get user email + * + * @return string|null + */ + public function getEmail() + { + return $this->response['email'] ?: null; + } + + /** + * Checks if user is verified + * + * @return boolean + */ + public function isVerified() + { + return $this->response['verified'] ?: false; + } + + /** + * Get user gender + * + * @return string|null + */ + public function getGender() + { + return $this->response['gender'] ?: null; + } + + /** + * Get user birthdate + * + * @return string|null + */ + public function getBirthdate() + { + return $this->response['birthdate'] ?: null; + } + + /** + * Get user zoneinfo + * + * @return string|null + */ + public function getZoneinfo() + { + return $this->response['zoneinfo'] ?: null; + } + + /** + * Get user locale + * + * @return string|null + */ + public function getLocale() + { + return $this->response['locale'] ?: null; + } + + /** + * Get user phone number + * + * @return string|null + */ + public function getPhoneNumber() + { + return $this->response['phone_number'] ?: null; + } + + /** + * Get user address + * + * @return array + */ + public function getAddress() + { + return $this->response['address'] ?: []; + } + + /** + * Checks if user has verified account + * + * @return boolean + */ + public function isVerifiedAccount() + { + return $this->response['verified_account'] ?: false; + } + + /** + * Get user account type + * + * @return string|null + */ + public function getAccountType() + { + return $this->response['account_type'] ?: null; + } + + /** + * Get user age range + * + * @return string|null + */ + public function getAgeRange() + { + return $this->response['age_range'] ?: null; + } + + /** + * Get user payer id + * + * @return string|null + */ + public function getPayerId() + { + return $this->response['payer_id'] ?: null; + } + + /** + * Return all of the owner details available as an array. + * + * @return array + */ + public function toArray() + { + return $this->response; + } +} diff --git a/tests/src/Provider/PaypalTest.php b/tests/src/Provider/PaypalTest.php new file mode 100644 index 0000000..8c852f8 --- /dev/null +++ b/tests/src/Provider/PaypalTest.php @@ -0,0 +1,214 @@ +provider = new \Stevenmaguire\OAuth2\Client\Provider\Paypal([ + 'clientId' => 'mock_client_id', + 'clientSecret' => 'mock_secret', + 'redirectUri' => 'none', + ]); + } + + public function tearDown() + { + m::close(); + parent::tearDown(); + } + + public function testAuthorizationUrl() + { + $url = $this->provider->getAuthorizationUrl(); + $uri = parse_url($url); + parse_str($uri['query'], $query); + + $this->assertArrayHasKey('client_id', $query); + $this->assertArrayHasKey('redirect_uri', $query); + $this->assertArrayHasKey('state', $query); + $this->assertArrayHasKey('scope', $query); + $this->assertArrayHasKey('response_type', $query); + $this->assertArrayHasKey('approval_prompt', $query); + $this->assertNotNull($this->provider->getState()); + } + + + public function testScopes() + { + $options = ['scope' => [uniqid(),uniqid()]]; + + $url = $this->provider->getAuthorizationUrl($options); + + $this->assertContains(urlencode(implode(' ', $options['scope'])), $url); + } + + public function testDefaultScopes() + { + $url = $this->provider->getAuthorizationUrl(); + + $this->assertContains('openid', $url); + } + + public function testSandbox() + { + $provider = new \Stevenmaguire\OAuth2\Client\Provider\Paypal([ + 'clientId' => 'mock_client_id', + 'clientSecret' => 'mock_secret', + 'redirectUri' => 'none', + 'isSandbox' => true, + ]); + + $authUrl = $provider->getAuthorizationUrl(); + $tokenUrl = $provider->getBaseAccessTokenUrl([]); + + $this->assertContains('https://www.sandbox.paypal.com', $authUrl); + $this->assertContains('https://api.sandbox.paypal.com', $tokenUrl); + } + + public function testGetAuthorizationUrl() + { + $url = $this->provider->getAuthorizationUrl(); + $uri = parse_url($url); + + $this->assertEquals('www.paypal.com', $uri['host']); + $this->assertEquals('/webapps/auth/protocol/openidconnect/v1/authorize', $uri['path']); + } + + public function testGetBaseAccessTokenUrl() + { + $params = []; + + $url = $this->provider->getBaseAccessTokenUrl($params); + $uri = parse_url($url); + + $this->assertEquals('api.paypal.com', $uri['host']); + $this->assertEquals('/v1/identity/openidconnect/tokenservice', $uri['path']); + } + + public function testGetAccessToken() + { + $response = m::mock('Psr\Http\Message\ResponseInterface'); + $response->shouldReceive('getBody')->andReturn('{"access_token": "mock_access_token", "token_type":"bearer", "expires_in":3600, "refresh_token":"mock_refresh_token"}'); + $response->shouldReceive('getHeader')->andReturn(['content-type' => 'json']); + $response->shouldReceive('getStatusCode')->andReturn(200); + + $client = m::mock('GuzzleHttp\ClientInterface'); + $client->shouldReceive('send')->times(1)->andReturn($response); + $this->provider->setHttpClient($client); + + $token = $this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']); + + $this->assertEquals('mock_access_token', $token->getToken()); + $this->assertLessThanOrEqual(time() + 3600, $token->getExpires()); + $this->assertGreaterThanOrEqual(time(), $token->getExpires()); + $this->assertEquals('mock_refresh_token', $token->getRefreshToken()); + $this->assertNull($token->getResourceOwnerId()); + } + + public function testUserData() + { + $userId = rand(1000,9999); + $name = uniqid(); + $given_name = uniqid(); + $family_name = uniqid(); + $email = uniqid(); + $verified = (bool) rand(0,1); + $gender = uniqid(); + $birthdate = uniqid(); + $zoneinfo = uniqid(); + $locale = uniqid(); + $phone_number = uniqid(); + $verified_account = (bool) rand(0,1); + $account_type = uniqid(); + $age_range = uniqid(); + $payer_id = uniqid(); + + + $postResponse = m::mock('Psr\Http\Message\ResponseInterface'); + $postResponse->shouldReceive('getBody')->andReturn('{"access_token": "mock_access_token", "token_type":"bearer", "expires_in":3600, "refresh_token":"mock_refresh_token"}'); + $postResponse->shouldReceive('getHeader')->andReturn(['content-type' => 'json']); + $postResponse->shouldReceive('getStatusCode')->andReturn(200); + + $userResponse = m::mock('Psr\Http\Message\ResponseInterface'); + $userResponse->shouldReceive('getBody')->andReturn('{"user_id": "'.$userId.'","name": "'.$name.'","given_name": "'.$given_name.'","family_name": "'.$family_name.'","email": "'.$email.'","verified": '.($verified ? 'true' : 'false').',"gender": "'.$gender.'","birthdate": "'.$birthdate.'","zoneinfo": "'.$zoneinfo.'","locale": "'.$locale.'","phone_number": "'.$phone_number.'","address": { },"verified_account": '.($verified_account ? 'true' : 'false').',"account_type": "'.$account_type.'","age_range": "'.$age_range.'","payer_id": "'.$payer_id.'"}'); + $userResponse->shouldReceive('getHeader')->andReturn(['content-type' => 'json']); + $userResponse->shouldReceive('getStatusCode')->andReturn(200); + + $client = m::mock('GuzzleHttp\ClientInterface'); + $client->shouldReceive('send') + ->times(2) + ->andReturn($postResponse, $userResponse); + $this->provider->setHttpClient($client); + + $token = $this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']); + $user = $this->provider->getResourceOwner($token); + + $this->assertEquals($userId, $user->getId()); + $this->assertEquals($userId, $user->toArray()['user_id']); + $this->assertEquals($name, $user->getName()); + $this->assertEquals($name, $user->toArray()['name']); + $this->assertEquals($given_name, $user->getGivenName()); + $this->assertEquals($given_name, $user->toArray()['given_name']); + $this->assertEquals($family_name, $user->getFamilyName()); + $this->assertEquals($family_name, $user->toArray()['family_name']); + $this->assertEquals($email, $user->getEmail()); + $this->assertEquals($email, $user->toArray()['email']); + + if ($verified) { + $this->assertTrue($user->isVerified()); + $this->assertTrue($user->toArray()['verified']); + } else { + $this->assertFalse($user->isVerified()); + $this->assertFalse($user->toArray()['verified']); + } + + $this->assertEquals($gender, $user->getGender()); + $this->assertEquals($gender, $user->toArray()['gender']); + $this->assertEquals($birthdate, $user->getBirthdate()); + $this->assertEquals($birthdate, $user->toArray()['birthdate']); + $this->assertEquals($zoneinfo, $user->getZoneinfo()); + $this->assertEquals($zoneinfo, $user->toArray()['zoneinfo']); + $this->assertEquals($locale, $user->getLocale()); + $this->assertEquals($locale, $user->toArray()['locale']); + $this->assertEquals($phone_number, $user->getPhoneNumber()); + $this->assertEquals($phone_number, $user->toArray()['phone_number']); + $this->assertTrue(is_array($user->getAddress())); + + if ($verified_account) { + $this->assertTrue($user->isVerifiedAccount()); + $this->assertTrue($user->toArray()['verified_account']); + } else { + $this->assertFalse($user->isVerifiedAccount()); + $this->assertFalse($user->toArray()['verified_account']); + } + + $this->assertEquals($account_type, $user->getAccountType()); + $this->assertEquals($account_type, $user->toArray()['account_type']); + $this->assertEquals($age_range, $user->getAgeRange()); + $this->assertEquals($age_range, $user->toArray()['age_range']); + $this->assertEquals($payer_id, $user->getPayerId()); + $this->assertEquals($payer_id, $user->toArray()['payer_id']); + } + + /** + * @expectedException League\OAuth2\Client\Provider\Exception\IdentityProviderException + **/ + public function testExceptionThrownWhenErrorObjectReceived() + { + $status = rand(401,599); + $postResponse = m::mock('Psr\Http\Message\ResponseInterface'); + $postResponse->shouldReceive('getBody')->andReturn('{"name": "mock_error_name","message": "mock_error_message","information_link": "mock_error_link","details": "mock_error_details"}'); + $postResponse->shouldReceive('getHeader')->andReturn(['content-type' => 'json']); + $postResponse->shouldReceive('getStatusCode')->andReturn($status); + $client = m::mock('GuzzleHttp\ClientInterface'); + $client->shouldReceive('send') + ->times(1) + ->andReturn($postResponse); + $this->provider->setHttpClient($client); + $token = $this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']); + } +}