No Description

FetchAuthTokenCacheTest.php 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /*
  3. * Copyright 2015 Google Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. namespace Google\Auth\tests;
  18. use Google\Auth\FetchAuthTokenCache;
  19. class FetchAuthTokenCacheTest extends BaseTest
  20. {
  21. protected function setUp()
  22. {
  23. $this->mockFetcher =
  24. $this
  25. ->getMockBuilder('Google\Auth\FetchAuthTokenInterface')
  26. ->getMock();
  27. $this->mockCacheItem =
  28. $this
  29. ->getMockBuilder('Psr\Cache\CacheItemInterface')
  30. ->getMock();
  31. $this->mockCache =
  32. $this
  33. ->getMockBuilder('Psr\Cache\CacheItemPoolInterface')
  34. ->getMock();
  35. }
  36. public function testUsesCachedAuthToken()
  37. {
  38. $cacheKey = 'myKey';
  39. $cachedValue = '2/abcdef1234567890';
  40. $this->mockCacheItem
  41. ->expects($this->once())
  42. ->method('isHit')
  43. ->will($this->returnValue(true));
  44. $this->mockCacheItem
  45. ->expects($this->once())
  46. ->method('get')
  47. ->will($this->returnValue($cachedValue));
  48. $this->mockCache
  49. ->expects($this->once())
  50. ->method('getItem')
  51. ->with($this->equalTo($cacheKey))
  52. ->will($this->returnValue($this->mockCacheItem));
  53. $this->mockFetcher
  54. ->expects($this->never())
  55. ->method('fetchAuthToken');
  56. $this->mockFetcher
  57. ->expects($this->any())
  58. ->method('getCacheKey')
  59. ->will($this->returnValue($cacheKey));
  60. // Run the test.
  61. $cachedFetcher = new FetchAuthTokenCache(
  62. $this->mockFetcher,
  63. null,
  64. $this->mockCache
  65. );
  66. $accessToken = $cachedFetcher->fetchAuthToken();
  67. $this->assertEquals($accessToken, ['access_token' => $cachedValue]);
  68. }
  69. public function testGetsCachedAuthTokenUsingCachePrefix()
  70. {
  71. $prefix = 'test_prefix_';
  72. $cacheKey = 'myKey';
  73. $cachedValue = '2/abcdef1234567890';
  74. $this->mockCacheItem
  75. ->expects($this->once())
  76. ->method('isHit')
  77. ->will($this->returnValue(true));
  78. $this->mockCacheItem
  79. ->expects($this->once())
  80. ->method('get')
  81. ->will($this->returnValue($cachedValue));
  82. $this->mockCache
  83. ->expects($this->once())
  84. ->method('getItem')
  85. ->with($this->equalTo($prefix . $cacheKey))
  86. ->will($this->returnValue($this->mockCacheItem));
  87. $this->mockFetcher
  88. ->expects($this->never())
  89. ->method('fetchAuthToken');
  90. $this->mockFetcher
  91. ->expects($this->any())
  92. ->method('getCacheKey')
  93. ->will($this->returnValue($cacheKey));
  94. // Run the test
  95. $cachedFetcher = new FetchAuthTokenCache(
  96. $this->mockFetcher,
  97. ['prefix' => $prefix],
  98. $this->mockCache
  99. );
  100. $accessToken = $cachedFetcher->fetchAuthToken();
  101. $this->assertEquals($accessToken, ['access_token' => $cachedValue]);
  102. }
  103. public function testShouldSaveValueInCacheWithCacheOptions()
  104. {
  105. $prefix = 'test_prefix_';
  106. $lifetime = '70707';
  107. $cacheKey = 'myKey';
  108. $token = '1/abcdef1234567890';
  109. $authResult = ['access_token' => $token];
  110. $this->mockCacheItem
  111. ->expects($this->any())
  112. ->method('get')
  113. ->will($this->returnValue(null));
  114. $this->mockCacheItem
  115. ->expects($this->once())
  116. ->method('set')
  117. ->with($this->equalTo($token))
  118. ->will($this->returnValue(false));
  119. $this->mockCacheItem
  120. ->expects($this->once())
  121. ->method('expiresAfter')
  122. ->with($this->equalTo($lifetime));
  123. $this->mockCache
  124. ->expects($this->exactly(2))
  125. ->method('getItem')
  126. ->with($this->equalTo($prefix . $cacheKey))
  127. ->will($this->returnValue($this->mockCacheItem));
  128. $this->mockFetcher
  129. ->expects($this->any())
  130. ->method('getCacheKey')
  131. ->will($this->returnValue($cacheKey));
  132. $this->mockFetcher
  133. ->expects($this->once())
  134. ->method('fetchAuthToken')
  135. ->will($this->returnValue($authResult));
  136. // Run the test
  137. $cachedFetcher = new FetchAuthTokenCache(
  138. $this->mockFetcher,
  139. ['prefix' => $prefix, 'lifetime' => $lifetime],
  140. $this->mockCache
  141. );
  142. $accessToken = $cachedFetcher->fetchAuthToken();
  143. $this->assertEquals($accessToken, ['access_token' => $token]);
  144. }
  145. }