mirror of
https://github.com/danog/Valinor.git
synced 2024-11-26 20:24:40 +01:00
87 lines
2.2 KiB
PHP
87 lines
2.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace CuyZ\Valinor\Tests\Unit\Cache;
|
|
|
|
use CuyZ\Valinor\Cache\VersionedCache;
|
|
use CuyZ\Valinor\Tests\Fake\Cache\FakeCache;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use function iterator_to_array;
|
|
|
|
final class VersionedCacheTest extends TestCase
|
|
{
|
|
private FakeCache $delegate;
|
|
|
|
/** @var VersionedCache<mixed> */
|
|
private VersionedCache $cache;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
$this->delegate = new FakeCache();
|
|
$this->cache = new VersionedCache($this->delegate);
|
|
}
|
|
|
|
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'));
|
|
}
|
|
}
|