Skip to content

Commit 2cb775c

Browse files
authored
Merge pull request #703 from bylexus/fix_issue_700_delete_multiple
fixes #700: Psr16Adapter::deleteMultiple converts $keys to an array.
2 parents 347a04b + d7bf4e7 commit 2cb775c

File tree

2 files changed

+65
-14
lines changed

2 files changed

+65
-14
lines changed

lib/Phpfastcache/Helper/Psr16Adapter.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,9 @@ public function clear(): bool
132132
*/
133133
public function getMultiple($keys, $default = null)
134134
{
135+
if ($keys instanceof \Traversable) {
136+
$keys = \iterator_to_array($keys);
137+
}
135138
try {
136139
return array_map(function (ExtendedCacheItemInterface $item) {
137140
return $item->get();
@@ -175,7 +178,13 @@ public function setMultiple($values, $ttl = null): bool
175178
public function deleteMultiple($keys): bool
176179
{
177180
try {
178-
return $this->internalCacheInstance->deleteItems($keys);
181+
if ($keys instanceof \Traversable) {
182+
return $this->internalCacheInstance->deleteItems(\iterator_to_array($keys));
183+
} elseif (is_array($keys)) {
184+
return $this->internalCacheInstance->deleteItems($keys);
185+
} else {
186+
throw new phpFastCacheInvalidArgumentException('$keys must be an array/Traversable instance.');
187+
}
179188
} catch (PhpfastcacheInvalidArgumentException $e) {
180189
throw new PhpfastcacheSimpleCacheException($e->getMessage(), 0, $e);
181190
}

tests/Psr16Adapter.test.php

Lines changed: 55 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,27 @@
1818
$value = str_shuffle(uniqid('pfc', true));
1919

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

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

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

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

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

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

5252
$values = $Psr16Adapter->getMultiple(['test-key', 'test-key2', 'test-key3']);
5353
if(count(array_filter($values)) === 3){
54-
$testHelper->printPassText('4/6 Psr16 multiple getters returned expected values (3)');
54+
$testHelper->printPassText('4/9 Psr16 multiple getters returned expected values (3)');
5555
}else{
56-
$testHelper->printFailText('4/6 Psr16 getters(3) returned unexpected values.');
56+
$testHelper->printFailText('4/9 Psr16 getters(3) returned unexpected values.');
57+
}
58+
59+
$values = $Psr16Adapter->getMultiple(new ArrayObject(['test-key', 'test-key2', 'test-key3']));
60+
if(count(array_filter($values)) === 3){
61+
$testHelper->printPassText('5/9 Psr16 multiple getters with Traversable returned expected values (3)');
62+
}else{
63+
$testHelper->printFailText('5/9 Psr16 getters(3) with Traversable returned unexpected values.');
5764
}
5865

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

6976
if($Psr16Adapter->has('test-key') && $Psr16Adapter->has('test-key2') && $Psr16Adapter->has('test-key3')){
70-
$testHelper->printPassText('5/6 Psr16 hasser returned expected booleans (true)');
77+
$testHelper->printPassText('6/9 Psr16 hasser returned expected booleans (true)');
78+
}else{
79+
$testHelper->printFailText('6/9 Psr16 hasser returned one or more unexpected boolean (false)');
80+
}
81+
82+
$testHelper->printNewLine()->printText('Clearing whole cache ...')->printNewLine();
83+
$Psr16Adapter->clear();
84+
85+
$testHelper->printText('Setting multiple values using a Traversable to "test-key, test-key2, test-key3"...')->printNewLine();
86+
$Psr16Adapter->setMultiple(new ArrayObject([
87+
'test-key' => $value,
88+
'test-key2' => $value,
89+
'test-key3' => $value
90+
]));
91+
92+
if($Psr16Adapter->has('test-key') && $Psr16Adapter->has('test-key2') && $Psr16Adapter->has('test-key3')){
93+
$testHelper->printPassText('7/9 Psr16 hasser returned expected booleans (true)');
7194
}else{
72-
$testHelper->printFailText('5/6 Psr16 hasser returned one or more unexpected boolean (false)');
95+
$testHelper->printFailText('7/9 Psr16 hasser returned one or more unexpected boolean (false)');
7396
}
7497

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

78101
if(!$Psr16Adapter->has('test-key') && !$Psr16Adapter->has('test-key2') && !$Psr16Adapter->has('test-key3')){
79-
$testHelper->printPassText('6/6 Psr16 hasser returned expected booleans (false)');
102+
$testHelper->printPassText('8/9 Psr16 hasser returned expected booleans (false)');
103+
}else{
104+
$testHelper->printFailText('8/9 Psr16 hasser returned one or more unexpected boolean (true)');
105+
}
106+
107+
$testHelper->printNewLine()->printText('Clearing whole cache ...')->printNewLine();
108+
$Psr16Adapter->clear();
109+
$testHelper->printText('Setting up value to "test-key, test-key2, test-key3"...')->printNewLine();
110+
$Psr16Adapter->setMultiple([
111+
'test-key' => $value,
112+
'test-key2' => $value,
113+
'test-key3' => $value
114+
]);
115+
116+
$testHelper->printText('Deleting up keys "test-key, test-key2, test-key3"... from a Traversable')->printNewLine();
117+
$traversable = new ArrayObject(['test-key', 'test-key2', 'test-key3']);
118+
$Psr16Adapter->deleteMultiple($traversable);
119+
120+
if(!$Psr16Adapter->has('test-key') && !$Psr16Adapter->has('test-key2') && !$Psr16Adapter->has('test-key3')){
121+
$testHelper->printPassText('9/9 Psr16 hasser returned expected booleans (false)');
80122
}else{
81-
$testHelper->printFailText('6/6 Psr16 hasser returned one or more unexpected boolean (true)');
123+
$testHelper->printFailText('9/9 Psr16 hasser returned one or more unexpected boolean (true)');
82124
}
83125

84-
$testHelper->terminateTest();
126+
$testHelper->terminateTest();

0 commit comments

Comments
 (0)