From c9d48ccf08a0a2f4f12b50dc51d75e523246d466 Mon Sep 17 00:00:00 2001 From: Peter Elmered Date: Wed, 18 Sep 2024 15:40:16 +0200 Subject: [PATCH] Improve testing of cache behaviour --- tests/Pest.php | 15 ++++- tests/Unit/TokenStoreTest.php | 118 +++++++++++++++++++++++++--------- 2 files changed, 100 insertions(+), 33 deletions(-) diff --git a/tests/Pest.php b/tests/Pest.php index 22dc778..0980c8b 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -25,6 +25,7 @@ */ use Carbon\Carbon; +use Pelmered\LaravelHttpOAuthHelper\AccessToken; expect()->extend('toBeOne', function () { return $this->toBe(1); @@ -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); } diff --git a/tests/Unit/TokenStoreTest.php b/tests/Unit/TokenStoreTest.php index d83dba6..c451c19 100644 --- a/tests/Unit/TokenStoreTest.php +++ b/tests/Unit/TokenStoreTest.php @@ -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', @@ -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', @@ -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'; @@ -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(); });