- Add DISCOVERY_LOG_LEVEL=debug - Add DISCOVERY_SHOW_PROGRESS=true - Temporary changes for debugging InitializerProcessor fixes on production
131 lines
4.0 KiB
PHP
131 lines
4.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Unit\Framework\Cache;
|
|
|
|
use App\Framework\Cache\CacheItem;
|
|
use App\Framework\Cache\CacheKey;
|
|
use App\Framework\Cache\Driver\InMemoryCache;
|
|
use App\Framework\Cache\GeneralCache;
|
|
use App\Framework\Core\ValueObjects\Duration;
|
|
use App\Framework\Serializer\Php\PhpSerializer;
|
|
|
|
test('cache can store and retrieve values', function () {
|
|
$cache = new GeneralCache(new InMemoryCache(), new PhpSerializer());
|
|
$key = CacheKey::fromString('test-key');
|
|
|
|
$result = $cache->set($key, 'test-value');
|
|
expect($result)->toBeTrue();
|
|
|
|
$item = $cache->get($key);
|
|
expect($item)->toBeInstanceOf(CacheItem::class);
|
|
expect($item->isHit)->toBeTrue();
|
|
expect($item->value)->toBe('test-value');
|
|
});
|
|
|
|
test('cache returns miss for non-existent key', function () {
|
|
$cache = new GeneralCache(new InMemoryCache(), new PhpSerializer());
|
|
$key = CacheKey::fromString('non-existent');
|
|
|
|
$item = $cache->get($key);
|
|
expect($item->isHit)->toBeFalse();
|
|
expect($item->value)->toBeNull();
|
|
});
|
|
|
|
test('cache can check if key exists', function () {
|
|
$cache = new GeneralCache(new InMemoryCache(), new PhpSerializer());
|
|
$key = CacheKey::fromString('test-key');
|
|
|
|
expect($cache->has($key))->toBeFalse();
|
|
|
|
$cache->set($key, 'value');
|
|
expect($cache->has($key))->toBeTrue();
|
|
});
|
|
|
|
test('cache can forget keys', function () {
|
|
$cache = new GeneralCache(new InMemoryCache(), new PhpSerializer());
|
|
$key = CacheKey::fromString('test-key');
|
|
|
|
$cache->set($key, 'value');
|
|
expect($cache->has($key))->toBeTrue();
|
|
|
|
$result = $cache->forget($key);
|
|
expect($result)->toBeTrue();
|
|
expect($cache->has($key))->toBeFalse();
|
|
});
|
|
|
|
test('cache can clear all entries', function () {
|
|
$cache = new GeneralCache(new InMemoryCache(), new PhpSerializer());
|
|
$key1 = CacheKey::fromString('key1');
|
|
$key2 = CacheKey::fromString('key2');
|
|
|
|
$cache->set($key1, 'value1');
|
|
$cache->set($key2, 'value2');
|
|
|
|
$result = $cache->clear();
|
|
expect($result)->toBeTrue();
|
|
|
|
expect($cache->has($key1))->toBeFalse();
|
|
expect($cache->has($key2))->toBeFalse();
|
|
});
|
|
|
|
test('cache remember pattern works', function () {
|
|
$cache = new GeneralCache(new InMemoryCache(), new PhpSerializer());
|
|
$key = CacheKey::fromString('test-key');
|
|
$callCount = 0;
|
|
|
|
$callback = function () use (&$callCount) {
|
|
$callCount++;
|
|
|
|
return 'computed-value';
|
|
};
|
|
|
|
// First call should execute callback
|
|
$item1 = $cache->remember($key, $callback);
|
|
expect($item1->value)->toBe('computed-value');
|
|
expect($callCount)->toBe(1);
|
|
|
|
// Second call should return cached value
|
|
$item2 = $cache->remember($key, $callback);
|
|
expect($item2->value)->toBe('computed-value');
|
|
expect($callCount)->toBe(1); // Callback not called again
|
|
});
|
|
|
|
test('cache respects TTL', function () {
|
|
$cache = new GeneralCache(new InMemoryCache(), new PhpSerializer());
|
|
$key = CacheKey::fromString('test-key');
|
|
$ttl = Duration::fromSeconds(60);
|
|
|
|
// This test would need a mock or a way to advance time
|
|
// For now, just test that TTL parameter is accepted
|
|
$result = $cache->set($key, 'value', $ttl);
|
|
expect($result)->toBeTrue();
|
|
});
|
|
|
|
test('cache can store different data types', function () {
|
|
$cache = new GeneralCache(new InMemoryCache(), new PhpSerializer());
|
|
|
|
// String
|
|
$stringKey = CacheKey::fromString('string');
|
|
$cache->set($stringKey, 'test');
|
|
expect($cache->get($stringKey)->value)->toBe('test');
|
|
|
|
// Integer
|
|
$intKey = CacheKey::fromString('int');
|
|
$cache->set($intKey, 42);
|
|
expect($cache->get($intKey)->value)->toBe(42);
|
|
|
|
// Array
|
|
$arrayKey = CacheKey::fromString('array');
|
|
$cache->set($arrayKey, ['a' => 1, 'b' => 2]);
|
|
expect($cache->get($arrayKey)->value)->toBe(['a' => 1, 'b' => 2]);
|
|
|
|
// Object
|
|
$objectKey = CacheKey::fromString('object');
|
|
$obj = new \stdClass();
|
|
$obj->test = 'value';
|
|
$cache->set($objectKey, $obj);
|
|
expect($cache->get($objectKey)->value)->toEqual($obj);
|
|
});
|