Skip to content

Commit

Permalink
Improve testing of cache behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
pelmered committed Sep 18, 2024
1 parent 78c5570 commit c9d48cc
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 33 deletions.
15 changes: 12 additions & 3 deletions tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
*/

use Carbon\Carbon;
use Pelmered\LaravelHttpOAuthHelper\AccessToken;

expect()->extend('toBeOne', function () {
return $this->toBe(1);
Expand All @@ -49,10 +50,18 @@ function something()
{

}
function isSameAccessToken($accessToken1, $accessToken2)
function isSameAccessToken($accessToken1, $accessToken2, int $tolerableExpiryDiff = 0)
{
expect($accessToken1->getAccessToken())->toBe($accessToken2->getAccessToken())
->and($accessToken1->getExpiresIn())->toBeWithin($accessToken1->getExpiresIn(), 10)
expect($accessToken1)->toBeInstanceOf(AccessToken::class)
->and($accessToken2)->toBeInstanceOf(AccessToken::class)
->and($accessToken1->getAccessToken())->toBe($accessToken2->getAccessToken())
->and($accessToken1->getTokenType())->toBe($accessToken2->getTokenType())
->and($accessToken1->getExpiresAt())->toBeInstanceOf(Carbon::class)
->and($accessToken1->getCustomCallback())->toBe($accessToken1->getCustomCallback());

$tolerableExpiryDiff >= 0
? expect($accessToken1->getExpiresIn())->toBe($accessToken1->getExpiresIn())
->and($accessToken1->getExpiresAt()->getPreciseTimestamp(6))->toBe($accessToken2->getExpiresAt()->getPreciseTimestamp(6))
: expect($accessToken1->getExpiresIn())->toBeWithin($accessToken1->getExpiresIn(), $tolerableExpiryDiff)
->and($accessToken1->getExpiresAt()->timestamp)->toBeWithin($accessToken2->getExpiresAt()->timestamp, $tolerableExpiryDiff);
}
118 changes: 88 additions & 30 deletions tests/Unit/TokenStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,32 @@

uses(\Pelmered\LaravelHttpOAuthHelper\Tests\TestCase::class);

use Carbon\Carbon;
use Illuminate\Cache\ArrayStore;
use Illuminate\Cache\FileStore;
use Illuminate\Support\Facades\Cache;
use Orchestra\Testbench\Attributes\DefineEnvironment;
use Pelmered\LaravelHttpOAuthHelper\AccessToken;
use Pelmered\LaravelHttpOAuthHelper\Credentials;
use Pelmered\LaravelHttpOAuthHelper\Options;
use Pelmered\LaravelHttpOAuthHelper\TokenStore;

beforeEach(function () {
//DefineEnvironment::

});

it('reads and stores a token in cache be default', function () {

Cache::clear();

/** @var Carbon $nowDate */
$nowDate = Carbon::create(2024, 11, 11, 11);
$cacheKey = 'oauth_token_example.com-oauth-token';

Carbon::setTestNow($nowDate);
$cacheManager = app('cache');
$cacheManagerSpy = Mockery::spy($cacheManager);
Cache::swap($cacheManagerSpy);

$cacheBefore = Cache::get('oauth_token_example.comoauthtoken');
$cacheRepository = Cache::driver(); // or: $cacheRepository = app('cache.store');
$cacheRepositorySpy = Mockery::spy($cacheRepository);
Cache::swap($cacheRepositorySpy);

Cache::shouldReceive('store')->andReturnSelf();
$cacheRepositorySpy->shouldReceive('put')->times(1);
$cacheRepositorySpy->shouldReceive('get')
->with($cacheKey)
->times(1)
->andReturn(null);

$accessToken1 = TokenStore::get(
'https://example.com/oauth/token',
Expand All @@ -35,9 +37,10 @@
),
);

$cacheAfterOne = Cache::get('oauth_token_example.comoauthtoken');

Carbon::setTestNow($nowDate->addHour());
$cacheRepositorySpy->shouldReceive('get')
->with($cacheKey)
->times(2)
->andReturn($accessToken1);

$accessToken2 = TokenStore::get(
'https://example.com/oauth/token',
Expand All @@ -46,23 +49,80 @@
scopes: ['scope1', 'scope2'],
),
);
$accessToken3 = TokenStore::get(
'https://example.com/oauth/token',
new Credentials('my_token'),
new Options(
scopes: ['scope1', 'scope2'],
),
);

expect($cacheBefore)->toBeNull();
isSameAccessToken($accessToken1, $accessToken2, 0);
isSameAccessToken($accessToken1, $accessToken3, 0);
});

