2021-11-28 17:43:02 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace CuyZ\Valinor\Tests\Unit\Cache;
|
|
|
|
|
2022-08-31 00:56:30 +02:00
|
|
|
use CuyZ\Valinor\Cache\KeySanitizerCache;
|
2021-11-28 17:43:02 +01:00
|
|
|
use CuyZ\Valinor\Tests\Fake\Cache\FakeCache;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
|
|
|
|
use function iterator_to_array;
|
|
|
|
|
2022-08-31 00:56:30 +02:00
|
|
|
final class KeySanitizerCacheTest extends TestCase
|
2021-11-28 17:43:02 +01:00
|
|
|
{
|
|
|
|
private FakeCache $delegate;
|
|
|
|
|
2022-08-31 00:56:30 +02:00
|
|
|
/** @var KeySanitizerCache<mixed> */
|
|
|
|
private KeySanitizerCache $cache;
|
2021-11-28 17:43:02 +01:00
|
|
|
|
|
|
|
protected function setUp(): void
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->delegate = new FakeCache();
|
2022-08-31 00:56:30 +02:00
|
|
|
$this->cache = new KeySanitizerCache($this->delegate);
|
2021-11-28 17:43:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function test_set_value_sets_value_in_delegate_with_changed_key(): void
|
|
|
|
{
|
|
|
|
$this->cache->set('foo', 'foo');
|
|
|
|
|
|
|
|
self::assertTrue($this->cache->has('foo'));
|
|
|
|
self::assertFalse($this->delegate->has('foo'));
|
|
|
|
|
|
|
|
self::assertSame('foo', $this->cache->get('foo'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_delete_entry_deletes_entry(): void
|
|
|
|
{
|
|
|
|
$this->cache->set('foo', 'foo');
|
|
|
|
$this->cache->delete('foo');
|
|
|
|
|
|
|
|
self::assertFalse($this->cache->has('foo'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_clear_entries_clears_everything(): void
|
|
|
|
{
|
|
|
|
$this->cache->set('foo', 'foo');
|
|
|
|
$this->cache->clear();
|
|
|
|
|
|
|
|
self::assertFalse($this->cache->has('foo'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_setting_values_sets_values_in_delegate_with_changed_key(): void
|
|
|
|
{
|
|
|
|
$values = [
|
|
|
|
'foo' => 'foo',
|
|
|
|
'bar' => 'bar',
|
|
|
|
];
|
|
|
|
|
|
|
|
$this->cache->setMultiple($values);
|
|
|
|
|
|
|
|
self::assertTrue($this->cache->has('foo'));
|
|
|
|
self::assertTrue($this->cache->has('bar'));
|
|
|
|
|
|
|
|
self::assertFalse($this->delegate->has('foo'));
|
|
|
|
self::assertFalse($this->delegate->has('bar'));
|
|
|
|
|
|
|
|
self::assertSame($values, iterator_to_array($this->cache->getMultiple(['foo', 'bar'])));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_delete_entries_deletes_correct_entries(): void
|
|
|
|
{
|
|
|
|
$this->cache->setMultiple([
|
|
|
|
'foo' => 'foo',
|
|
|
|
'bar' => 'bar',
|
|
|
|
'baz' => 'baz',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->cache->deleteMultiple(['foo', 'baz']);
|
|
|
|
|
|
|
|
self::assertFalse($this->cache->has('foo'));
|
|
|
|
self::assertTrue($this->cache->has('bar'));
|
|
|
|
self::assertFalse($this->cache->has('baz'));
|
|
|
|
}
|
|
|
|
}
|