Skip to content

Commit

Permalink
Add tests for setting cache_handler
Browse files Browse the repository at this point in the history
  • Loading branch information
joshcanhelp committed Dec 3, 2019
1 parent 1193b91 commit dad0170
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
7 changes: 3 additions & 4 deletions src/Auth0.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ class Auth0
* leave empty to default to SessionStore
* - transient_store (Mixed) Optional. StorageInterface for transient auth data;
* leave empty to default to CookieStore
* - cache_handler (Mixed) Optional. A class that implements CacheInterface of false for none
* - cache_handler (Mixed) Optional. CacheInterface instance or false for none
* - persist_user (Boolean) Optional. Persist the user info, default true
* - persist_access_token (Boolean) Optional. Persist the access token, default false
* - persist_refresh_token (Boolean) Optional. Persist the refresh token, default false
Expand Down Expand Up @@ -310,9 +310,8 @@ public function __construct(array $config)

$this->transientHandler = new TransientStoreHandler( $transientStore );

if (isset($config['cache_handler']) && $config['cache_handler'] instanceof CacheInterface) {
$this->cacheHandler = $config['cache_handler'];
} else {
$this->cacheHandler = $config['cache_handler'] ?? null;
if (! $this->cacheHandler instanceof CacheInterface) {
$this->cacheHandler = new NoCacheHandler();
}

Expand Down
34 changes: 34 additions & 0 deletions tests/Auth0Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
use Auth0\SDK\Store\SessionStore;
use Auth0\Tests\Helpers\Tokens\SymmetricVerifierTest;
use Auth0\Tests\Traits\ErrorHelpers;
use Cache\Adapter\PHPArray\ArrayCachePool;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use GuzzleHttp\Psr7\Response;
use PHPUnit\Exception;
use PHPUnit\Framework\TestCase;

/**
Expand Down Expand Up @@ -624,6 +626,38 @@ public function testThatIdTokenLeewayFromConstructorIsUsed()
$this->assertEquals( $id_token, $auth0->getIdToken() );
}

public function testThatCacheHandlerCanBeSet()
{
$request_history = [];
$mock = new MockHandler([
new Response( 200, [ 'Content-Type' => 'application/json' ], '{"keys":[{"kid":"abc","x5c":["123"]}]}' ),
]);
$handler = HandlerStack::create($mock);
$handler->push( Middleware::history($request_history) );

$pool = new ArrayCachePool();
$auth0 = new Auth0([
'domain' => 'test.auth0.com',
'client_id' => uniqid(),
'redirect_uri' => uniqid(),
'cache_handler' => $pool,
'guzzle_options' => [
'handler' => $handler
]
]);

try {
@$auth0->setIdToken(uniqid());
} catch ( \Exception $e ) {
// ...
}

$stored_jwks = $pool->get(md5('https://test.auth0.com/.well-known/jwks.json'));

$this->assertArrayHasKey('abc', $stored_jwks);
$this->assertEquals("-----BEGIN CERTIFICATE-----\n123\n-----END CERTIFICATE-----\n", $stored_jwks['abc']);
}

/*
* Test helper methods.
*/
Expand Down

0 comments on commit dad0170

Please sign in to comment.