Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 10 additions & 1 deletion lib/Phpfastcache/Helper/Psr16Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ public function clear(): bool
*/
public function getMultiple($keys, $default = null)
{
if ($keys instanceof \Traversable) {
$keys = \iterator_to_array($keys);
}
try {
return array_map(function (ExtendedCacheItemInterface $item) {
return $item->get();
Expand Down Expand Up @@ -175,7 +178,13 @@ public function setMultiple($values, $ttl = null): bool
public function deleteMultiple($keys): bool
{
try {
return $this->internalCacheInstance->deleteItems($keys);
if ($keys instanceof \Traversable) {
return $this->internalCacheInstance->deleteItems(\iterator_to_array($keys));
} elseif (is_array($keys)) {
return $this->internalCacheInstance->deleteItems($keys);
} else {
throw new phpFastCacheInvalidArgumentException('$keys must be an array/Traversable instance.');
}
} catch (PhpfastcacheInvalidArgumentException $e) {
throw new PhpfastcacheSimpleCacheException($e->getMessage(), 0, $e);
}
Expand Down
68 changes: 55 additions & 13 deletions tests/Psr16Adapter.test.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,27 @@
$value = str_shuffle(uniqid('pfc', true));

if(!$Psr16Adapter->has('test-key')){
$testHelper->printPassText('1/6 Psr16 hasser returned expected boolean (false)');
$testHelper->printPassText('1/9 Psr16 hasser returned expected boolean (false)');
}else{
$testHelper->printFailText('1/6 Psr16 hasser returned unexpected boolean (true)');
$testHelper->printFailText('1/9 Psr16 hasser returned unexpected boolean (true)');
}

$testHelper->printNewLine()->printText('Setting up value to "test-key"...')->printNewLine();
$Psr16Adapter->set('test-key', $value);

if($Psr16Adapter->get('test-key') === $value){
$testHelper->printPassText('2/6 Psr16 getter returned expected value: ' . $value);
$testHelper->printPassText('2/9 Psr16 getter returned expected value: ' . $value);
}else{
$testHelper->printFailText('2/6 Psr16 getter returned unexpected value: ' . $value);
$testHelper->printFailText('2/9 Psr16 getter returned unexpected value: ' . $value);
}

$testHelper->printNewLine()->printText('Deleting key "test-key"...')->printNewLine();
$Psr16Adapter->delete('test-key');

if(!$Psr16Adapter->has('test-key')){
$testHelper->printPassText('3/6 Psr16 hasser returned expected boolean (false)');
$testHelper->printPassText('3/9 Psr16 hasser returned expected boolean (false)');
}else{
$testHelper->printFailText('3/6 Psr16 hasser returned unexpected boolean (true)');
$testHelper->printFailText('3/9 Psr16 hasser returned unexpected boolean (true)');
}

$testHelper->printNewLine()->printText('Setting up value to "test-key, test-key2, test-key3"...')->printNewLine();
Expand All @@ -51,9 +51,16 @@

$values = $Psr16Adapter->getMultiple(['test-key', 'test-key2', 'test-key3']);
if(count(array_filter($values)) === 3){
$testHelper->printPassText('4/6 Psr16 multiple getters returned expected values (3)');
$testHelper->printPassText('4/9 Psr16 multiple getters returned expected values (3)');
}else{
$testHelper->printFailText('4/6 Psr16 getters(3) returned unexpected values.');
$testHelper->printFailText('4/9 Psr16 getters(3) returned unexpected values.');
}

$values = $Psr16Adapter->getMultiple(new ArrayObject(['test-key', 'test-key2', 'test-key3']));
if(count(array_filter($values)) === 3){
$testHelper->printPassText('5/9 Psr16 multiple getters with Traversable returned expected values (3)');
}else{
$testHelper->printFailText('5/9 Psr16 getters(3) with Traversable returned unexpected values.');
}

$testHelper->printNewLine()->printText('Clearing whole cache ...')->printNewLine();
Expand All @@ -67,18 +74,53 @@
]);

if($Psr16Adapter->has('test-key') && $Psr16Adapter->has('test-key2') && $Psr16Adapter->has('test-key3')){
$testHelper->printPassText('5/6 Psr16 hasser returned expected booleans (true)');
$testHelper->printPassText('6/9 Psr16 hasser returned expected booleans (true)');
}else{
$testHelper->printFailText('6/9 Psr16 hasser returned one or more unexpected boolean (false)');
}

$testHelper->printNewLine()->printText('Clearing whole cache ...')->printNewLine();
$Psr16Adapter->clear();

$testHelper->printText('Setting multiple values using a Traversable to "test-key, test-key2, test-key3"...')->printNewLine();
$Psr16Adapter->setMultiple(new ArrayObject([
'test-key' => $value,
'test-key2' => $value,
'test-key3' => $value
]));

if($Psr16Adapter->has('test-key') && $Psr16Adapter->has('test-key2') && $Psr16Adapter->has('test-key3')){
$testHelper->printPassText('7/9 Psr16 hasser returned expected booleans (true)');
}else{
$testHelper->printFailText('5/6 Psr16 hasser returned one or more unexpected boolean (false)');
$testHelper->printFailText('7/9 Psr16 hasser returned one or more unexpected boolean (false)');
}

$testHelper->printNewLine()->printText('Deleting up keys "test-key, test-key2, test-key3"...')->printNewLine();
$Psr16Adapter->deleteMultiple(['test-key', 'test-key2', 'test-key3']);

if(!$Psr16Adapter->has('test-key') && !$Psr16Adapter->has('test-key2') && !$Psr16Adapter->has('test-key3')){
$testHelper->printPassText('6/6 Psr16 hasser returned expected booleans (false)');
$testHelper->printPassText('8/9 Psr16 hasser returned expected booleans (false)');
}else{
$testHelper->printFailText('8/9 Psr16 hasser returned one or more unexpected boolean (true)');
}

$testHelper->printNewLine()->printText('Clearing whole cache ...')->printNewLine();
$Psr16Adapter->clear();
$testHelper->printText('Setting up value to "test-key, test-key2, test-key3"...')->printNewLine();
$Psr16Adapter->setMultiple([
'test-key' => $value,
'test-key2' => $value,
'test-key3' => $value
]);

$testHelper->printText('Deleting up keys "test-key, test-key2, test-key3"... from a Traversable')->printNewLine();
$traversable = new ArrayObject(['test-key', 'test-key2', 'test-key3']);
$Psr16Adapter->deleteMultiple($traversable);

if(!$Psr16Adapter->has('test-key') && !$Psr16Adapter->has('test-key2') && !$Psr16Adapter->has('test-key3')){
$testHelper->printPassText('9/9 Psr16 hasser returned expected booleans (false)');
}else{
$testHelper->printFailText('6/6 Psr16 hasser returned one or more unexpected boolean (true)');
$testHelper->printFailText('9/9 Psr16 hasser returned one or more unexpected boolean (true)');
}

$testHelper->terminateTest();
$testHelper->terminateTest();