mockFetcher = $this ->getMockBuilder('Google\Auth\FetchAuthTokenInterface') ->getMock(); $this->mockCacheItem = $this ->getMockBuilder('Psr\Cache\CacheItemInterface') ->getMock(); $this->mockCache = $this ->getMockBuilder('Psr\Cache\CacheItemPoolInterface') ->getMock(); } public function testUsesCachedAuthToken() { $cacheKey = 'myKey'; $cachedValue = '2/abcdef1234567890'; $this->mockCacheItem ->expects($this->once()) ->method('isHit') ->will($this->returnValue(true)); $this->mockCacheItem ->expects($this->once()) ->method('get') ->will($this->returnValue($cachedValue)); $this->mockCache ->expects($this->once()) ->method('getItem') ->with($this->equalTo($cacheKey)) ->will($this->returnValue($this->mockCacheItem)); $this->mockFetcher ->expects($this->never()) ->method('fetchAuthToken'); $this->mockFetcher ->expects($this->any()) ->method('getCacheKey') ->will($this->returnValue($cacheKey)); // Run the test. $cachedFetcher = new FetchAuthTokenCache( $this->mockFetcher, null, $this->mockCache ); $accessToken = $cachedFetcher->fetchAuthToken(); $this->assertEquals($accessToken, ['access_token' => $cachedValue]); } public function testGetsCachedAuthTokenUsingCachePrefix() { $prefix = 'test_prefix_'; $cacheKey = 'myKey'; $cachedValue = '2/abcdef1234567890'; $this->mockCacheItem ->expects($this->once()) ->method('isHit') ->will($this->returnValue(true)); $this->mockCacheItem ->expects($this->once()) ->method('get') ->will($this->returnValue($cachedValue)); $this->mockCache ->expects($this->once()) ->method('getItem') ->with($this->equalTo($prefix . $cacheKey)) ->will($this->returnValue($this->mockCacheItem)); $this->mockFetcher ->expects($this->never()) ->method('fetchAuthToken'); $this->mockFetcher ->expects($this->any()) ->method('getCacheKey') ->will($this->returnValue($cacheKey)); // Run the test $cachedFetcher = new FetchAuthTokenCache( $this->mockFetcher, ['prefix' => $prefix], $this->mockCache ); $accessToken = $cachedFetcher->fetchAuthToken(); $this->assertEquals($accessToken, ['access_token' => $cachedValue]); } public function testShouldSaveValueInCacheWithCacheOptions() { $prefix = 'test_prefix_'; $lifetime = '70707'; $cacheKey = 'myKey'; $token = '1/abcdef1234567890'; $authResult = ['access_token' => $token]; $this->mockCacheItem ->expects($this->any()) ->method('get') ->will($this->returnValue(null)); $this->mockCacheItem ->expects($this->once()) ->method('set') ->with($this->equalTo($token)) ->will($this->returnValue(false)); $this->mockCacheItem ->expects($this->once()) ->method('expiresAfter') ->with($this->equalTo($lifetime)); $this->mockCache ->expects($this->exactly(2)) ->method('getItem') ->with($this->equalTo($prefix . $cacheKey)) ->will($this->returnValue($this->mockCacheItem)); $this->mockFetcher ->expects($this->any()) ->method('getCacheKey') ->will($this->returnValue($cacheKey)); $this->mockFetcher ->expects($this->once()) ->method('fetchAuthToken') ->will($this->returnValue($authResult)); // Run the test $cachedFetcher = new FetchAuthTokenCache( $this->mockFetcher, ['prefix' => $prefix, 'lifetime' => $lifetime], $this->mockCache ); $accessToken = $cachedFetcher->fetchAuthToken(); $this->assertEquals($accessToken, ['access_token' => $token]); } public function testGetLastReceivedToken() { $token = 'foo'; $mockFetcher = $this->prophesize('Google\Auth\FetchAuthTokenInterface'); $mockFetcher->getLastReceivedToken() ->shouldBeCalled() ->willReturn([ 'access_token' => $token ]); $fetcher = new FetchAuthTokenCache( $mockFetcher->reveal(), [], $this->mockCache ); $this->assertEquals($token, $fetcher->getLastReceivedToken()['access_token']); } public function testGetClientName() { $name = 'test@example.com'; $mockFetcher = $this->prophesize('Google\Auth\SignBlobInterface'); $mockFetcher->getClientName(null) ->shouldBeCalled() ->willReturn($name); $fetcher = new FetchAuthTokenCache( $mockFetcher->reveal(), [], $this->mockCache ); $this->assertEquals($name, $fetcher->getClientName()); } public function testSignBlob() { $stringToSign = 'foobar'; $signature = 'helloworld'; $mockFetcher = $this->prophesize('Google\Auth\SignBlobInterface'); $mockFetcher->willImplement('Google\Auth\FetchAuthTokenInterface'); $mockFetcher->signBlob($stringToSign, true) ->shouldBeCalled() ->willReturn($signature); $fetcher = new FetchAuthTokenCache( $mockFetcher->reveal(), [], $this->mockCache ); $this->assertEquals($signature, $fetcher->signBlob($stringToSign, true)); } /** * @expectedException RuntimeException */ public function testSignBlobInvalidFetcher() { $mockFetcher = $this->prophesize('Google\Auth\FetchAuthTokenInterface'); $mockFetcher->signBlob('test') ->shouldNotbeCalled(); $fetcher = new FetchAuthTokenCache( $mockFetcher->reveal(), [], $this->mockCache ); $this->assertEquals($signature, $fetcher->signBlob('test')); } }