From b0afe526f67f49da87469f31817a888a2a4fc4dc Mon Sep 17 00:00:00 2001 From: beau-ottens Date: Wed, 13 Dec 2023 15:48:31 +0100 Subject: [PATCH 1/6] [DV-6720] Upgraded client to PHP ^8.2 and updated related symfony packages/contracts --- composer.json | 10 +++--- src/AccessToken.php | 19 ++--------- src/AuthZeroAuthenticatingHttpClient.php | 43 ++++-------------------- src/AuthZeroConfiguration.php | 34 ++++--------------- 4 files changed, 21 insertions(+), 85 deletions(-) diff --git a/composer.json b/composer.json index 51110f0..b7ce3eb 100755 --- a/composer.json +++ b/composer.json @@ -10,14 +10,14 @@ } ], "require": { - "php": "^7.1.3 || ^8.1", - "symfony/cache": "^4.2 || ^5.0 || ^6.1", - "symfony/http-client-contracts": "^1.1 || ^2 || ^3.3" + "php": "^8.2", + "symfony/cache": "^6.4", + "symfony/http-client-contracts": "^3.4" }, "require-dev": { "friendsofphp/php-cs-fixer": "^2.15", - "phpunit/phpunit": "^8.3", - "symfony/http-client": "^4.3" + "phpunit/phpunit": "^8.5", + "symfony/http-client": "^6.4" }, "suggest": { "symfony/http-client": "This package requires an actual Symfony HTTP client implementation to decorate." diff --git a/src/AccessToken.php b/src/AccessToken.php index 601bea4..982dccb 100755 --- a/src/AccessToken.php +++ b/src/AccessToken.php @@ -7,25 +7,10 @@ * * @author Niels Nijens */ -class AccessToken +readonly class AccessToken { - /** - * @var string - */ - private $token; - - /** - * @var int - */ - private $ttl; - - /** - * Constructs a new AccessToken instance. - */ - public function __construct(string $token, int $ttl) + public function __construct(private string $token, private int $ttl) { - $this->token = $token; - $this->ttl = $ttl; } public function getToken(): string diff --git a/src/AuthZeroAuthenticatingHttpClient.php b/src/AuthZeroAuthenticatingHttpClient.php index 0db1d6b..4f869b4 100755 --- a/src/AuthZeroAuthenticatingHttpClient.php +++ b/src/AuthZeroAuthenticatingHttpClient.php @@ -3,11 +3,11 @@ namespace Superbrave\AuthZeroHttpClient; use Symfony\Component\Cache\Adapter\ArrayAdapter; +use Symfony\Component\HttpClient\DecoratorTrait; use Symfony\Contracts\Cache\CacheInterface; use Symfony\Contracts\Cache\ItemInterface; use Symfony\Contracts\HttpClient\HttpClientInterface; use Symfony\Contracts\HttpClient\ResponseInterface; -use Symfony\Contracts\HttpClient\ResponseStreamInterface; /** * Handles authentication with Auth0 before actual requests are made. @@ -16,37 +16,16 @@ */ class AuthZeroAuthenticatingHttpClient implements HttpClientInterface { - /** - * @var HttpClientInterface - */ - private $client; - - /** - * @var AuthZeroConfiguration - */ - private $authZeroConfiguration; + use DecoratorTrait; - /** - * @var CacheInterface - */ - private $accessTokenCache; - - /** - * Constructs a new AuthZeroAuthenticatingHttpClient instance. - */ public function __construct( - HttpClientInterface $client, - AuthZeroConfiguration $authZeroConfiguration, - CacheInterface $accessTokenCache = null + private HttpClientInterface $client, + private readonly AuthZeroConfiguration $authZeroConfiguration, + private ArrayAdapter|CacheInterface|null $accessTokenCache = null, ) { - $this->client = $client; - $this->authZeroConfiguration = $authZeroConfiguration; - - if ($accessTokenCache === null) { - $accessTokenCache = new ArrayAdapter(); + if ($this->accessTokenCache === null) { + $this->accessTokenCache = new ArrayAdapter(); } - - $this->accessTokenCache = $accessTokenCache; } /** @@ -61,14 +40,6 @@ public function request(string $method, string $url, array $options = []): Respo return $this->client->request($method, $url, $options); } - /** - * {@inheritdoc} - */ - public function stream($responses, float $timeout = null): ResponseStreamInterface - { - return $this->client->stream($responses, $timeout); - } - /** * Appends the 'auth_bearer' option with the retrieved access token from Auth0. */ diff --git a/src/AuthZeroConfiguration.php b/src/AuthZeroConfiguration.php index 6cf924e..6621b39 100755 --- a/src/AuthZeroConfiguration.php +++ b/src/AuthZeroConfiguration.php @@ -8,28 +8,8 @@ * * @author Niels Nijens */ -class AuthZeroConfiguration +readonly class AuthZeroConfiguration { - /** - * @var string - */ - private $tenantUri; - - /** - * @var string - */ - private $clientId; - - /** - * @var string - */ - private $clientSecret; - - /** - * @var string - */ - private $audience; - /** * Constructs a new AuthZeroConfiguration instance. * @@ -38,12 +18,12 @@ class AuthZeroConfiguration * @param string $clientSecret Your application's Client Secret * @param string $audience The unique identifier of the target API you want to access */ - public function __construct(string $tenantUri, string $clientId, string $clientSecret, string $audience) - { - $this->tenantUri = $tenantUri; - $this->clientId = $clientId; - $this->clientSecret = $clientSecret; - $this->audience = $audience; + public function __construct( + private string $tenantUri, + private string $clientId, + private string $clientSecret, + private string $audience + ) { } public function getAudience(): string From 05a147cf20d88385d85972273a188a9e3b8346a8 Mon Sep 17 00:00:00 2001 From: beau-ottens Date: Thu, 14 Dec 2023 09:54:35 +0100 Subject: [PATCH 2/6] [DV-6720] Upgrade php version --- .scrutinizer.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.scrutinizer.yml b/.scrutinizer.yml index b577bec..7cc0c12 100755 --- a/.scrutinizer.yml +++ b/.scrutinizer.yml @@ -41,7 +41,7 @@ build: environment: php: - version: '7.4' + version: '8.2' build_failure_conditions: - 'elements.rating(<= D).new.exists' # No new classes/methods with a rating of D or worse. From 4cdf9301fecf038fc0429392b22dd7d03e88199a Mon Sep 17 00:00:00 2001 From: An Date: Wed, 3 Jan 2024 13:26:10 +0100 Subject: [PATCH 3/6] [DV-6720] Fix potential not existing method error --- src/AuthZeroAuthenticatingHttpClient.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/AuthZeroAuthenticatingHttpClient.php b/src/AuthZeroAuthenticatingHttpClient.php index 4f869b4..77b3ae5 100755 --- a/src/AuthZeroAuthenticatingHttpClient.php +++ b/src/AuthZeroAuthenticatingHttpClient.php @@ -19,7 +19,7 @@ class AuthZeroAuthenticatingHttpClient implements HttpClientInterface use DecoratorTrait; public function __construct( - private HttpClientInterface $client, + private readonly HttpClientInterface $client, private readonly AuthZeroConfiguration $authZeroConfiguration, private ArrayAdapter|CacheInterface|null $accessTokenCache = null, ) { @@ -63,6 +63,10 @@ private function getAccessTokenFromCache(): ?AccessToken // Replace invalid cache key characters with an underscore. $cacheKey = preg_replace('#[\{\}\(\)\/\\\@:]+#', '_', $this->authZeroConfiguration->getAudience()); + if (!$this->accessTokenCache instanceof CacheInterface) { + return null; + } + return $this->accessTokenCache->get( $cacheKey, function (ItemInterface $item) { From 259d82ebaa29ae27a3be196d877beeee0cdf9e9a Mon Sep 17 00:00:00 2001 From: An Date: Wed, 3 Jan 2024 13:58:57 +0100 Subject: [PATCH 4/6] [DV-6720] Fix phpcs phpunit packages to match php version --- .php_cs.dist => .php-cs-fixer.dist.php | 6 ++++-- composer.json | 4 ++-- src/AuthZeroConfiguration.php | 6 +++--- tests/AuthZeroAuthenticatingHttpClientTest.php | 3 +-- 4 files changed, 10 insertions(+), 9 deletions(-) rename .php_cs.dist => .php-cs-fixer.dist.php (60%) diff --git a/.php_cs.dist b/.php-cs-fixer.dist.php similarity index 60% rename from .php_cs.dist rename to .php-cs-fixer.dist.php index acb275a..b15fc94 100755 --- a/.php_cs.dist +++ b/.php-cs-fixer.dist.php @@ -1,13 +1,15 @@ exclude('vendor') ->in(__DIR__); -return PhpCsFixer\Config::create() +return (new Config()) ->setRules([ '@Symfony' => true, - 'yoda_style' => null, // Do not enforce Yoda style (add unit tests instead...) + 'yoda_style' => false, // Do not enforce Yoda style (add unit tests instead...) 'ordered_imports' => true, ]) ->setFinder($finder); diff --git a/composer.json b/composer.json index b7ce3eb..739d466 100755 --- a/composer.json +++ b/composer.json @@ -15,8 +15,8 @@ "symfony/http-client-contracts": "^3.4" }, "require-dev": { - "friendsofphp/php-cs-fixer": "^2.15", - "phpunit/phpunit": "^8.5", + "friendsofphp/php-cs-fixer": "^3.45", + "phpunit/phpunit": "^10.0", "symfony/http-client": "^6.4" }, "suggest": { diff --git a/src/AuthZeroConfiguration.php b/src/AuthZeroConfiguration.php index 6621b39..2f6f35f 100755 --- a/src/AuthZeroConfiguration.php +++ b/src/AuthZeroConfiguration.php @@ -19,9 +19,9 @@ * @param string $audience The unique identifier of the target API you want to access */ public function __construct( - private string $tenantUri, - private string $clientId, - private string $clientSecret, + private string $tenantUri, + private string $clientId, + private string $clientSecret, private string $audience ) { } diff --git a/tests/AuthZeroAuthenticatingHttpClientTest.php b/tests/AuthZeroAuthenticatingHttpClientTest.php index 36c6a5d..ceccde7 100755 --- a/tests/AuthZeroAuthenticatingHttpClientTest.php +++ b/tests/AuthZeroAuthenticatingHttpClientTest.php @@ -2,7 +2,6 @@ namespace Superbrave\AuthZeroHttpClient\Tests; -use ArrayIterator; use PHPUnit\Framework\TestCase; use Superbrave\AuthZeroHttpClient\AuthZeroAuthenticatingHttpClient; use Superbrave\AuthZeroHttpClient\AuthZeroConfiguration; @@ -44,7 +43,7 @@ class AuthZeroAuthenticatingHttpClientTest extends TestCase */ protected function setUp(): void { - $this->mockResponses = new ArrayIterator(); + $this->mockResponses = new \ArrayIterator(); $this->mockHttpClient = new MockHttpClient($this->mockResponses); $this->authZeroConfiguration = new AuthZeroConfiguration( From f93dd36f83ce6346840a2c2ca24144ef92384146 Mon Sep 17 00:00:00 2001 From: An Date: Wed, 3 Jan 2024 14:04:20 +0100 Subject: [PATCH 5/6] [DV-6720] Fix phpunit to be compatible with php upgrade --- phpunit.xml.dist | 34 +++++++++--------------- src/AuthZeroAuthenticatingHttpClient.php | 4 ++- 2 files changed, 15 insertions(+), 23 deletions(-) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index f9d29e3..2253b4a 100755 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,23 +1,13 @@ - - - - - - ./tests/ - - - - - - ./src/ - - + + + + + ./tests/ + + + + + ./src/ + + diff --git a/src/AuthZeroAuthenticatingHttpClient.php b/src/AuthZeroAuthenticatingHttpClient.php index 77b3ae5..3b80e00 100755 --- a/src/AuthZeroAuthenticatingHttpClient.php +++ b/src/AuthZeroAuthenticatingHttpClient.php @@ -19,13 +19,15 @@ class AuthZeroAuthenticatingHttpClient implements HttpClientInterface use DecoratorTrait; public function __construct( - private readonly HttpClientInterface $client, + HttpClientInterface $client, private readonly AuthZeroConfiguration $authZeroConfiguration, private ArrayAdapter|CacheInterface|null $accessTokenCache = null, ) { if ($this->accessTokenCache === null) { $this->accessTokenCache = new ArrayAdapter(); } + + $this->client = $client; } /** From e44d797846fbcf37d61a030d2ea3afc0c94b23ab Mon Sep 17 00:00:00 2001 From: An Date: Wed, 3 Jan 2024 14:11:27 +0100 Subject: [PATCH 6/6] [DV-6720] Add xdebugmode as phpstan requires it in https://github.com/sebastianbergmann/php-code-coverage/issues/834 --- .scrutinizer.yml | 2 +- phpunit.xml.dist | 30 +++++++++++++++++++----------- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/.scrutinizer.yml b/.scrutinizer.yml index 7cc0c12..21b9c23 100755 --- a/.scrutinizer.yml +++ b/.scrutinizer.yml @@ -32,7 +32,7 @@ build: - command: '~/.symfony/bin/symfony security:check --force-update' only_if: '[ "$SCRUTINIZER_BRANCH" == "master" ] && [ -z "$SCRUTINIZER_PR_SOURCE_BRANCH" ]' - - command: './vendor/bin/phpunit --coverage-clover=coverage-clover.xml' + - command: 'XDEBUG_MODE=coverage ./vendor/bin/phpunit --coverage-clover=coverage-clover.xml --coverage-text' coverage: file: 'coverage-clover.xml' format: 'clover' diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 2253b4a..295260b 100755 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,13 +1,21 @@ - - - - ./tests/ - - - - - ./src/ - - + + + + + ./tests/ + + + + + ./src/ + +