isSameAccessToken($accessToken1, $cacheAfterOne);
it('reads and stores a token in cache with custom cache key and driver', function () {

isSameAccessToken($accessToken1, $accessToken2);
Cache::clear();

});
$cacheStore = 'file';
$cacheKey = 'custom_cache_key';

it('reads and stores a token in cache with custom cache driver', function () {
$cacheManager = app('cache');
$cacheManagerSpy = Mockery::spy($cacheManager);
Cache::swap($cacheManagerSpy);

Cache::clear();
//Cache::spy();
$cacheRepository = Cache::driver($cacheStore); // or: $cacheRepository = app('cache.store');
$cacheRepositorySpy = Mockery::spy($cacheRepository);
Cache::swap($cacheRepositorySpy);

//FileStore::
Cache::shouldReceive('store')->with($cacheStore)->andReturnSelf();
$cacheRepositorySpy->shouldReceive('put')
->with($cacheKey, Mockery::type(AccessToken::class), Mockery::any())
->times(1);
$cacheRepositorySpy->shouldReceive('get')
->with($cacheKey)
->times(1)
->andReturn(null);

//Cache::shouldReceive('get')->once()->with('oauth_token_example.comoauthtoken')->andReturn(null);
$accessToken1 = TokenStore::get(
'https://example.com/oauth/token',
new Credentials(
clientId: 'this_is_my_client_id',
clientSecret: 'this_is_my_client_secret',
),
new Options(
scopes: ['scope1', 'scope2'],
authType: Credentials::AUTH_TYPE_BASIC,
cacheKey: $cacheKey,
cacheDriver: $cacheStore
),
);

$cacheRepositorySpy->shouldReceive('get')
->with($cacheKey)
->times(1)
->andReturn($accessToken1);

$accessToken2 = TokenStore::get(
'https://example.com/oauth/token',
new Credentials(
clientId: 'this_is_my_client_id',
clientSecret: 'this_is_my_client_secret',
),
new Options(
scopes: ['scope1', 'scope2'],
authType: Credentials::AUTH_TYPE_BASIC,
cacheKey: $cacheKey,
cacheDriver: $cacheStore
),
);

isSameAccessToken($accessToken1, $accessToken2);
});
it('reads and stores a token in cache with custom cache driver2', function () {

Cache::clear();

$cacheStore = 'file';
$cacheKey = 'custom_cache_key';
Expand All @@ -85,9 +145,7 @@
$accessToken2 = Cache::store($cacheStore)->get($cacheKey);
$accessToken3 = Cache::store($cacheStoreNotUsed)->get($cacheKey);

expect($accessToken->getAccessToken())->toBe($accessToken2->getAccessToken())
->and($accessToken->getExpiresIn())->toBe($accessToken2->getExpiresIn())
->and($accessToken->getTokenType())->toBe($accessToken2->getTokenType())
->and($accessToken->getTokenName())->toBe($accessToken2->getTokenName())
->and($accessToken3)->toBeNull();
isSameAccessToken($accessToken, $accessToken2);

expect($accessToken3)->toBeNull();
});

0 comments on commit c9d48cc

Please sign in to comment.