Skip to content

Commit cf42483

Browse files
Updated to simple-cache 3.0 and adjusted tests (#5)
* Updated to simple-cache 3.0 and adjusted tests
1 parent 5aa650b commit cf42483

File tree

3 files changed

+44
-22
lines changed

3 files changed

+44
-22
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"prefer-stable": true,
1414
"require": {
1515
"php": ">=8.0",
16-
"psr/simple-cache": "^2.0",
16+
"psr/simple-cache": "^2.0|^3.0",
1717
"predis/predis": "^1.1"
1818
},
1919
"autoload": {

src/PredisSimpleCache.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ public function __construct(Client $client, int $default_ttl)
2929
$this->default_ttl = $default_ttl;
3030
}
3131

32-
public function get($key, $default = null)
32+
public function get(string $key, mixed $default = null): mixed
3333
{
3434
$this->validateKey($key);
3535

3636
return $this->client->exists($key) ? unserialize($this->client->get($key)) : $default;
3737
}
3838

39-
public function set($key, $value, $ttl = null): bool
39+
public function set(string $key, mixed $value, null|int|\DateInterval $ttl = null): bool
4040
{
4141
$this->validateKey($key);
4242

@@ -57,7 +57,7 @@ public function set($key, $value, $ttl = null): bool
5757
}
5858
}
5959

60-
public function delete($key): bool
60+
public function delete(string $key): bool
6161
{
6262
$this->validateKey($key);
6363

@@ -73,7 +73,7 @@ public function clear(): bool
7373
return mb_strpos('OK', $result) !== false;
7474
}
7575

76-
public function getMultiple($keys, $default = null)
76+
public function getMultiple(iterable $keys, mixed $default = null): iterable
7777
{
7878
$this->validateIterable($keys);
7979

@@ -93,7 +93,7 @@ public function getMultiple($keys, $default = null)
9393
return $result;
9494
}
9595

96-
public function setMultiple($values, $ttl = null): bool
96+
public function setMultiple(iterable $values, null|int|\DateInterval $ttl = null): bool
9797
{
9898
$this->validateIterable($values);
9999

@@ -120,7 +120,7 @@ public function setMultiple($values, $ttl = null): bool
120120
return true;
121121
}
122122

123-
public function deleteMultiple($keys): bool
123+
public function deleteMultiple(iterable $keys): bool
124124
{
125125
/** @var $keys array|Traversable */
126126
$this->validateIterable($keys);
@@ -143,7 +143,7 @@ public function deleteMultiple($keys): bool
143143
return true;
144144
}
145145

146-
public function has($key): bool
146+
public function has(string $key): bool
147147
{
148148
$this->validateKey($key);
149149

tests/integration/PredisSimpleCacheCest.php

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
declare(strict_types=1);
23

34
namespace Kodus\PredisSimpleCache\Test;
45

@@ -9,6 +10,7 @@
910
use Kodus\PredisSimpleCache\PredisSimpleCache;
1011
use Psr\SimpleCache\CacheInterface;
1112
use Psr\SimpleCache\InvalidArgumentException;
13+
use TypeError;
1214

1315
class 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

Comments
 (0)