11<?php
2+ declare (strict_types=1 );
23
34namespace Kodus \PredisSimpleCache \Test ;
45
910use Kodus \PredisSimpleCache \PredisSimpleCache ;
1011use Psr \SimpleCache \CacheInterface ;
1112use Psr \SimpleCache \InvalidArgumentException ;
13+ use TypeError ;
1214
1315class PredisSimpleCacheCest
1416{
@@ -261,7 +263,7 @@ public function testGetMultipleInvalidKeys(IntegrationTester $I, Example $exampl
261263
262264 public function getMultipleNoIterable (IntegrationTester $ I ): void
263265 {
264- $ I ->expectThrowable (InvalidArgumentException ::class, fn () => $ this ->cache ->getMultiple ('key ' ));
266+ $ I ->expectThrowable (TypeError ::class, fn () => $ this ->cache ->getMultiple ('key ' ));
265267 }
266268
267269 /**
@@ -290,7 +292,7 @@ public function setMultipleInvalidKeys(IntegrationTester $I, Example $example):
290292
291293 public function setMultipleNoIterable (IntegrationTester $ I ): void
292294 {
293- $ I ->expectThrowable (InvalidArgumentException ::class, fn () => $ this ->cache ->setMultiple ('key ' ));
295+ $ I ->expectThrowable (TypeError ::class, fn () => $ this ->cache ->setMultiple ('key ' ));
294296 }
295297
296298 /**
@@ -326,7 +328,7 @@ public function deleteMultipleInvalidKeys(IntegrationTester $I, Example $example
326328
327329 public function deleteMultipleNoIterable (IntegrationTester $ I ): void
328330 {
329- $ I ->expectThrowable (InvalidArgumentException ::class, fn () => $ this ->cache ->deleteMultiple ('key ' ));
331+ $ I ->expectThrowable (TypeError ::class, fn () => $ this ->cache ->deleteMultiple ('key ' ));
330332 }
331333
332334 /**
@@ -336,7 +338,7 @@ public function setInvalidTtl(IntegrationTester $I, Example $example): void
336338 {
337339 $ ttl = $ example ['ttl ' ];
338340
339- $ I ->expectThrowable (InvalidArgumentException ::class, fn () => $ this ->cache ->set ('key ' , 'value ' , $ ttl ));
341+ $ I ->expectThrowable (TypeError ::class, fn () => $ this ->cache ->set ('key ' , 'value ' , $ ttl ));
340342 }
341343
342344 /**
@@ -347,7 +349,7 @@ public function setMultipleInvalidTtl(IntegrationTester $I, Example $example): v
347349 $ ttl = $ example ['ttl ' ];
348350
349351 $ I ->expectThrowable (
350- InvalidArgumentException ::class,
352+ TypeError ::class,
351353 fn () => $ this ->cache ->setMultiple (['key ' => 'value ' ], $ ttl )
352354 );
353355 }
@@ -575,24 +577,38 @@ protected function validData(): array
575577 ];
576578 }
577579
578- protected function invalidKeys (): array
580+ /**
581+ * ['0' => 'some data'] and [0 => 'some data'] are equivalent in PHP, so we have to accept integers as keys, when
582+ * reading or writing multiple entries with getMultiple() or setMultiple().
583+ *
584+ * But when using get() or set(), integer keys are clearly integers and are considered invalid.
585+ */
586+ protected function invalidKeyTypes (): array
579587 {
580588 return array_merge (
581- $ this ->invalidArrayKeys (),
589+ $ this ->invalidArrayKeyTypes (),
582590 [
583591 ['key ' => 0 ],
584592 ['key ' => 2 ],
585593 ]);
586594 }
587595
588- protected function invalidArrayKeys (): array
596+ protected function invalidArrayKeyTypes (): array
589597 {
590598 return [
591- ['key ' => '' ],
599+ ['key ' => new \ stdClass () ],
592600 ['key ' => true ],
593601 ['key ' => false ],
594602 ['key ' => null ],
595603 ['key ' => 2.5 ],
604+ ['key ' => ['array ' ]],
605+ ];
606+ }
607+
608+ protected function invalidKeys (): array
609+ {
610+ return [
611+ ['key ' => '' ],
596612 ['key ' => '{str ' ],
597613 ['key ' => 'rand{ ' ],
598614 ['key ' => 'rand{str ' ],
@@ -603,21 +619,27 @@ protected function invalidArrayKeys(): array
603619 ['key ' => 'rand \\str ' ],
604620 ['key ' => 'rand@str ' ],
605621 ['key ' => 'rand:str ' ],
606- ['key ' => new \stdClass ()],
607- ['key ' => ['array ' ]],
608622 ];
609623 }
610624
611- protected function invalidTTL (): array
625+ protected function invalidArrayKeys (): array
626+ {
627+ return array_merge (
628+ $ this ->invalidKeys (),
629+ $ this ->invalidArrayKeyTypes (),
630+ );
631+ }
632+
633+ protected function invalidTtl (): array
612634 {
613635 return [
614636 ['ttl ' => '' ],
615637 ['ttl ' => true ],
616638 ['ttl ' => false ],
617639 ['ttl ' => 'abc ' ],
618640 ['ttl ' => 2.5 ],
619- ['ttl ' => ' 1 ' ], // Could be cast to a int
620- ['ttl ' => '12foo ' ], // Could be cast to a int
641+ ['ttl ' => ' 1 ' ], // Could be cast to an int
642+ ['ttl ' => '12foo ' ], // Could be cast to an int
621643 ['ttl ' => '025 ' ], // Could be interpreted as hex
622644 ['ttl ' => new \stdClass ()],
623645 ['ttl ' => ['array ' ]],
0 commit comments