From c7e03fa94fb4b92ab57b9a2ef0261db04bbec122 Mon Sep 17 00:00:00 2001 From: Ronan Giron Date: Fri, 18 Oct 2024 13:49:22 +0200 Subject: [PATCH] Compatibility with `psr/simple-cache` ^2.0 and ^3.0 --- CHANGELOG.md | 6 +++++ src/Cache/AbstractCacheDriver.php | 6 ++--- src/Cache/CacheManager.php | 15 ++++++------ src/Cache/FileCacheDriver.php | 9 +++---- src/Cache/KeyControlTrait.php | 2 +- src/Cache/MemoryCacheDriver.php | 9 +++---- src/Cache/NullCacheDriver.php | 17 ++++++------- tests/Cache/AbstractCacheDriverTest.php | 23 ------------------ tests/Cache/NullCacheDriverTest.php | 24 ------------------- .../Provider/ServiceProviderTest.php | 10 ++++---- 10 files changed, 42 insertions(+), 79 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 81244b9..f4ab34e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file. This projec to [Semantic Versioning] (http://semver.org/). For change log format, use [Keep a Changelog] (http://keepachangelog.com/). +## [2.4.0] - 2024-10-18 + +### Added + +- Compatibility with `psr/simple-cache` ^2.0 and ^3.0 + ## [2.3.0] - 2023-09-04 ### Changed diff --git a/src/Cache/AbstractCacheDriver.php b/src/Cache/AbstractCacheDriver.php index 0a17c4c..d48e044 100644 --- a/src/Cache/AbstractCacheDriver.php +++ b/src/Cache/AbstractCacheDriver.php @@ -34,7 +34,7 @@ abstract class AbstractCacheDriver implements CacheInterface * @inheritDoc * @throws CacheException */ - public function getMultiple($keys, $default = null): array + public function getMultiple(iterable $keys, mixed $default = null): iterable { if (!is_iterable($keys)) { throw new InvalidArgumentCacheException('First argument must be iterable'); @@ -53,7 +53,7 @@ public function getMultiple($keys, $default = null): array * @inheritDoc * @throws CacheException */ - public function setMultiple($values, $ttl = null): bool + public function setMultiple(iterable $values, null|int|DateInterval $ttl = null): bool { if (!is_iterable($values)) { throw new InvalidArgumentCacheException('First argument must be iterable'); @@ -75,7 +75,7 @@ public function setMultiple($values, $ttl = null): bool * @inheritDoc * @throws CacheException */ - public function deleteMultiple($keys): bool + public function deleteMultiple(iterable $keys): bool { if (!is_iterable($keys)) { throw new InvalidArgumentCacheException('First argument must be iterable'); diff --git a/src/Cache/CacheManager.php b/src/Cache/CacheManager.php index 7fb4918..84e636e 100644 --- a/src/Cache/CacheManager.php +++ b/src/Cache/CacheManager.php @@ -15,6 +15,7 @@ namespace Berlioz\Core\Cache; use Berlioz\Core\Directories\DirectoriesInterface; +use DateInterval; use Psr\SimpleCache\CacheInterface; /** @@ -62,7 +63,7 @@ public function getClass(): string|false /** * @inheritDoc */ - public function get($key, $default = null): mixed + public function get(string $key, mixed $default = null): mixed { return $this->cache->get($key, $default); } @@ -70,7 +71,7 @@ public function get($key, $default = null): mixed /** * @inheritDoc */ - public function set($key, $value, $ttl = null): bool + public function set(string $key, mixed $value, null|int|DateInterval $ttl = null): bool { return $this->cache->set($key, $value, $ttl); } @@ -78,7 +79,7 @@ public function set($key, $value, $ttl = null): bool /** * @inheritDoc */ - public function delete($key): bool + public function delete(string $key): bool { return $this->cache->delete($key); } @@ -94,7 +95,7 @@ public function clear(): bool /** * @inheritDoc */ - public function getMultiple($keys, $default = null): iterable + public function getMultiple(iterable $keys, mixed $default = null): iterable { return $this->cache->getMultiple($keys, $default); } @@ -102,7 +103,7 @@ public function getMultiple($keys, $default = null): iterable /** * @inheritDoc */ - public function setMultiple($values, $ttl = null): bool + public function setMultiple(iterable $values, null|int|DateInterval $ttl = null): bool { return $this->cache->setMultiple($values, $ttl); } @@ -110,7 +111,7 @@ public function setMultiple($values, $ttl = null): bool /** * @inheritDoc */ - public function deleteMultiple($keys): bool + public function deleteMultiple(iterable $keys): bool { return $this->cache->deleteMultiple($keys); } @@ -118,7 +119,7 @@ public function deleteMultiple($keys): bool /** * @inheritDoc */ - public function has($key) + public function has(string $key): bool { return $this->cache->has($key); } diff --git a/src/Cache/FileCacheDriver.php b/src/Cache/FileCacheDriver.php index 8e0c1a3..8f2afe8 100644 --- a/src/Cache/FileCacheDriver.php +++ b/src/Cache/FileCacheDriver.php @@ -16,6 +16,7 @@ use Berlioz\Core\Directories\DirectoriesInterface; use Berlioz\Core\Exception\CacheException; +use DateInterval; use Exception; use Psr\SimpleCache\CacheInterface; @@ -39,7 +40,7 @@ public function __construct(protected DirectoriesInterface $directories) * @inheritDoc * @throws CacheException */ - public function has($key): bool + public function has(string $key): bool { $this->controlKey($key); $cacheFilename = $this->getFilename($key); @@ -55,7 +56,7 @@ public function has($key): bool * @inheritDoc * @throws CacheException */ - public function get($key, $default = null) + public function get(string $key, mixed $default = null): mixed { $this->controlKey($key); $cacheFilename = $this->getFilename($key); @@ -85,7 +86,7 @@ public function get($key, $default = null) * @inheritDoc * @throws CacheException */ - public function set($key, $value, $ttl = null): bool + public function set(string $key, mixed $value, null|int|DateInterval $ttl = null): bool { $this->controlKey($key); $cacheFilename = $this->getFilename($key); @@ -116,7 +117,7 @@ public function set($key, $value, $ttl = null): bool * @inheritDoc * @throws CacheException */ - public function delete($key): bool + public function delete(string $key): bool { $this->controlKey($key); $cacheFilename = $this->getFilename($key); diff --git a/src/Cache/KeyControlTrait.php b/src/Cache/KeyControlTrait.php index 8257ab8..3c824a5 100644 --- a/src/Cache/KeyControlTrait.php +++ b/src/Cache/KeyControlTrait.php @@ -28,7 +28,7 @@ trait KeyControlTrait * * @throws InvalidArgumentCacheException */ - protected function controlKey($key) + protected function controlKey($key): void { if (!is_string($key)) { throw new InvalidArgumentCacheException('Invalid key name for cache, must be string'); diff --git a/src/Cache/MemoryCacheDriver.php b/src/Cache/MemoryCacheDriver.php index a6dbe6f..65232f5 100644 --- a/src/Cache/MemoryCacheDriver.php +++ b/src/Cache/MemoryCacheDriver.php @@ -15,6 +15,7 @@ namespace Berlioz\Core\Cache; use Berlioz\Core\Exception\CacheException; +use DateInterval; use Psr\SimpleCache\CacheInterface; /** @@ -27,7 +28,7 @@ class MemoryCacheDriver extends AbstractCacheDriver implements CacheInterface /** * @inheritDoc */ - public function has($key): bool + public function has(string $key): bool { $this->controlKey($key); @@ -41,7 +42,7 @@ public function has($key): bool /** * @inheritDoc */ - public function get($key, $default = null) + public function get(string $key, mixed $default = null): mixed { $this->controlKey($key); @@ -56,7 +57,7 @@ public function get($key, $default = null) * @inheritDoc * @throws CacheException */ - public function set($key, $value, $ttl = null): bool + public function set(string $key, mixed $value, null|int|DateInterval $ttl = null): bool { $this->controlKey($key); @@ -71,7 +72,7 @@ public function set($key, $value, $ttl = null): bool /** * @inheritDoc */ - public function delete($key): bool + public function delete(string $key): bool { $this->controlKey($key); diff --git a/src/Cache/NullCacheDriver.php b/src/Cache/NullCacheDriver.php index 54c14b7..cace9b8 100644 --- a/src/Cache/NullCacheDriver.php +++ b/src/Cache/NullCacheDriver.php @@ -15,6 +15,7 @@ namespace Berlioz\Core\Cache; use Berlioz\Core\Exception\InvalidArgumentCacheException; +use DateInterval; use Psr\SimpleCache\CacheInterface; /** @@ -27,7 +28,7 @@ class NullCacheDriver implements CacheInterface /** * @inheritDoc */ - public function get($key, $default = null) + public function get(string $key, mixed $default = null): mixed { $this->controlKey($key); @@ -37,7 +38,7 @@ public function get($key, $default = null) /** * @inheritDoc */ - public function set($key, $value, $ttl = null) + public function set(string $key, mixed $value, null|int|DateInterval $ttl = null): bool { $this->controlKey($key); @@ -47,7 +48,7 @@ public function set($key, $value, $ttl = null) /** * @inheritDoc */ - public function delete($key) + public function delete(string $key): bool { $this->controlKey($key); @@ -57,7 +58,7 @@ public function delete($key) /** * @inheritDoc */ - public function clear() + public function clear(): bool { return true; } @@ -65,7 +66,7 @@ public function clear() /** * @inheritDoc */ - public function getMultiple($keys, $default = null) + public function getMultiple(iterable $keys, mixed $default = null): iterable { if (!is_iterable($keys)) { throw new InvalidArgumentCacheException('First argument must be iterable'); @@ -79,7 +80,7 @@ public function getMultiple($keys, $default = null) /** * @inheritDoc */ - public function setMultiple($values, $ttl = null) + public function setMultiple(iterable $values, null|int|DateInterval $ttl = null): bool { if (!is_iterable($values)) { throw new InvalidArgumentCacheException('First argument must be iterable'); @@ -93,7 +94,7 @@ public function setMultiple($values, $ttl = null) /** * @inheritDoc */ - public function deleteMultiple($keys) + public function deleteMultiple(iterable $keys): bool { if (!is_iterable($keys)) { throw new InvalidArgumentCacheException('First argument must be iterable'); @@ -107,7 +108,7 @@ public function deleteMultiple($keys) /** * @inheritDoc */ - public function has($key) + public function has(string $key): bool { $this->controlKey($key); diff --git a/tests/Cache/AbstractCacheDriverTest.php b/tests/Cache/AbstractCacheDriverTest.php index b79d3cf..9fea90f 100644 --- a/tests/Cache/AbstractCacheDriverTest.php +++ b/tests/Cache/AbstractCacheDriverTest.php @@ -28,8 +28,6 @@ public function providerSet() return [ ['key', 'value', true], ['key-key', new stdClass(), true], - [['array'], 'value', false], - [new stdClass(), 'value', false], ['', 'value', false] ]; } @@ -141,13 +139,6 @@ public function testSetMultiple($dataSet, $ttl) $this->assertTrue($this->getCacheDriver()->setMultiple($dataSet, $ttl)); } - public function testSetMultiple_withNonIterable() - { - $this->expectException(InvalidArgumentCacheException::class); - - $this->getCacheDriver()->setMultiple('foo'); - } - /** * @dataProvider providerMultiple */ @@ -166,13 +157,6 @@ public function testGetMultiple($dataSet) $this->assertEquals($result, $values); } - public function testGetMultiple_withNonIterable() - { - $this->expectException(InvalidArgumentCacheException::class); - - $this->getCacheDriver()->getMultiple('foo'); - } - /** * @dataProvider providerMultiple */ @@ -189,11 +173,4 @@ public function testDeleteMultiple($dataSet) $values = $this->getCacheDriver()->getMultiple($keys); $this->assertEquals([], array_filter($values)); } - - public function testDeleteMultiple_withNonIterable() - { - $this->expectException(InvalidArgumentCacheException::class); - - $this->getCacheDriver()->deleteMultiple('foo'); - } } diff --git a/tests/Cache/NullCacheDriverTest.php b/tests/Cache/NullCacheDriverTest.php index 9ea9211..edcb8fb 100644 --- a/tests/Cache/NullCacheDriverTest.php +++ b/tests/Cache/NullCacheDriverTest.php @@ -53,14 +53,6 @@ public function testGetMultiple() $this->assertEquals(['bar' => 'qux', 'foo' => 'qux'], $nullDriver->getMultiple(['bar', 'foo'], 'qux')); } - public function testGetMultiple_withNonIterable() - { - $this->expectException(InvalidArgumentCacheException::class); - - $nullDriver = new NullCacheDriver(); - $nullDriver->getMultiple('foo'); - } - public function testSetMultiple() { $nullDriver = new NullCacheDriver(); @@ -68,14 +60,6 @@ public function testSetMultiple() $this->assertTrue($nullDriver->setMultiple(['bar' => 'qux', 'foo' => 'qux'])); } - public function testSetMultiple_withNonIterable() - { - $this->expectException(InvalidArgumentCacheException::class); - - $nullDriver = new NullCacheDriver(); - $nullDriver->setMultiple('foo'); - } - public function testDeleteMultiple() { $nullDriver = new NullCacheDriver(); @@ -83,14 +67,6 @@ public function testDeleteMultiple() $this->assertTrue($nullDriver->deleteMultiple(['bar', 'foo'])); } - public function testDeleteMultiple_withNonIterable() - { - $this->expectException(InvalidArgumentCacheException::class); - - $nullDriver = new NullCacheDriver(); - $nullDriver->deleteMultiple('foo'); - } - public function testClear() { $nullDriver = new NullCacheDriver(); diff --git a/tests/Container/Provider/ServiceProviderTest.php b/tests/Container/Provider/ServiceProviderTest.php index 8fceee9..a45030d 100644 --- a/tests/Container/Provider/ServiceProviderTest.php +++ b/tests/Container/Provider/ServiceProviderTest.php @@ -20,11 +20,11 @@ class ServiceProviderTest extends ProviderTestCase { - private Core $core; + private static Core $core; - protected function getCore(): Core + protected static function getCore(): Core { - return $this->core ?? $this->core = new Core(new FakeDefaultDirectories(), false); + return self::$core ?? self::$core = new Core(new FakeDefaultDirectories(), false); } /** @@ -33,8 +33,8 @@ protected function getCore(): Core public static function providers(): array { return [ - [new AppServiceProvider($this->getCore())], - [new CoreServiceProvider($this->getCore())] + [new AppServiceProvider(self::getCore())], + [new CoreServiceProvider(self::getCore())] ]; } }