| 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 |
|
| 18 |
namespace Google\Auth\Tests; |
| 19 |
|
| 20 |
use Google\Auth\CacheTrait; |
| 21 |
|
| 22 |
class CacheTraitTest extends \PHPUnit_Framework_TestCase |
| 23 |
{ |
| 24 |
private $mockFetcher; |
| 25 |
private $mockCache; |
| 26 |
|
| 27 |
public function setUp() |
| 28 |
{ |
| 29 |
$this->mockFetcher = |
| 30 |
$this |
| 31 |
->getMockBuilder('Google\Auth\FetchAuthTokenInterface') |
| 32 |
->getMock(); |
| 33 |
$this->mockCache = |
| 34 |
$this |
| 35 |
->getMockBuilder('Google\Auth\CacheInterface') |
| 36 |
->getMock(); |
| 37 |
} |
| 38 |
|
| 39 |
public function testSuccessfullyPullsFromCacheWithoutFetcher() |
| 40 |
{ |
| 41 |
$expectedValue = '1234'; |
| 42 |
$this->mockCache |
| 43 |
->expects($this->once()) |
| 44 |
->method('get') |
| 45 |
->will($this->returnValue($expectedValue)); |
| 46 |
|
| 47 |
$implementation = new CacheTraitImplementation([ |
| 48 |
'cache' => $this->mockCache |
| 49 |
]); |
| 50 |
|
| 51 |
$cachedValue = $implementation->gCachedValue(); |
| 52 |
$this->assertEquals($expectedValue, $cachedValue); |
| 53 |
} |
| 54 |
|
| 55 |
public function testSuccessfullyPullsFromCacheWithFetcher() |
| 56 |
{ |
| 57 |
$expectedValue = '1234'; |
| 58 |
$this->mockCache |
| 59 |
->expects($this->once()) |
| 60 |
->method('get') |
| 61 |
->will($this->returnValue($expectedValue)); |
| 62 |
$this->mockFetcher |
| 63 |
->expects($this->once()) |
| 64 |
->method('getCacheKey') |
| 65 |
->will($this->returnValue('key')); |
| 66 |
|
| 67 |
$implementation = new CacheTraitImplementation([ |
| 68 |
'cache' => $this->mockCache, |
| 69 |
'fetcher' => $this->mockFetcher |
| 70 |
]); |
| 71 |
|
| 72 |
$cachedValue = $implementation->gCachedValue(); |
| 73 |
$this->assertEquals($expectedValue, $cachedValue); |
| 74 |
} |
| 75 |
|
| 76 |
public function testFailsPullFromCacheWithNoCache() |
| 77 |
{ |
| 78 |
$implementation = new CacheTraitImplementation(); |
| 79 |
|
| 80 |
$cachedValue = $implementation->gCachedValue(); |
| 81 |
$this->assertEquals(null, $cachedValue); |
| 82 |
} |
| 83 |
|
| 84 |
public function testFailsPullFromCacheWithoutKey() |
| 85 |
{ |
| 86 |
$this->mockFetcher |
| 87 |
->expects($this->once()) |
| 88 |
->method('getCacheKey') |
| 89 |
->will($this->returnValue(null)); |
| 90 |
|
| 91 |
$implementation = new CacheTraitImplementation([ |
| 92 |
'cache' => $this->mockCache, |
| 93 |
'fetcher' => $this->mockFetcher |
| 94 |
]); |
| 95 |
|
| 96 |
$cachedValue = $implementation->gCachedValue(); |
| 97 |
} |
| 98 |
|
| 99 |
public function testSuccessfullySetsToCacheWithoutFetcher() |
| 100 |
{ |
| 101 |
$value = '1234'; |
| 102 |
$this->mockCache |
| 103 |
->expects($this->once()) |
| 104 |
->method('set') |
| 105 |
->with('key', $value); |
| 106 |
|
| 107 |
$implementation = new CacheTraitImplementation([ |
| 108 |
'cache' => $this->mockCache |
| 109 |
]); |
| 110 |
|
| 111 |
$implementation->sCachedValue($value); |
| 112 |
} |
| 113 |
|
| 114 |
public function testSuccessfullySetsToCacheWithFetcher() |
| 115 |
{ |
| 116 |
$value = '1234'; |
| 117 |
$this->mockCache |
| 118 |
->expects($this->once()) |
| 119 |
->method('set') |
| 120 |
->with('key', $value); |
| 121 |
$this->mockFetcher |
| 122 |
->expects($this->once()) |
| 123 |
->method('getCacheKey') |
| 124 |
->will($this->returnValue('key')); |
| 125 |
|
| 126 |
$implementation = new CacheTraitImplementation([ |
| 127 |
'cache' => $this->mockCache, |
| 128 |
'fetcher' => $this->mockFetcher |
| 129 |
]); |
| 130 |
|
| 131 |
$implementation->sCachedValue($value); |
| 132 |
} |
| 133 |
|
| 134 |
public function testFailsSetToCacheWithNoCache() |
| 135 |
{ |
| 136 |
$this->mockFetcher |
| 137 |
->expects($this->never()) |
| 138 |
->method('getCacheKey'); |
| 139 |
|
| 140 |
$implementation = new CacheTraitImplementation([ |
| 141 |
'fetcher' => $this->mockFetcher |
| 142 |
]); |
| 143 |
|
| 144 |
$implementation->sCachedValue('1234'); |
| 145 |
} |
| 146 |
|
| 147 |
public function testFailsSetToCacheWithoutKey() |
| 148 |
{ |
| 149 |
$this->mockFetcher |
| 150 |
->expects($this->once()) |
| 151 |
->method('getCacheKey') |
| 152 |
->will($this->returnValue(null)); |
| 153 |
|
| 154 |
$implementation = new CacheTraitImplementation([ |
| 155 |
'cache' => $this->mockCache, |
| 156 |
'fetcher' => $this->mockFetcher |
| 157 |
]); |
| 158 |
|
| 159 |
$cachedValue = $implementation->sCachedValue('1234'); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
class CacheTraitImplementation |
| 164 |
{ |
| 165 |
use CacheTrait; |
| 166 |
|
| 167 |
private $cache; |
| 168 |
private $fetcher; |
| 169 |
private $cacheConfig; |
| 170 |
|
| 171 |
public function __construct(array $config = []) |
| 172 |
{ |
| 173 |
$this->cache = isset($config['cache']) ? $config['cache'] : null; |
| 174 |
$this->fetcher = isset($config['fetcher']) ? $config['fetcher'] : null; |
| 175 |
$this->cacheConfig = [ |
| 176 |
'prefix' => '', |
| 177 |
'lifetime' => 1000 |
| 178 |
]; |
| 179 |
} |
| 180 |
|
| 181 |
// allows us to keep trait methods private |
| 182 |
public function gCachedValue() |
| 183 |
{ |
| 184 |
return $this->getCachedValue(); |
| 185 |
} |
| 186 |
|
| 187 |
public function sCachedValue($v) |
| 188 |
{ |
| 189 |
$this->setCachedValue($v); |
| 190 |
} |
| 191 |
|
| 192 |
private function getCacheKey() |
| 193 |
{ |
| 194 |
return 'key'; |
| 195 |
} |
| 196 |
} |
| 197 |
|