diff --git a/Auth/src/Cache/FileSystemCacheItemPool.php b/Auth/src/Cache/FileSystemCacheItemPool.php index 07162274ef5..f2091823126 100644 --- a/Auth/src/Cache/FileSystemCacheItemPool.php +++ b/Auth/src/Cache/FileSystemCacheItemPool.php @@ -64,21 +64,26 @@ public function getItem(string $key): CacheItemInterface ); } - $item = new TypedItem($key); - $itemPath = $this->cacheFilePath($key); if (!file_exists($itemPath)) { - return $item; + return new TypedItem($key); } $serializedItem = file_get_contents($itemPath); if ($serializedItem === false) { - return $item; + return new TypedItem($key); + } + + $data = unserialize($serializedItem); + + if ($data instanceof CacheItemInterface) { + return $data; } - $item->set(unserialize($serializedItem)); + $item = new TypedItem($key); + $item->set($data); return $item; } @@ -113,7 +118,7 @@ public function save(CacheItemInterface $item): bool } $itemPath = $this->cacheFilePath($item->getKey()); - $serializedItem = serialize($item->get()); + $serializedItem = serialize($item); $result = file_put_contents($itemPath, $serializedItem, LOCK_EX); diff --git a/Auth/src/CacheTrait.php b/Auth/src/CacheTrait.php index a991c57713b..78642a36603 100644 --- a/Auth/src/CacheTrait.php +++ b/Auth/src/CacheTrait.php @@ -59,6 +59,8 @@ private function getCachedValue($k) if ($cacheItem->isHit()) { return $cacheItem->get(); } + + return null; } /** diff --git a/Auth/tests/Cache/FileSystemCacheItemPoolTest.php b/Auth/tests/Cache/FileSystemCacheItemPoolTest.php index c6c9a4f209c..f19af435e0d 100644 --- a/Auth/tests/Cache/FileSystemCacheItemPoolTest.php +++ b/Auth/tests/Cache/FileSystemCacheItemPoolTest.php @@ -17,6 +17,7 @@ namespace Google\Auth\Tests\Cache; +use DateTime; use Google\Auth\Cache\FileSystemCacheItemPool; use Google\Auth\Cache\TypedItem; use PHPUnit\Framework\TestCase; @@ -66,6 +67,29 @@ public function testSaveAndGetItem() $this->assertEquals($retrievedItem->get(), $item->get()); } + public function testSaveAndGetExpiredItem() + { + $item = $this->getNewItem(); + $item->expiresAt(new DateTime('yesterday')); + $this->pool->save($item); + $retrievedItem = $this->pool->getItem($item->getKey()); + + $this->assertFalse($retrievedItem->isHit()); + $this->assertNull($retrievedItem->get()); + } + + public function testGetItemLegacyCacheFile() + { + $key = 'LegacyItem'; + $itemPath = $this->cachePath . '/' . $key; + file_put_contents($itemPath, serialize('legacyValue')); + + $retrievedItem = $this->pool->getItem($key); + + $this->assertTrue($retrievedItem->isHit()); + $this->assertEquals('legacyValue', $retrievedItem->get()); + } + public function testHasItem() { $item = $this->getNewItem(); diff --git a/Auth/tests/CacheTraitTest.php b/Auth/tests/CacheTraitTest.php index ccef6b579b9..849bdd6af95 100644 --- a/Auth/tests/CacheTraitTest.php +++ b/Auth/tests/CacheTraitTest.php @@ -58,6 +58,25 @@ public function testSuccessfullyPullsFromCache() $this->assertEquals($expectedValue, $cachedValue); } + public function testFailsPullFromCacheWhenItemIsNotHit() + { + $this->mockCacheItem->isHit() + ->shouldBeCalledTimes(1) + ->willReturn(false); + $this->mockCacheItem->get() + ->shouldNotBeCalled(); + $this->mockCache->getItem('key') + ->shouldBeCalledTimes(1) + ->willReturn($this->mockCacheItem->reveal()); + + $implementation = $this->getCacheTraitImplementation([ + 'cache' => $this->mockCache->reveal(), + ]); + + $cachedValue = $implementation->getCachedValue('key'); + $this->assertNull($cachedValue); + } + public function testSuccessfullyPullsFromCacheWithInvalidKey() { $key = 'this-key-has-@-illegal-characters';