diff --git a/src/Discord/Helpers/VoidCache.php b/src/Discord/Helpers/VoidCache.php new file mode 100644 index 000000000..6b2b0864e --- /dev/null +++ b/src/Discord/Helpers/VoidCache.php @@ -0,0 +1,122 @@ + + * + * This file is subject to the MIT license that is bundled + * with this source code in the LICENSE.md file. + */ + +namespace Discord\Helpers; + +use Psr\SimpleCache\CacheInterface; + +/** + * A no-op cache that never stores values and always returns the provided default. + * + * This class is conditionally defined to remain compatible with + * psr/simple-cache v1, v2 and v3 method signatures. + * + * @since v10.48.0 + */ +if ((new \ReflectionMethod(CacheInterface::class, 'get'))->hasReturnType()) { + class VoidCache implements CacheInterface + { + public function get(string $key, mixed $default = null): mixed + { + return $default; + } + + public function set(string $key, mixed $value, null|int|\DateInterval $ttl = null): bool + { + return true; + } + + public function delete(string $key): bool + { + return true; + } + + public function getMultiple(iterable $keys, mixed $default = null): iterable + { + $result = []; + + foreach ($keys as $key) { + $result[$key] = $default; + } + + return $result; + } + + public function setMultiple(iterable $values, null|int|\DateInterval $ttl = null): bool + { + return true; + } + + public function deleteMultiple(iterable $keys): bool + { + return true; + } + + public function clear(): bool + { + return true; + } + + public function has(string $key): bool + { + return false; + } + } +} else { + class VoidCache implements CacheInterface + { + public function get($key, $default = null) + { + return $default; + } + + public function set($key, $value, $ttl = null) + { + return true; + } + + public function delete($key) + { + return true; + } + + public function getMultiple($keys, $default = null) + { + $result = []; + + foreach ($keys as $key) { + $result[$key] = $default; + } + + return $result; + } + + public function setMultiple($values, $ttl = null) + { + return true; + } + + public function deleteMultiple($keys) + { + return true; + } + + public function clear() + { + return true; + } + + public function has($key) + { + return false; + } + } +} diff --git a/tests/Helpers/VoidCacheTest.php b/tests/Helpers/VoidCacheTest.php new file mode 100644 index 000000000..d63604baf --- /dev/null +++ b/tests/Helpers/VoidCacheTest.php @@ -0,0 +1,93 @@ + + * + * This file is subject to the MIT license that is bundled + * with this source code in the LICENSE.md file. + */ + +use Discord\Helpers\VoidCache; +use PHPUnit\Framework\TestCase; + +final class VoidCacheTest extends TestCase +{ + private VoidCache $cache; + + protected function setUp(): void + { + $this->cache = new VoidCache(); + } + + public function testGetReturnsNullByDefault(): void + { + $this->assertNull($this->cache->get('key')); + } + + public function testGetReturnsProvidedDefault(): void + { + $this->assertSame('default', $this->cache->get('key', 'default')); + $this->assertSame(42, $this->cache->get('key', 42)); + } + + public function testSetReturnsTrue(): void + { + $this->assertTrue($this->cache->set('key', 'value')); + } + + public function testDeleteReturnsTrue(): void + { + $this->assertTrue($this->cache->delete('key')); + } + + public function testClearReturnsTrue(): void + { + $this->assertTrue($this->cache->clear()); + } + + public function testHasReturnsFalse(): void + { + $this->assertFalse($this->cache->has('key')); + } + + public function testGetMultipleReturnsDefaultForAllKeys(): void + { + $keys = ['a', 'b', 'c']; + $result = $this->cache->getMultiple($keys, 'default'); + + $this->assertSame([ + 'a' => 'default', + 'b' => 'default', + 'c' => 'default', + ], $result); + } + + public function testGetMultipleReturnsNullDefaultWhenNotSpecified(): void + { + $result = $this->cache->getMultiple(['x', 'y']); + + $this->assertSame(['x' => null, 'y' => null], $result); + } + + public function testSetMultipleReturnsTrue(): void + { + $this->assertTrue($this->cache->setMultiple(['a' => 1, 'b' => 2])); + } + + public function testDeleteMultipleReturnsTrue(): void + { + $this->assertTrue($this->cache->deleteMultiple(['a', 'b'])); + } + + public function testSetDoesNotPersistValue(): void + { + $this->cache->set('key', 'value'); + + $this->assertNull($this->cache->get('key')); + $this->assertFalse($this->cache->has('key')); + } +}