- Add DISCOVERY_LOG_LEVEL=debug - Add DISCOVERY_SHOW_PROGRESS=true - Temporary changes for debugging InitializerProcessor fixes on production
114 lines
3.1 KiB
PHP
114 lines
3.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
use App\Framework\Cache\CacheKey;
|
|
use App\Framework\Cache\Driver\InMemoryCache;
|
|
use App\Framework\Core\ValueObjects\Duration;
|
|
|
|
beforeEach(function () {
|
|
$this->cache = new InMemoryCache();
|
|
});
|
|
|
|
test('get returns miss for non-existent key', function () {
|
|
$key = CacheKey::fromString('non-existent');
|
|
$item = $this->cache->get($key);
|
|
|
|
expect($item->isHit)->toBeFalse()
|
|
->and($item->key)->toBe($key)
|
|
->and($item->value)->toBeNull();
|
|
});
|
|
|
|
test('set and get stores and retrieves value', function () {
|
|
$key = CacheKey::fromString('test-key');
|
|
$value = 'test-value';
|
|
|
|
$result = $this->cache->set($key, $value);
|
|
|
|
expect($result)->toBeTrue();
|
|
|
|
$item = $this->cache->get($key);
|
|
|
|
expect($item->isHit)->toBeTrue()
|
|
->and($item->key)->toBe($key)
|
|
->and($item->value)->toBe($value);
|
|
});
|
|
|
|
test('has returns correct existence status', function () {
|
|
$key = CacheKey::fromString('test-key');
|
|
|
|
expect($this->cache->has($key))->toBeFalse();
|
|
|
|
$this->cache->set($key, 'value');
|
|
|
|
expect($this->cache->has($key))->toBeTrue();
|
|
});
|
|
|
|
test('forget removes item from cache', function () {
|
|
$key = CacheKey::fromString('test-key');
|
|
$this->cache->set($key, 'value');
|
|
|
|
expect($this->cache->has($key))->toBeTrue();
|
|
|
|
$result = $this->cache->forget($key);
|
|
|
|
expect($result)->toBeTrue()
|
|
->and($this->cache->has($key))->toBeFalse();
|
|
});
|
|
|
|
test('clear removes all items from cache', function () {
|
|
$key1 = CacheKey::fromString('key1');
|
|
$key2 = CacheKey::fromString('key2');
|
|
|
|
$this->cache->set($key1, 'value1');
|
|
$this->cache->set($key2, 'value2');
|
|
|
|
expect($this->cache->has($key1))->toBeTrue()
|
|
->and($this->cache->has($key2))->toBeTrue();
|
|
|
|
$result = $this->cache->clear();
|
|
|
|
expect($result)->toBeTrue()
|
|
->and($this->cache->has($key1))->toBeFalse()
|
|
->and($this->cache->has($key2))->toBeFalse();
|
|
});
|
|
|
|
test('set with ttl parameter still stores value', function () {
|
|
$key = CacheKey::fromString('test-key');
|
|
$value = 'test-value';
|
|
$ttl = Duration::fromHours(1);
|
|
|
|
$result = $this->cache->set($key, $value, $ttl);
|
|
|
|
expect($result)->toBeTrue();
|
|
|
|
$item = $this->cache->get($key);
|
|
|
|
expect($item->isHit)->toBeTrue()
|
|
->and($item->value)->toBe($value);
|
|
});
|
|
|
|
test('multiple keys can be stored independently', function () {
|
|
$key1 = CacheKey::fromString('key1');
|
|
$key2 = CacheKey::fromString('key2');
|
|
$key3 = CacheKey::fromString('key3');
|
|
|
|
$this->cache->set($key1, 'value1');
|
|
$this->cache->set($key2, 'value2');
|
|
$this->cache->set($key3, 'value3');
|
|
|
|
expect($this->cache->get($key1)->value)->toBe('value1')
|
|
->and($this->cache->get($key2)->value)->toBe('value2')
|
|
->and($this->cache->get($key3)->value)->toBe('value3');
|
|
});
|
|
|
|
test('overwriting existing key updates value', function () {
|
|
$key = CacheKey::fromString('test-key');
|
|
|
|
$this->cache->set($key, 'original-value');
|
|
expect($this->cache->get($key)->value)->toBe('original-value');
|
|
|
|
$this->cache->set($key, 'updated-value');
|
|
expect($this->cache->get($key)->value)->toBe('updated-value');
|
|
});
|