Skip to content

Commit

Permalink
Merge pull request #101 from laminas/2.10.x-merge-up-into-2.11.x_6090…
Browse files Browse the repository at this point in the history
…5718b1bfe5.09583286

Merge release 2.10.3 into 2.11.x
  • Loading branch information
boesing committed May 3, 2021
2 parents f45e916 + 8ff716e commit f888588
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 3 deletions.
19 changes: 17 additions & 2 deletions src/Psr/CacheItemPool/CacheItemPoolDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
use Laminas\Cache\Storage\StorageInterface;
use Psr\Cache\CacheItemInterface;
use Psr\Cache\CacheItemPoolInterface;
use function array_unique;
use function in_array;
use function is_array;

/**
* Decorate laminas-cache adapters as PSR-6 cache item pools.
Expand Down Expand Up @@ -194,13 +197,25 @@ public function deleteItems(array $keys)
$this->deferred = array_diff_key($this->deferred, array_flip($keys));

try {
return $this->storage->removeItems($keys) === [];
$result = $this->storage->removeItems($keys);
} catch (Exception\InvalidArgumentException $e) {
throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
} catch (Exception\ExceptionInterface $e) {
return false;
}

return false;
// BC compatibility can be removed in 3.0
if (! is_array($result)) {
return $result !== null;
}

if ($result === []) {
return true;
}

$existing = $this->storage->hasItems($result);
$unified = array_unique($existing);
return ! in_array(true, $unified, true);
}

/**
Expand Down
42 changes: 42 additions & 0 deletions test/Psr/CacheItemPool/CacheItemPoolDecoratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -531,4 +531,46 @@ private function getAdapter(?ObjectProphecy $storage = null): CacheItemPoolDecor
assert($revealedStorage instanceof StorageInterface);
return new CacheItemPoolDecorator($revealedStorage);
}

public function testCanHandleRemoveItemsReturningNonArray()
{
$adapter = $this->getStorageProphecy();
$adapter
->removeItems(Argument::type('array'))
->willReturn(null);

$cache = new CacheItemPoolDecorator($adapter->reveal());

self::assertFalse($cache->deleteItems(['foo']));
}

/**
* @param bool $exists
* @param bool $sucsessful
*
* @dataProvider deletionVerificationProvider
*/
public function testWillVerifyKeyExistenceByUsingHasItemsWhenDeletionWasNotSuccessful($exists, $sucsessful)
{
$adapter = $this->getStorageProphecy();
$adapter
->removeItems(Argument::type('array'))
->willReturn(['foo']);

$adapter
->hasItems(Argument::exact(['foo']))
->willReturn(['foo' => $exists]);

$cache = new CacheItemPoolDecorator($adapter->reveal());

self::assertEquals($sucsessful, $cache->deleteItems(['foo']));
}

public function deletionVerificationProvider()
{
return [
'deletion failed due to hasItems states the key still exists' => [true, false],
'deletion successful due to hasItems states the key does not exist' => [false, true],
];
}
}
12 changes: 11 additions & 1 deletion test/Psr/CacheItemPool/MockStorageTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@ protected function getStorageProphecy($capabilities = false, $options = false, $
return $adapterOptions;
});

$storage->hasItems(Argument::type('array'))
->will(function ($args) use (&$items) {
$keys = $args[0];
$status = [];
foreach ($keys as $key) {
$status[$key] = array_key_exists($key, $items);
}

return $status;
});
$storage->hasItem(Argument::type('string'))
->will(function ($args) use (&$items) {
$key = $args[0];
Expand Down Expand Up @@ -118,7 +128,7 @@ protected function getStorageProphecy($capabilities = false, $options = false, $
$storage->removeItems(Argument::type('array'))
->will(function ($args) use (&$items) {
$items = array_diff_key($items, array_flip($args[0]));
return true;
return [];
});

return $storage;
Expand Down

0 comments on commit f888588

Please sign in to comment.