No Description

CacheTraitTest.php 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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\CacheTrait;
  19. use PHPUnit\Framework\TestCase;
  20. class CacheTraitTest extends TestCase
  21. {
  22. private $mockFetcher;
  23. private $mockCacheItem;
  24. private $mockCache;
  25. public function setUp()
  26. {
  27. $this->mockFetcher =
  28. $this
  29. ->getMockBuilder('Google\Auth\FetchAuthTokenInterface')
  30. ->getMock();
  31. $this->mockCacheItem =
  32. $this
  33. ->getMockBuilder('Psr\Cache\CacheItemInterface')
  34. ->getMock();
  35. $this->mockCache =
  36. $this
  37. ->getMockBuilder('Psr\Cache\CacheItemPoolInterface')
  38. ->getMock();
  39. }
  40. public function testSuccessfullyPullsFromCache()
  41. {
  42. $expectedValue = '1234';
  43. $this->mockCacheItem
  44. ->expects($this->once())
  45. ->method('isHit')
  46. ->will($this->returnValue(true));
  47. $this->mockCacheItem
  48. ->expects($this->once())
  49. ->method('get')
  50. ->will($this->returnValue($expectedValue));
  51. $this->mockCache
  52. ->expects($this->once())
  53. ->method('getItem')
  54. ->will($this->returnValue($this->mockCacheItem));
  55. $implementation = new CacheTraitImplementation([
  56. 'cache' => $this->mockCache,
  57. ]);
  58. $cachedValue = $implementation->gCachedValue();
  59. $this->assertEquals($expectedValue, $cachedValue);
  60. }
  61. public function testSuccessfullyPullsFromCacheWithInvalidKey()
  62. {
  63. $key = 'this-key-has-@-illegal-characters';
  64. $expectedKey = 'thiskeyhasillegalcharacters';
  65. $expectedValue = '1234';
  66. $this->mockCacheItem
  67. ->expects($this->once())
  68. ->method('isHit')
  69. ->will($this->returnValue(true));
  70. $this->mockCacheItem
  71. ->expects($this->once())
  72. ->method('get')
  73. ->will($this->returnValue($expectedValue));
  74. $this->mockCache
  75. ->expects($this->once())
  76. ->method('getItem')
  77. ->with($expectedKey)
  78. ->will($this->returnValue($this->mockCacheItem));
  79. $implementation = new CacheTraitImplementation([
  80. 'cache' => $this->mockCache,
  81. 'key' => $key,
  82. ]);
  83. $cachedValue = $implementation->gCachedValue();
  84. $this->assertEquals($expectedValue, $cachedValue);
  85. }
  86. public function testSuccessfullyPullsFromCacheWithLongKey()
  87. {
  88. $key = 'this-key-is-over-64-characters-and-it-will-still-work'
  89. . '-but-it-will-be-hashed-and-shortened';
  90. $expectedKey = str_replace('-', '', $key);
  91. $expectedKey = substr(hash('sha256', $expectedKey), 0, 64);
  92. $expectedValue = '1234';
  93. $this->mockCacheItem
  94. ->expects($this->once())
  95. ->method('isHit')
  96. ->will($this->returnValue(true));
  97. $this->mockCacheItem
  98. ->expects($this->once())
  99. ->method('get')
  100. ->will($this->returnValue($expectedValue));
  101. $this->mockCache
  102. ->expects($this->once())
  103. ->method('getItem')
  104. ->with($expectedKey)
  105. ->will($this->returnValue($this->mockCacheItem));
  106. $implementation = new CacheTraitImplementation([
  107. 'cache' => $this->mockCache,
  108. 'key' => $key
  109. ]);
  110. $cachedValue = $implementation->gCachedValue();
  111. $this->assertEquals($expectedValue, $cachedValue);
  112. }
  113. public function testFailsPullFromCacheWithNoCache()
  114. {
  115. $implementation = new CacheTraitImplementation();
  116. $cachedValue = $implementation->gCachedValue();
  117. $this->assertEquals(null, $cachedValue);
  118. }
  119. public function testFailsPullFromCacheWithoutKey()
  120. {
  121. $implementation = new CacheTraitImplementation([
  122. 'cache' => $this->mockCache,
  123. 'key' => null,
  124. ]);
  125. $cachedValue = $implementation->gCachedValue();
  126. }
  127. public function testSuccessfullySetsToCache()
  128. {
  129. $value = '1234';
  130. $this->mockCacheItem
  131. ->expects($this->once())
  132. ->method('set')
  133. ->with($value);
  134. $this->mockCache
  135. ->expects($this->once())
  136. ->method('getItem')
  137. ->with($this->equalTo('key'))
  138. ->will($this->returnValue($this->mockCacheItem));
  139. $implementation = new CacheTraitImplementation([
  140. 'cache' => $this->mockCache,
  141. ]);
  142. $implementation->sCachedValue($value);
  143. }
  144. public function testFailsSetToCacheWithNoCache()
  145. {
  146. $implementation = new CacheTraitImplementation();
  147. $implementation->sCachedValue('1234');
  148. $cachedValue = $implementation->sCachedValue('1234');
  149. $this->assertNull($cachedValue);
  150. }
  151. public function testFailsSetToCacheWithoutKey()
  152. {
  153. $implementation = new CacheTraitImplementation([
  154. 'cache' => $this->mockCache,
  155. 'key' => null,
  156. ]);
  157. $cachedValue = $implementation->sCachedValue('1234');
  158. $this->assertNull($cachedValue);
  159. }
  160. }
  161. class CacheTraitImplementation
  162. {
  163. use CacheTrait;
  164. private $cache;
  165. private $cacheConfig;
  166. public function __construct(array $config = [])
  167. {
  168. $this->key = array_key_exists('key', $config) ? $config['key'] : 'key';
  169. $this->cache = isset($config['cache']) ? $config['cache'] : null;
  170. $this->cacheConfig = [
  171. 'prefix' => '',
  172. 'lifetime' => 1000,
  173. ];
  174. }
  175. // allows us to keep trait methods private
  176. public function gCachedValue()
  177. {
  178. return $this->getCachedValue($this->key);
  179. }
  180. public function sCachedValue($v)
  181. {
  182. $this->setCachedValue($this->key, $v);
  183. }
  184. }