Skip to content

Commit

Permalink
Pass null for cache parameter to disable caching
Browse files Browse the repository at this point in the history
  • Loading branch information
jlevers committed Jun 24, 2024
1 parent 285ab2f commit f77901d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/SellingPartnerApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -330,13 +330,18 @@ protected function createOAuthAuthenticator(string $accessToken, ?DateTimeImmuta

protected function getCacheableAuthenticator(string $key, callable $refetch): AccessTokenAuthenticator
{
$cached = $this->cache->get($key);
if ($cached) {
return $cached;
if ($this->cache) {
$cached = $this->cache->get($key);
if ($cached) {
return $cached;
}
}

$fetched = $refetch();
$this->cache->set($key, $fetched);

if ($this->cache) {
$this->cache->set($key, $fetched);
}

return $fetched;
}
Expand Down
27 changes: 27 additions & 0 deletions tests/AuthenticationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -388,4 +388,31 @@ public function testFetchesTokenFromCache(): void
// Make sure different dataElements sets result in different RDTs
$this->assertNotEquals($dataElementsRdtAuthenticator1->accessToken, $dataElementsRdtAuthenticator2->accessToken);
}

public function testWorksWithNoCache(): void
{
$faker = Faker\Factory::create();
$mockClient = new MockClient([
GetAccessTokenRequest::class => fn () => MockResponse::make([
'access_token' => $faker->unique()->asciify('********************************************'),
'token_type' => 'bearer',
'expires_in' => 3600,
'refresh_token' => 'refresh-token',
]),
]);

$connector = SellingPartnerApi::seller(
clientId: 'client-id',
clientSecret: 'client-secret',
refreshToken: 'refresh-token',
endpoint: Endpoint::NA_SANDBOX,
cache: null,
);
$connector->withMockClient($mockClient);

$authenticator1 = $connector->defaultAuth();
$authenticator2 = $connector->defaultAuth();

$this->assertNotEquals($authenticator1->accessToken, $authenticator2->accessToken);
}
}

0 comments on commit f77901d

Please sign in to comment.