Skip to content

Commit 167a222

Browse files
committed
[Server] Use an in-memory PSR-16 cache in SessionTest
1 parent 64a5cba commit 167a222

1 file changed

Lines changed: 66 additions & 3 deletions

File tree

‎tests/Unit/Server/Session/SessionTest.php‎

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
use Mcp\Server\Session\SessionStoreInterface;
1919
use PHPUnit\Framework\Attributes\DataProvider;
2020
use PHPUnit\Framework\TestCase;
21-
use Symfony\Component\Cache\Adapter\ArrayAdapter;
22-
use Symfony\Component\Cache\Psr16Cache;
21+
use Psr\SimpleCache\CacheInterface;
2322
use Symfony\Component\Uid\UuidV4;
2423

2524
class SessionTest extends TestCase
@@ -221,7 +220,7 @@ public static function provideStores(): iterable
221220
{
222221
yield 'in-memory' => [static fn () => new InMemorySessionStore()];
223222
yield 'file' => [static fn () => new FileSessionStore(sys_get_temp_dir().'/mcp-sessions-'.bin2hex(random_bytes(6)))];
224-
yield 'psr-16' => [static fn () => new Psr16SessionStore(new Psr16Cache(new ArrayAdapter()))];
223+
yield 'psr-16' => [static fn () => new Psr16SessionStore(self::arrayCache())];
225224
}
226225

227226
/**
@@ -402,4 +401,68 @@ public function testAllReturnsEmptyArrayForUndecodablePayload(): void
402401

403402
$this->assertSame([], $session->all());
404403
}
404+
405+
private static function arrayCache(): CacheInterface
406+
{
407+
return new class implements CacheInterface {
408+
/** @var array<string, mixed> */
409+
private array $values = [];
410+
411+
public function get(string $key, mixed $default = null): mixed
412+
{
413+
return $this->values[$key] ?? $default;
414+
}
415+
416+
public function set(string $key, mixed $value, int|\DateInterval|null $ttl = null): bool
417+
{
418+
$this->values[$key] = $value;
419+
420+
return true;
421+
}
422+
423+
public function delete(string $key): bool
424+
{
425+
unset($this->values[$key]);
426+
427+
return true;
428+
}
429+
430+
public function clear(): bool
431+
{
432+
$this->values = [];
433+
434+
return true;
435+
}
436+
437+
public function getMultiple(iterable $keys, mixed $default = null): iterable
438+
{
439+
foreach ($keys as $key) {
440+
yield $key => $this->get($key, $default);
441+
}
442+
}
443+
444+
public function setMultiple(iterable $values, int|\DateInterval|null $ttl = null): bool
445+
{
446+
foreach ($values as $key => $value) {
447+
$this->set((string) $key, $value, $ttl);
448+
}
449+
450+
return true;
451+
}
452+
453+
public function deleteMultiple(iterable $keys): bool
454+
{
455+
foreach ($keys as $key) {
456+
$this->delete($key);
457+
}
458+
459+
return true;
460+
}
461+
462+
public function has(string $key): bool
463+
{
464+
return \array_key_exists($key, $this->values);
465+
}
466+
};
467+
}
405468
}

0 commit comments

Comments
 (0)