From e4e7f9ff0902f5e923bb4fa32ed4b8a62bc0edb0 Mon Sep 17 00:00:00 2001 From: ursuleacv Date: Wed, 4 Dec 2024 17:13:10 -0500 Subject: [PATCH 1/2] Update .travis.yml --- .travis.yml | 8 -------- 1 file changed, 8 deletions(-) diff --git a/.travis.yml b/.travis.yml index f9ffc3a..f84776c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,14 +6,6 @@ matrix: - php: 7.1 - php: 7.2 - php: nightly - - php: hhvm-3.15 - sudo: required - dist: trusty - group: edge - - php: hhvm-nightly - sudo: required - dist: trusty - group: edge fast_finish: true allow_failures: - php: nightly From 2440d6e031f3aa72d1f0a71d034fabef1e93668a Mon Sep 17 00:00:00 2001 From: ursuleacv Date: Wed, 4 Dec 2024 19:07:18 -0500 Subject: [PATCH 2/2] Update to PHP 8.1 --- .gitignore | 3 +- .travis.yml | 7 ++-- README.md | 15 ++++++-- composer.json | 12 +++--- phpunit.xml | 5 --- rector.php | 18 +++++++++ src/Grant/LsExchangeToken.php | 2 +- src/Provider/Lightspeed.php | 11 +++--- src/Provider/LightspeedResourceOwner.php | 2 +- src/Provider/MerchantOS.php | 26 +++---------- .../Provider/LightspeedResourceOwnerTest.php | 2 +- tests/src/Provider/LightspeedTest.php | 38 +++++++++++++------ 12 files changed, 83 insertions(+), 58 deletions(-) create mode 100644 rector.php diff --git a/.gitignore b/.gitignore index e8c7cb6..f1aa177 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ /build /vendor composer.phar -composer.lock \ No newline at end of file +composer.lock +.phpunit.result.cache diff --git a/.travis.yml b/.travis.yml index f84776c..cbac3d4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,9 +2,10 @@ language: php matrix: include: - - php: 7.0 - - php: 7.1 - - php: 7.2 + - php: 8.1 + - php: 8.2 + - php: 8.3 + - php: 8.4 - php: nightly fast_finish: true allow_failures: diff --git a/README.md b/README.md index 4a7b3af..87f233a 100644 --- a/README.md +++ b/README.md @@ -16,9 +16,10 @@ This package is compliant with [PSR-1][], [PSR-2][], [PSR-4][], and [PSR-7][]. I The following versions of PHP are supported. -* PHP 7.0 -* PHP 7.1 -* PHP 7.2 +* PHP 8.1 +* PHP 8.2 +* PHP 8.3 +* PHP 8.4 * HHVM ## Installation @@ -28,7 +29,7 @@ Add the following to your `composer.json` file. ```json { "require": { - "ursuleacv/oauth2-lightspeed": "~2.0" + "ursuleacv/oauth2-lightspeed": "~3.0" } } ``` @@ -102,6 +103,12 @@ echo ''; $ ./vendor/bin/phpunit ``` +## Upgrading + +```bash +vendor/bin/rector process src --dry-run +``` + ## Contributing Please see [CONTRIBUTING](https://github.com/ursuleacv/oauth2-lightspeed/blob/master/CONTRIBUTING.md) for details. diff --git a/composer.json b/composer.json index d5e4e67..451526d 100644 --- a/composer.json +++ b/composer.json @@ -6,8 +6,7 @@ { "name": "Valentin Ursuleac", "email": "ursuleacv@gmail.com", - "homepage": "http://valnet.ca", - "role": "Developer" + "role": "Software Developer" } ], "keywords": [ @@ -19,16 +18,17 @@ "LightSpeed" ], "require": { - "php": ">=5.6.0", + "php": ">=8.1.0", "ext-curl": "*", "ext-json": "*", "league/oauth2-client": "~2.0", - "guzzlehttp/guzzle": "~6.0" + "guzzlehttp/guzzle": "^7.0" }, "require-dev": { - "phpunit/phpunit": "~6.0", + "phpunit/phpunit": "~10.0", "mockery/mockery": "~1.0", - "squizlabs/php_codesniffer": "^3.4" + "squizlabs/php_codesniffer": "^3.4", + "rector/rector": "^1.2" }, "autoload": { "psr-4": { diff --git a/phpunit.xml b/phpunit.xml index 5d0b9f8..b725677 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,14 +1,9 @@ diff --git a/rector.php b/rector.php new file mode 100644 index 0000000..7290a4c --- /dev/null +++ b/rector.php @@ -0,0 +1,18 @@ +withPaths([ + __DIR__ . '/src', + __DIR__ . '/tests', + ]) + ->withSets([ + PHPUnitSetList::PHPUNIT_100, + ]) + // uncomment to reach your current PHP version + ->withPhpSets(php81: true) + ->withTypeCoverageLevel(0); diff --git a/src/Grant/LsExchangeToken.php b/src/Grant/LsExchangeToken.php index 089b015..0389539 100644 --- a/src/Grant/LsExchangeToken.php +++ b/src/Grant/LsExchangeToken.php @@ -4,7 +4,7 @@ class LsExchangeToken extends AbstractGrant { - public function __toString() + public function __toString(): string { return 'ls_exchange_token'; } diff --git a/src/Provider/Lightspeed.php b/src/Provider/Lightspeed.php index cc0f288..79b6580 100644 --- a/src/Provider/Lightspeed.php +++ b/src/Provider/Lightspeed.php @@ -5,6 +5,7 @@ use InvalidArgumentException; use League\OAuth2\Client\Provider\Exception\IdentityProviderException; use League\OAuth2\Client\Token\AccessToken; +use League\OAuth2\Client\Token\AccessTokenInterface; use Psr\Http\Message\ResponseInterface; class Lightspeed extends AbstractProvider @@ -39,12 +40,12 @@ public function getBaseAuthorizationUrl() * @param array $params * @return string */ - public function getBaseAccessTokenUrl(array $params) + public function getBaseAccessTokenUrl(array $params): string { return static::LIGHTSPEED_TOKEN_ENDPOINT; } - public function getDefaultScopes() + public function getDefaultScopes(): array { return ['employee:all']; } @@ -54,7 +55,7 @@ public function getDefaultScopes() * @param array $params * @return AccessToken */ - public function getAccessToken($grant = 'authorization_code', array $params = []) + public function getAccessToken($grant = 'authorization_code', array $params = []): AccessTokenInterface { return parent::getAccessToken($grant, $params); } @@ -66,7 +67,7 @@ public function getAccessToken($grant = 'authorization_code', array $params = [] * * @return \League\OAuth2\Client\Token\AccessToken */ - public function getLongLivedAccessToken($accessToken) + public function getLongLivedAccessToken($accessToken): AccessTokenInterface { $params = [ 'ls_exchange_token' => (string) $accessToken, @@ -90,7 +91,7 @@ public function getAccountId(AccessToken $token) * @param AccessToken $token * @return mixed */ - public function getResourceOwnerDetailsUrl(AccessToken $token) + public function getResourceOwnerDetailsUrl(AccessToken $token): string { return $this->getBaseLightspeedApiUrl() . 'Account/.json?oauth_token=' . $token; } diff --git a/src/Provider/LightspeedResourceOwner.php b/src/Provider/LightspeedResourceOwner.php index 982774f..ed62215 100644 --- a/src/Provider/LightspeedResourceOwner.php +++ b/src/Provider/LightspeedResourceOwner.php @@ -18,7 +18,7 @@ class LightspeedResourceOwner implements ResourceOwnerInterface * * @param array $response */ - public function __construct(array $response = array()) + public function __construct(array $response = []) { $this->response = $response; } diff --git a/src/Provider/MerchantOS.php b/src/Provider/MerchantOS.php index b67a8ac..364bb92 100644 --- a/src/Provider/MerchantOS.php +++ b/src/Provider/MerchantOS.php @@ -14,11 +14,6 @@ class MerchantOS extends Lightspeed */ private $oauthToken; - /** - * @var mixed - */ - protected $accountId; - /** * @var string */ @@ -50,10 +45,9 @@ class MerchantOS extends Lightspeed * @param AccessToken $token * @param mixed $accountId */ - public function __construct(AccessToken $token, $accountId) + public function __construct(AccessToken $token, protected $accountId) { $this->oauthToken = $token->getToken(); - $this->accountId = $accountId; } /** @@ -1040,11 +1034,7 @@ public function getConfig() { $response = $this->makeAPICall('Account.Config', 'GET', null, [], []); - if (isset($response['Config'])) { - return $response['Config']; - } - - return []; + return $response['Config'] ?? []; } /** @@ -1068,11 +1058,7 @@ public function getLocale() { $response = $this->makeAPICall('Locale', 'GET', null, [], []); - if (isset($response['Locale'])) { - return $response['Locale']; - } - - return []; + return $response['Locale'] ?? []; } /** @@ -1094,7 +1080,7 @@ public function makeAPICall($controlUrl, $action, $uniqueId, $params, $data) $url = $this->prepareApiUrl($controlUrl, $this->accountId, $uniqueId, $params); $this->context['apiCall'] = $url; - $this->context['action'] = strtoupper($action); + $this->context['action'] = strtoupper((string) $action); $headers = [ 'User-Agent' => $this->userAgent, @@ -1175,7 +1161,7 @@ protected function buildQueryString(array $data) } else { $qs = ''; foreach ($data as $key => $value) { - $append = urlencode($key) . '=' . urlencode($value); + $append = urlencode($key) . '=' . urlencode((string) $value); $qs .= $qs ? '&' . $append : $append; } return $qs; @@ -1231,7 +1217,7 @@ protected function sleepIfNecessary() $bucketLevelStr = $headers['X-LS-API-Bucket-Level'][0]; - list($currentLevel, $bucketSize) = explode('/', $bucketLevelStr); + [$currentLevel, $bucketSize] = explode('/', (string) $bucketLevelStr); //The drip rate is calculated by dividing the bucket size by 60 seconds. $dripRate = $bucketSize / 60; diff --git a/tests/src/Provider/LightspeedResourceOwnerTest.php b/tests/src/Provider/LightspeedResourceOwnerTest.php index 8336833..8bea773 100644 --- a/tests/src/Provider/LightspeedResourceOwnerTest.php +++ b/tests/src/Provider/LightspeedResourceOwnerTest.php @@ -11,7 +11,7 @@ class LightspeedResourceOwnerTest extends \PHPUnit\Framework\TestCase */ protected $account; - protected function setUp() + protected function setUp(): void { $arr = [ '[@attributes' => [ diff --git a/tests/src/Provider/LightspeedTest.php b/tests/src/Provider/LightspeedTest.php index 34e34f0..fb75f6f 100644 --- a/tests/src/Provider/LightspeedTest.php +++ b/tests/src/Provider/LightspeedTest.php @@ -4,6 +4,7 @@ use League\OAuth2\Client\Provider\Lightspeed; use League\OAuth2\Client\Token\AccessToken; +use Psr\Http\Message\StreamInterface; use Mockery as m; class FooLightspeedProvider extends Lightspeed @@ -22,7 +23,7 @@ class LightspeedTest extends \PHPUnit\Framework\TestCase */ protected $provider; - protected function setUp() + protected function setUp(): void { $this->provider = new Lightspeed([ 'clientId' => 'mock_client_id', @@ -31,7 +32,7 @@ protected function setUp() ]); } - public function tearDown() + public function tearDown(): void { m::close(); parent::tearDown(); @@ -62,15 +63,23 @@ public function testGetBaseAccessTokenUrl() public function testGetAccessToken() { - $response = m::mock('Psr\Http\Message\ResponseInterface'); + $stream = $this->createMock(StreamInterface::class); + $stream + ->method('__toString') + ->willReturn('{"access_token":"mock_access_token","token_type":"bearer","expires_in":3600}'); + + $response = m::mock(\Psr\Http\Message\ResponseInterface::class); $response->shouldReceive('getHeader') ->times(1) - ->andReturn('application/json'); + // ->andReturn('application/json'); + ->andReturn(['Content-Type' => 'application/json']); $response->shouldReceive('getBody') ->times(1) - ->andReturn('{"access_token":"mock_access_token","token_type":"bearer","expires_in":3600}'); + // ->andReturn('{"access_token":"mock_access_token","token_type":"bearer","expires_in":3600}'); + // ->andReturn(m::mock(StreamInterface::class, ['getContents' => '{"access_token":"long-lived-token","token_type":"bearer","expires_in":3600}'])); + ->andReturn($stream); - $client = m::mock('GuzzleHttp\ClientInterface'); + $client = m::mock(\GuzzleHttp\ClientInterface::class); $client->shouldReceive('send')->times(1)->andReturn($response); $this->provider->setHttpClient($client); @@ -85,15 +94,22 @@ public function testGetAccessToken() public function testCanGetALongLivedAccessTokenFromShortLivedOne() { - $response = m::mock('Psr\Http\Message\ResponseInterface'); + $stream = $this->createMock(StreamInterface::class); + $stream + ->method('__toString') + ->willReturn('{"access_token":"long-lived-token","token_type":"bearer","expires_in":3600}'); + + $response = m::mock(\Psr\Http\Message\ResponseInterface::class); $response->shouldReceive('getHeader') ->times(1) - ->andReturn('application/json'); + // ->andReturn('application/json'); + ->andReturn(['Content-Type' => 'application/json']); $response->shouldReceive('getBody') ->times(1) - ->andReturn('{"access_token":"long-lived-token","token_type":"bearer","expires_in":3600}'); + // ->andReturn('{"access_token":"long-lived-token","token_type":"bearer","expires_in":3600}'); + ->andReturn($stream); - $client = m::mock('GuzzleHttp\ClientInterface'); + $client = m::mock(\GuzzleHttp\ClientInterface::class); $client->shouldReceive('send')->times(1)->andReturn($response); $this->provider->setHttpClient($client); @@ -111,7 +127,7 @@ public function testAccountData() { $provider = new FooLightspeedProvider(); - $token = m::mock('League\OAuth2\Client\Token\AccessToken'); + $token = m::mock(\League\OAuth2\Client\Token\AccessToken::class); $account = $provider->getResourceOwner($token); $this->assertEquals(12345, $account->getId($token));