From e021f6ccff646ca765da321a3a699aca9a39d068 Mon Sep 17 00:00:00 2001 From: Alexander Schenkel Date: Tue, 3 Sep 2019 15:03:32 +0200 Subject: [PATCH 1/2] fixes #700 vor v6: Psr16Adapter::deleteMultiple converts $keys to an array. The internal Psr\Cache\CacheItemPoolInterface::deleteItems requires an array as argument. Some popular tools like PhpSpreadsheet use a Traversable instead. This is the fix for the v6 branch. --- src/phpFastCache/Helper/Psr16Adapter.php | 8 ++++- tests/Psr16Adapter.test.php | 45 +++++++++++++++++------- 2 files changed, 39 insertions(+), 14 deletions(-) diff --git a/src/phpFastCache/Helper/Psr16Adapter.php b/src/phpFastCache/Helper/Psr16Adapter.php index 85448d25c..fa9b50870 100644 --- a/src/phpFastCache/Helper/Psr16Adapter.php +++ b/src/phpFastCache/Helper/Psr16Adapter.php @@ -167,7 +167,13 @@ public function setMultiple($values, $ttl = null) public function deleteMultiple($keys) { 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(), null, $e); } diff --git a/tests/Psr16Adapter.test.php b/tests/Psr16Adapter.test.php index c4e4d7215..84cfb3477 100644 --- a/tests/Psr16Adapter.test.php +++ b/tests/Psr16Adapter.test.php @@ -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/7 Psr16 hasser returned expected boolean (false)'); }else{ - $testHelper->printFailText('1/6 Psr16 hasser returned unexpected boolean (true)'); + $testHelper->printFailText('1/7 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/7 Psr16 getter returned expected value: ' . $value); }else{ - $testHelper->printFailText('2/6 Psr16 getter returned unexpected value: ' . $value); + $testHelper->printFailText('2/7 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/7 Psr16 hasser returned expected boolean (false)'); }else{ - $testHelper->printFailText('3/6 Psr16 hasser returned unexpected boolean (true)'); + $testHelper->printFailText('3/7 Psr16 hasser returned unexpected boolean (true)'); } $testHelper->printNewLine()->printText('Setting up value to "test-key, test-key2, test-key3"...')->printNewLine(); @@ -51,9 +51,9 @@ $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/7 Psr16 multiple getters returned expected values (3)'); }else{ - $testHelper->printFailText('4/6 Psr16 getters(3) returned unexpected values.'); + $testHelper->printFailText('4/7 Psr16 getters(3) returned unexpected values.'); } $testHelper->printNewLine()->printText('Clearing whole cache ...')->printNewLine(); @@ -67,18 +67,37 @@ ]); 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('5/7 Psr16 hasser returned expected booleans (true)'); }else{ - $testHelper->printFailText('5/6 Psr16 hasser returned one or more unexpected boolean (false)'); + $testHelper->printFailText('5/7 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('6/7 Psr16 hasser returned expected booleans (false)'); }else{ - $testHelper->printFailText('6/6 Psr16 hasser returned one or more unexpected boolean (true)'); + $testHelper->printFailText('6/7 Psr16 hasser returned one or more unexpected boolean (true)'); } -$testHelper->terminateTest(); \ No newline at end of file +$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('7/7 Psr16 hasser returned expected booleans (false)'); +} else { + $testHelper->printFailText('7/7 Psr16 hasser returned one or more unexpected boolean (true)'); +} + +$testHelper->terminateTest(); From f9397a2944b54f45cf93b7aebfb0d6145b4a38d2 Mon Sep 17 00:00:00 2001 From: Alexander Schenkel Date: Tue, 3 Sep 2019 15:52:35 +0200 Subject: [PATCH 2/2] fixes #700 for v6: Psr16Adapter::getMultiple() now converts Traversable $keys in an array The internal array_map function can only be used with native arrays. --- src/phpFastCache/Helper/Psr16Adapter.php | 3 ++ tests/Psr16Adapter.test.php | 51 +++++++++++++++++------- 2 files changed, 40 insertions(+), 14 deletions(-) diff --git a/src/phpFastCache/Helper/Psr16Adapter.php b/src/phpFastCache/Helper/Psr16Adapter.php index fa9b50870..4d79cd984 100644 --- a/src/phpFastCache/Helper/Psr16Adapter.php +++ b/src/phpFastCache/Helper/Psr16Adapter.php @@ -124,6 +124,9 @@ public function clear() */ 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(); diff --git a/tests/Psr16Adapter.test.php b/tests/Psr16Adapter.test.php index 84cfb3477..957b05681 100644 --- a/tests/Psr16Adapter.test.php +++ b/tests/Psr16Adapter.test.php @@ -18,27 +18,27 @@ $value = str_shuffle(uniqid('pfc', true)); if(!$Psr16Adapter->has('test-key')){ - $testHelper->printPassText('1/7 Psr16 hasser returned expected boolean (false)'); + $testHelper->printPassText('1/9 Psr16 hasser returned expected boolean (false)'); }else{ - $testHelper->printFailText('1/7 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/7 Psr16 getter returned expected value: ' . $value); + $testHelper->printPassText('2/9 Psr16 getter returned expected value: ' . $value); }else{ - $testHelper->printFailText('2/7 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/7 Psr16 hasser returned expected boolean (false)'); + $testHelper->printPassText('3/9 Psr16 hasser returned expected boolean (false)'); }else{ - $testHelper->printFailText('3/7 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(); @@ -51,9 +51,16 @@ $values = $Psr16Adapter->getMultiple(['test-key', 'test-key2', 'test-key3']); if(count(array_filter($values)) === 3){ - $testHelper->printPassText('4/7 Psr16 multiple getters returned expected values (3)'); + $testHelper->printPassText('4/9 Psr16 multiple getters returned expected values (3)'); }else{ - $testHelper->printFailText('4/7 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(); @@ -67,18 +74,34 @@ ]); if($Psr16Adapter->has('test-key') && $Psr16Adapter->has('test-key2') && $Psr16Adapter->has('test-key3')){ - $testHelper->printPassText('5/7 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/7 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/7 Psr16 hasser returned expected booleans (false)'); + $testHelper->printPassText('8/9 Psr16 hasser returned expected booleans (false)'); }else{ - $testHelper->printFailText('6/7 Psr16 hasser returned one or more unexpected boolean (true)'); + $testHelper->printFailText('8/9 Psr16 hasser returned one or more unexpected boolean (true)'); } $testHelper->printNewLine()->printText('Clearing whole cache ...')->printNewLine(); @@ -95,9 +118,9 @@ $Psr16Adapter->deleteMultiple($traversable); if (!$Psr16Adapter->has('test-key') && !$Psr16Adapter->has('test-key2') && !$Psr16Adapter->has('test-key3')) { - $testHelper->printPassText('7/7 Psr16 hasser returned expected booleans (false)'); + $testHelper->printPassText('9/9 Psr16 hasser returned expected booleans (false)'); } else { - $testHelper->printFailText('7/7 Psr16 hasser returned one or more unexpected boolean (true)'); + $testHelper->printFailText('9/9 Psr16 hasser returned one or more unexpected boolean (true)'); } $testHelper->terminateTest();