Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for Doctrine\Cache 1.7 with deleteMultiple #22

Merged
merged 3 commits into from
Sep 14, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
21 changes: 3 additions & 18 deletions src/Exception/CacheException.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,11 @@

final class CacheException extends \RuntimeException implements PsrCacheException
{
public static function fromNonClearableCache(DoctrineCache $cache) : self
public static function fromNonMultiOperationCache(DoctrineCache $cache) : self
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor BC break: requires UPGRADE.md notes or revert + deprecation

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fromNonClearableCache shouldn't have gone, oops. Added that back in :)

{
return new self(sprintf(
'The given cache %s was not clearable, but you tried to use a feature that requires a clearable cache.',
get_class($cache)
));
}

public static function fromNonMultiGetCache(DoctrineCache $cache) : self
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor BC break: requires UPGRADE.md notes or revert + deprecation

{
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor BC break: requires UPGRADE.md notes or revert + deprecation

{
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)
));
}
Expand Down
34 changes: 9 additions & 25 deletions src/SimpleCacheAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -25,14 +24,8 @@ public function __construct(DoctrineCache $doctrineCache)
{
$this->doctrineCache = $doctrineCache;

if (!$this->doctrineCache instanceof ClearableCache) {
throw Exception\CacheException::fromNonClearableCache($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);
}
}

Expand Down Expand Up @@ -110,7 +103,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);
}

Expand Down Expand Up @@ -144,7 +137,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));
Expand Down Expand Up @@ -173,10 +166,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);
}

Expand All @@ -194,16 +187,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));
}

/**
Expand Down
5 changes: 2 additions & 3 deletions test/asset/FullyImplementedCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
}
5 changes: 2 additions & 3 deletions test/asset/NotClearableCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
}
12 changes: 0 additions & 12 deletions test/asset/NotMultiPuttableCache.php

This file was deleted.

39 changes: 4 additions & 35 deletions test/unit/Exception/CacheExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,50 +12,19 @@
*/
final class CacheExceptionTest extends \PHPUnit_Framework_TestCase
{
public function testFromNonClearableCache()
public function testFromNonMultiOperationCache()
{
/** @var DoctrineCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
$doctrineCache = $this->createMock(DoctrineCache::class);

$exception = CacheException::fromNonClearableCache($doctrineCache);
$exception = CacheException::fromNonMultiOperationCache($doctrineCache);

self::assertInstanceOf(CacheException::class, $exception);
self::assertInstanceOf(PsrCacheException::class, $exception);

self::assertStringMatchesFormat(
'The given cache %s was not clearable, but you tried to use a feature that requires a clearable cache.',
$exception->getMessage()
);
}

public function testFromNonMultiGetCache()
{
/** @var DoctrineCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
$doctrineCache = $this->createMock(DoctrineCache::class);

$exception = CacheException::fromNonMultiGetCache($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()
);
}
Expand Down
31 changes: 5 additions & 26 deletions test/unit/SimpleCacheAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -67,26 +66,8 @@ public function invalidKeys()

public function testConstructorThrowsExceptionWhenNotMultiPuttableCacheIsUsed()
{
/** @var NotMultiPuttableCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
$doctrineCache = $this->createMock(NotMultiPuttableCache::class);

$this->expectException(CacheException::class);
new SimpleCacheAdapter($doctrineCache);
}

public function testConstructorThrowsExceptionWhenNotClearableCacheIsUsed()
{
/** @var NotClearableCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
$doctrineCache = $this->createMock(NotClearableCache::class);

$this->expectException(CacheException::class);
new SimpleCacheAdapter($doctrineCache);
}

public function testConstructorThrowsExceptionWhenNotMultiGettableCacheIsUsed()
{
/** @var NotMultiGettableCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
$doctrineCache = $this->createMock(NotMultiGettableCache::class);
/** @var NotMultiOperationCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
$doctrineCache = $this->createMock(NotMultiOperationCache::class);

$this->expectException(CacheException::class);
new SimpleCacheAdapter($doctrineCache);
Expand Down Expand Up @@ -407,8 +388,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));
Expand All @@ -423,8 +403,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));
Expand Down