Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions Auth/src/Cache/FileSystemCacheItemPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);

Expand Down
2 changes: 2 additions & 0 deletions Auth/src/CacheTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ private function getCachedValue($k)
if ($cacheItem->isHit()) {
return $cacheItem->get();
}

return null;
}

/**
Expand Down
24 changes: 24 additions & 0 deletions Auth/tests/Cache/FileSystemCacheItemPoolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
19 changes: 19 additions & 0 deletions Auth/tests/CacheTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
Loading