Skip to content

Commit

Permalink
Add Cache Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ilicmiljan committed Mar 9, 2024
1 parent df9c637 commit fc1f300
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

118 changes: 118 additions & 0 deletions tests/Cache/ItemPoolCompatibleCacheTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php

namespace IlicMiljan\SecureProps\Tests\Cache;

use IlicMiljan\SecureProps\Cache\Exception\InvalidCacheKey;
use IlicMiljan\SecureProps\Cache\ItemPoolCompatibleCache;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Cache\CacheItemInterface;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Cache\InvalidArgumentException;

class ItemPoolCompatibleCacheTest extends TestCase
{
/**
* @var CacheItemPoolInterface&MockObject
*/
private $cachePool;
/**
* @var CacheItemInterface&MockObject
*/
private $cacheItem;
private ItemPoolCompatibleCache $cache;

protected function setUp(): void
{
parent::setUp();

$this->cachePool = $this->createMock(CacheItemPoolInterface::class);
$this->cacheItem = $this->createMock(CacheItemInterface::class);
$this->cache = new ItemPoolCompatibleCache($this->cachePool);
}

public function testGetWithCacheHit(): void
{
$key = 'testKey';
$expectedValue = 'testValue';

$this->cachePool->expects($this->once())
->method('getItem')
->with($key)
->willReturn($this->cacheItem);

$this->cacheItem->expects($this->once())
->method('isHit')
->willReturn(true);
$this->cacheItem->expects($this->once())
->method('get')
->willReturn($expectedValue);

$result = $this->cache->get($key, function () {
return 'valueFromCallable';
});

$this->assertEquals($expectedValue, $result);
}

public function testGetWithCacheMiss(): void
{
$key = 'testKey';
$valueFromCallable = 'valueFromCallable';

$this->cachePool->method('getItem')->willReturn($this->cacheItem);
$this->cacheItem->method('isHit')->willReturn(false);

// Ensure callable is executed to generate value on cache miss
$this->cacheItem->expects($this->once())->method('set')->with($valueFromCallable)
->willReturnCallback(function () use ($valueFromCallable) {
$this->cacheItem->method('get')->willReturn($valueFromCallable);
return $this->cacheItem;
});

$this->cacheItem->expects($this->once())->method('expiresAfter')->with($this->isNull());
$this->cachePool->expects($this->once())->method('save')->with($this->cacheItem);

$result = $this->cache->get($key, function () use ($valueFromCallable) {
return $valueFromCallable;
});

$this->assertEquals($valueFromCallable, $result);
}

public function testGetThrowsInvalidCacheKey(): void
{
$this->expectException(InvalidCacheKey::class);

$this->cachePool->method('getItem')->willThrowException($this->createMock(InvalidArgumentException::class));

$this->cache->get('invalidKey', function () {
return 'value';
});
}

public function testGetWithTTL(): void
{
$key = 'testKey';
$ttl = 3600;
$valueFromCallable = 'valueFromCallable';

$this->cachePool->method('getItem')->willReturn($this->cacheItem);
$this->cacheItem->method('isHit')->willReturn(false);

$this->cacheItem->expects($this->once())->method('set')->with($valueFromCallable)
->willReturnCallback(function () use ($valueFromCallable) {
$this->cacheItem->method('get')->willReturn($valueFromCallable);
return $this->cacheItem;
});

$this->cacheItem->expects($this->once())->method('expiresAfter')->with($ttl);
$this->cachePool->expects($this->once())->method('save')->with($this->cacheItem);

$result = $this->cache->get($key, function () use ($valueFromCallable) {
return $valueFromCallable;
}, $ttl);

$this->assertEquals($valueFromCallable, $result);
}
}

0 comments on commit fc1f300

Please sign in to comment.