diff --git a/README.md b/README.md index f872ca2..8754d0c 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ Create your Doctrine Cache the usual way and inject it into `SimpleCacheAdapter` namespace App; -use Interop\Container\ContainerInterface; +use Psr\Container\ContainerInterface; use Psr\SimpleCache\CacheInterface as PsrCacheInterface; use Roave\DoctrineSimpleCache\SimpleCacheAdapter; use Doctrine\Common\Cache\RedisCache; @@ -40,3 +40,12 @@ final class MyCacheFactory } ``` +## Upgrade Guide + +### BC Breaks in 2.0 + + * Support for `MultiOperationCache` added, includes support for `deleteMultiple` in Doctrine 1.7 breaks: + * `CacheException` static constructors `fromNonMultiGetCache` and `fromNonMultiPutCache` have been replaced with + `fromNonMultiOperationCache`. + * `SimpleCacheAdapter` now requires an adapter implementing `MultiOperationCache`, and no longer specifically + requires a cache implementing `MultiGetCache` or `MultiPutCache` explicitly. diff --git a/composer.json b/composer.json index 414a26a..4b1c7ba 100644 --- a/composer.json +++ b/composer.json @@ -4,7 +4,7 @@ "type": "library", "require": { "php": "~7.1.0", - "doctrine/cache": "^1.6", + "doctrine/cache": "^1.7", "psr/simple-cache": "^1.0" }, "require-dev": { diff --git a/src/Exception/CacheException.php b/src/Exception/CacheException.php index 4b2d021..bbd34a9 100644 --- a/src/Exception/CacheException.php +++ b/src/Exception/CacheException.php @@ -16,18 +16,11 @@ public static function fromNonClearableCache(DoctrineCache $cache) : self )); } - public static function fromNonMultiGetCache(DoctrineCache $cache) : self + public static function fromNonMultiOperationCache(DoctrineCache $cache) : self { return new self(sprintf( - 'The given cache %s cannot multi-get, but you tried to use a feature that requires a multi-get cache.', - get_class($cache) - )); - } - - public static function fromNonMultiPutCache(DoctrineCache $cache) : self - { - return new self(sprintf( - 'The given cache %s cannot multi-put, but you tried to use a feature that requires a multi-put cache.', + 'The given cache %s does not support multiple operations, ' + . 'but you tried to use a feature that requires a multi-operation cache.', get_class($cache) )); } diff --git a/src/SimpleCacheAdapter.php b/src/SimpleCacheAdapter.php index f4a9aea..736e373 100644 --- a/src/SimpleCacheAdapter.php +++ b/src/SimpleCacheAdapter.php @@ -5,15 +5,14 @@ use Doctrine\Common\Cache\Cache as DoctrineCache; use Doctrine\Common\Cache\ClearableCache; -use Doctrine\Common\Cache\MultiGetCache; -use Doctrine\Common\Cache\MultiPutCache; +use Doctrine\Common\Cache\MultiOperationCache; use Psr\SimpleCache\CacheInterface as PsrCache; use Roave\DoctrineSimpleCache\Exception\InvalidArgumentException; final class SimpleCacheAdapter implements PsrCache { /** - * @var DoctrineCache|ClearableCache|MultiGetCache|MultiPutCache + * @var DoctrineCache|ClearableCache|MultiOperationCache */ private $doctrineCache; @@ -26,13 +25,11 @@ public function __construct(DoctrineCache $doctrineCache) $this->doctrineCache = $doctrineCache; if (!$this->doctrineCache instanceof ClearableCache) { - throw Exception\CacheException::fromNonClearableCache($this->doctrineCache); + throw Exception\CacheException::fromNonMultiOperationCache($this->doctrineCache); } - if (!$this->doctrineCache instanceof MultiGetCache) { - throw Exception\CacheException::fromNonMultiGetCache($this->doctrineCache); - } - if (!$this->doctrineCache instanceof MultiPutCache) { - throw Exception\CacheException::fromNonMultiPutCache($this->doctrineCache); + + if (!$this->doctrineCache instanceof MultiOperationCache) { + throw Exception\CacheException::fromNonMultiOperationCache($this->doctrineCache); } } @@ -110,7 +107,7 @@ public function set($key, $value, $ttl = null) : bool $ttl = $this->convertDateIntervalToInteger($ttl); } - if (!is_integer($ttl)) { + if (!is_int($ttl)) { throw InvalidArgumentException::fromKeyAndInvalidTTL($key, $ttl); } @@ -144,7 +141,7 @@ public function clear() : bool * @return array * @throws \Roave\DoctrineSimpleCache\Exception\InvalidArgumentException */ - public function getMultiple($keys, $default = null) + public function getMultiple($keys, $default = null) : array { $keys = $this->filterValidateMultipleKeys($keys); return array_merge(array_fill_keys($keys, $default), $this->doctrineCache->fetchMultiple($keys)); @@ -173,10 +170,10 @@ public function setMultiple($values, $ttl = null) : bool } if ($ttl instanceof \DateInterval) { - $ttl = self::convertDateIntervalToInteger($ttl); + $ttl = $this->convertDateIntervalToInteger($ttl); } - if (!is_integer($ttl)) { + if (!is_int($ttl)) { throw InvalidArgumentException::fromKeyAndInvalidTTL(key($validatedValues), $ttl); } @@ -194,16 +191,7 @@ public function setMultiple($values, $ttl = null) : bool */ public function deleteMultiple($keys) : bool { - $keys = $this->filterValidateMultipleKeys($keys); - - $success = true; - foreach ($keys as $key) { - if (!$this->delete($key)) { - $success = false; - } - } - - return $success; + return $this->doctrineCache->deleteMultiple($this->filterValidateMultipleKeys($keys)); } /** diff --git a/test/asset/FullyImplementedCache.php b/test/asset/FullyImplementedCache.php index a63554a..85ae9c0 100644 --- a/test/asset/FullyImplementedCache.php +++ b/test/asset/FullyImplementedCache.php @@ -5,9 +5,8 @@ use Doctrine\Common\Cache\Cache; use Doctrine\Common\Cache\ClearableCache; -use Doctrine\Common\Cache\MultiGetCache; -use Doctrine\Common\Cache\MultiPutCache; +use Doctrine\Common\Cache\MultiOperationCache; -interface FullyImplementedCache extends Cache, ClearableCache, MultiGetCache, MultiPutCache +interface FullyImplementedCache extends Cache, ClearableCache, MultiOperationCache { } diff --git a/test/asset/NotClearableCache.php b/test/asset/NotClearableCache.php index 8e8144a..5d79f9e 100644 --- a/test/asset/NotClearableCache.php +++ b/test/asset/NotClearableCache.php @@ -4,9 +4,8 @@ namespace RoaveTestAsset\DoctrineSimpleCache; use Doctrine\Common\Cache\Cache; -use Doctrine\Common\Cache\MultiGetCache; -use Doctrine\Common\Cache\MultiPutCache; +use Doctrine\Common\Cache\MultiOperationCache; -interface NotClearableCache extends Cache, MultiGetCache, MultiPutCache +interface NotClearableCache extends Cache, MultiOperationCache { } diff --git a/test/asset/NotMultiGettableCache.php b/test/asset/NotMultiOperationCache.php similarity index 57% rename from test/asset/NotMultiGettableCache.php rename to test/asset/NotMultiOperationCache.php index 9fd4f7c..c9451c5 100644 --- a/test/asset/NotMultiGettableCache.php +++ b/test/asset/NotMultiOperationCache.php @@ -5,8 +5,7 @@ use Doctrine\Common\Cache\Cache; use Doctrine\Common\Cache\ClearableCache; -use Doctrine\Common\Cache\MultiPutCache; -interface NotMultiGettableCache extends Cache, ClearableCache, MultiPutCache +interface NotMultiOperationCache extends Cache, ClearableCache { } diff --git a/test/asset/NotMultiPuttableCache.php b/test/asset/NotMultiPuttableCache.php deleted file mode 100644 index cf36278..0000000 --- a/test/asset/NotMultiPuttableCache.php +++ /dev/null @@ -1,12 +0,0 @@ -createMock(DoctrineCache::class); - $exception = CacheException::fromNonMultiGetCache($doctrineCache); + $exception = CacheException::fromNonMultiOperationCache($doctrineCache); self::assertInstanceOf(CacheException::class, $exception); self::assertInstanceOf(PsrCacheException::class, $exception); self::assertStringMatchesFormat( - 'The given cache %s cannot multi-get, but you tried to use a feature that requires a multi-get cache.', - $exception->getMessage() - ); - } - - public function testFromNonMultiPutCache() - { - /** @var DoctrineCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ - $doctrineCache = $this->createMock(DoctrineCache::class); - - $exception = CacheException::fromNonMultiPutCache($doctrineCache); - - self::assertInstanceOf(CacheException::class, $exception); - self::assertInstanceOf(PsrCacheException::class, $exception); - - self::assertStringMatchesFormat( - 'The given cache %s cannot multi-put, but you tried to use a feature that requires a multi-put cache.', + 'The given cache %s does not support multiple operations, ' + . 'but you tried to use a feature that requires a multi-operation cache.', $exception->getMessage() ); } diff --git a/test/unit/SimpleCacheAdapterTest.php b/test/unit/SimpleCacheAdapterTest.php index cdb26f2..84cbbed 100644 --- a/test/unit/SimpleCacheAdapterTest.php +++ b/test/unit/SimpleCacheAdapterTest.php @@ -9,8 +9,7 @@ use Roave\DoctrineSimpleCache\SimpleCacheAdapter; use RoaveTestAsset\DoctrineSimpleCache\FullyImplementedCache; use RoaveTestAsset\DoctrineSimpleCache\NotClearableCache; -use RoaveTestAsset\DoctrineSimpleCache\NotMultiGettableCache; -use RoaveTestAsset\DoctrineSimpleCache\NotMultiPuttableCache; +use RoaveTestAsset\DoctrineSimpleCache\NotMultiOperationCache; /** * @covers \Roave\DoctrineSimpleCache\SimpleCacheAdapter @@ -65,10 +64,10 @@ public function invalidKeys() ]; } - public function testConstructorThrowsExceptionWhenNotMultiPuttableCacheIsUsed() + public function testConstructorThrowsExceptionWhenNotMultiOperationCacheIsUsed() { - /** @var NotMultiPuttableCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ - $doctrineCache = $this->createMock(NotMultiPuttableCache::class); + /** @var NotMultiOperationCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ + $doctrineCache = $this->createMock(NotMultiOperationCache::class); $this->expectException(CacheException::class); new SimpleCacheAdapter($doctrineCache); @@ -83,15 +82,6 @@ public function testConstructorThrowsExceptionWhenNotClearableCacheIsUsed() new SimpleCacheAdapter($doctrineCache); } - public function testConstructorThrowsExceptionWhenNotMultiGettableCacheIsUsed() - { - /** @var NotMultiGettableCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ - $doctrineCache = $this->createMock(NotMultiGettableCache::class); - - $this->expectException(CacheException::class); - new SimpleCacheAdapter($doctrineCache); - } - public function testGetProxiesToDoctrineFetch() { $key = uniqid('key', true); @@ -407,8 +397,7 @@ public function testDeleteMultipleReturnsTrueWhenAllDeletesSucceed() /** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ $doctrineCache = $this->createMock(FullyImplementedCache::class); - $doctrineCache->expects(self::at(0))->method('delete')->with($keys[0])->willReturn(true); - $doctrineCache->expects(self::at(1))->method('delete')->with($keys[1])->willReturn(true); + $doctrineCache->expects(self::once())->method('deleteMultiple')->with($keys)->willReturn(true); $psrCache = new SimpleCacheAdapter($doctrineCache); self::assertTrue($psrCache->deleteMultiple($keys)); @@ -423,8 +412,7 @@ public function testDeleteMultipleReturnsFalseWhenOneDeleteFails() /** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ $doctrineCache = $this->createMock(FullyImplementedCache::class); - $doctrineCache->expects(self::at(0))->method('delete')->with($keys[0])->willReturn(false); - $doctrineCache->expects(self::at(1))->method('delete')->with($keys[1])->willReturn(true); + $doctrineCache->expects(self::once())->method('deleteMultiple')->with($keys)->willReturn(false); $psrCache = new SimpleCacheAdapter($doctrineCache); self::assertFalse($psrCache->deleteMultiple($keys));