You are here

public function JWKFetcherTest::testCacheReturn in Auth0 Single Sign On 8.2

Test that the requestJwkX5c method returns a cached value, if set.

Return value

void

File

vendor/auth0/auth0-php/tests/Helpers/JWKFetcherTest.php, line 188

Class

JWKFetcherTest
Class JWKFetcherTest.

Namespace

Auth0\Tests\Helpers

Code

public function testCacheReturn() {
  $jwks_url = 'https://localhost/.well-known/jwks.json';
  $kid = '__test_kid_2__';
  $cache_value = '__cached_value__';
  $set_spy = $this
    ->once();
  $get_spy = $this
    ->any();

  // Mock the CacheHandler interface.
  $cache_handler = $this
    ->getMockBuilder(CacheHandler::class)
    ->getMock();

  // The set method should only be called once.
  $cache_handler
    ->expects($set_spy)
    ->method('set')
    ->willReturn(null);

  // The get method should be called once and return no cache first, then a cache value after.
  $cache_handler
    ->expects($get_spy)
    ->method('get')
    ->will($this
    ->onConsecutiveCalls(null, $cache_value));
  $jwksFetcher = $this
    ->getStub($cache_handler);
  $pem_not_cached = $jwksFetcher
    ->requestJwkX5c($jwks_url, $kid);
  $this
    ->assertNotEmpty($pem_not_cached);
  $pem_cached = $jwksFetcher
    ->requestJwkX5c($jwks_url, $kid);
  $this
    ->assertEquals($cache_value, $pem_cached);

  // Test that the set method was called with the correct parameters.
  $set_invocations = $set_spy
    ->getInvocations();
  $this
    ->assertEquals($jwks_url . '|' . $kid, $set_invocations[0]->parameters[0]);
  $this
    ->assertEquals($pem_not_cached, $set_invocations[0]->parameters[1]);

  // Test that the get method was only called twice.
  $this
    ->assertEquals(2, $get_spy
    ->getInvocationCount());
}