From 02e86b85493bb6037ba7bd96faf425c4bde876e8 Mon Sep 17 00:00:00 2001 From: Hans Erik Jepsen Date: Wed, 24 Aug 2022 14:34:12 +0200 Subject: [PATCH] Upgrade to simple cache 2 & 3 (#11) * Updated PHP requirements to >=8.0 and updated tests to codeception 5 * Removed mindplay/simple-cache dev-dependency * Updated simple-cache to ^2||^3 * Updated MockCache::get signature * Updated MockCache::set signature * Updated MockCache::delete signature * Updated MockCache::clear signature and implementation * Updated MockCache::getMultiple signature * Updated MockCache::setMultiple signature * Updated MockCache::deleteMultiple signature and implementation * Updated MockCache::has signature * MockCache::getMultiple and MockCache::setMultiple no longer need to throw InvalidArgumentException. If the first argument is not an iterable a TypeError will be thrown by PHP. * MockCache::deleteMultiple no longer needs to throw InvalidArgumentException. If the first argument is not an iterable a TypeError will be thrown by PHP. * Removed mindplay/simple-cache dev-dependency --- composer.json | 2 +- src/MockCache.php | 32 ++++++++++++-------------------- tests/unit/MockCacheCest.php | 7 ++++--- 3 files changed, 17 insertions(+), 24 deletions(-) diff --git a/composer.json b/composer.json index ade45d8..57d6d4c 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,7 @@ "prefer-stable": true, "require": { "php": ">= 8.0", - "psr/simple-cache": "^1" + "psr/simple-cache": "^2||^3" }, "require-dev": { "codeception/codeception": "^5", diff --git a/src/MockCache.php b/src/MockCache.php index af8a9be..b79f1bb 100644 --- a/src/MockCache.php +++ b/src/MockCache.php @@ -61,7 +61,7 @@ public function skipTime($seconds) $this->time += $seconds; } - public function get($key, $default = null) + public function get(string $key, mixed $default = null): mixed { $this->validateKey($key); @@ -70,7 +70,7 @@ public function get($key, $default = null) : $default; } - public function set($key, $value, $ttl = null) + public function set(string $key, mixed $value, DateInterval|int|null $ttl = null): bool { $this->validateKey($key); @@ -90,7 +90,7 @@ public function set($key, $value, $ttl = null) return true; } - public function delete($key) + public function delete(string $key): bool { $this->validateKey($key); @@ -106,18 +106,16 @@ public function delete($key) return $success; } - public function clear() + public function clear(): bool { $this->cache = []; $this->cache_expiration = []; + + return true; } - public function getMultiple($keys, $default = null) + public function getMultiple(iterable $keys, mixed $default = null): iterable { - if (! is_array($keys) && ! $keys instanceof Traversable) { - throw new InvalidArgumentException("keys must be either of type array or Traversable"); - } - $values = []; foreach ($keys as $key) { @@ -128,12 +126,8 @@ public function getMultiple($keys, $default = null) return $values; } - public function setMultiple($values, $ttl = null) + public function setMultiple(iterable $values, DateInterval|int|null $ttl = null): bool { - if (! is_array($values) && ! $values instanceof Traversable) { - throw new InvalidArgumentException("keys must be either of type array or Traversable"); - } - foreach ($values as $key => $value) { $this->validateKey($key); $this->set($key, $value, $ttl); @@ -142,19 +136,17 @@ public function setMultiple($values, $ttl = null) return true; } - public function deleteMultiple($keys) + public function deleteMultiple(iterable $keys): bool { - if (! is_array($keys) && ! $keys instanceof Traversable) { - throw new InvalidArgumentException("keys must be either of type array or Traversable"); - } - foreach ($keys as $key) { $this->validateKey($key); $this->delete($key); } + + return true; } - public function has($key) + public function has(string $key): bool { return $this->get($key, $this) !== $this; } diff --git a/tests/unit/MockCacheCest.php b/tests/unit/MockCacheCest.php index 1c0ffed..db7ebaa 100644 --- a/tests/unit/MockCacheCest.php +++ b/tests/unit/MockCacheCest.php @@ -5,6 +5,7 @@ use DateInterval; use Kodus\Cache\MockCache; use Kodus\Cache\InvalidArgumentException; +use TypeError; use UnitTester; class MockCacheCest @@ -130,11 +131,11 @@ public function testGetAndSetMultiple(UnitTester $I): void $I->assertSame(["key1" => "value1", "key2" => "value2", "key3" => false], $results); - $I->expectThrowable(InvalidArgumentException::class, function () { + $I->expectThrowable(TypeError::class, function () { $this->cache->getMultiple("Invalid type"); }); - $I->expectThrowable(InvalidArgumentException::class, function () { + $I->expectThrowable(TypeError::class, function () { $this->cache->setMultiple("Invalid type"); }); @@ -157,7 +158,7 @@ public function testDeleteMultiple(UnitTester $I): void $I->assertSame("value3", $this->cache->get("key3")); - $I->expectThrowable(InvalidArgumentException::class, function () { + $I->expectThrowable(TypeError::class, function () { $this->cache->deleteMultiple("Invalid type"); });