Skip to content

Commit

Permalink
Upgrade to simple cache 2 & 3 (#11)
Browse files Browse the repository at this point in the history
* Updated PHP requirements to >=8.0 and updated tests to codeception 5

* Removed mindplay/simple-cache dev-dependency

* Updated simple-cache to ^2||^3

* Updated MockCache::get signature

* Updated MockCache::set signature

* Updated MockCache::delete signature 

* Updated MockCache::clear signature and implementation

* Updated MockCache::getMultiple signature

* Updated MockCache::setMultiple signature

* Updated MockCache::deleteMultiple signature and implementation

* Updated MockCache::has signature

* MockCache::getMultiple and MockCache::setMultiple no longer need to throw InvalidArgumentException. If the first argument is not an iterable a TypeError will be thrown by PHP.

* MockCache::deleteMultiple no longer needs to throw InvalidArgumentException. If the first argument is not an iterable a TypeError will be thrown by PHP.

* Removed mindplay/simple-cache dev-dependency
  • Loading branch information
vortrixs authored Aug 24, 2022
1 parent b9ef3bc commit 02e86b8
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 24 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"prefer-stable": true,
"require": {
"php": ">= 8.0",
"psr/simple-cache": "^1"
"psr/simple-cache": "^2||^3"
},
"require-dev": {
"codeception/codeception": "^5",
Expand Down
32 changes: 12 additions & 20 deletions src/MockCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function skipTime($seconds)
$this->time += $seconds;
}

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

Expand All @@ -70,7 +70,7 @@ public function get($key, $default = null)
: $default;
}

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

Expand All @@ -90,7 +90,7 @@ public function set($key, $value, $ttl = null)
return true;
}

public function delete($key)
public function delete(string $key): bool
{
$this->validateKey($key);

Expand All @@ -106,18 +106,16 @@ public function delete($key)
return $success;
}

public function clear()
public function clear(): bool
{
$this->cache = [];
$this->cache_expiration = [];

return true;
}

public function getMultiple($keys, $default = null)
public function getMultiple(iterable $keys, mixed $default = null): iterable
{
if (! is_array($keys) && ! $keys instanceof Traversable) {
throw new InvalidArgumentException("keys must be either of type array or Traversable");
}

$values = [];

foreach ($keys as $key) {
Expand All @@ -128,12 +126,8 @@ public function getMultiple($keys, $default = null)
return $values;
}

public function setMultiple($values, $ttl = null)
public function setMultiple(iterable $values, DateInterval|int|null $ttl = null): bool
{
if (! is_array($values) && ! $values instanceof Traversable) {
throw new InvalidArgumentException("keys must be either of type array or Traversable");
}

foreach ($values as $key => $value) {
$this->validateKey($key);
$this->set($key, $value, $ttl);
Expand All @@ -142,19 +136,17 @@ public function setMultiple($values, $ttl = null)
return true;
}

public function deleteMultiple($keys)
public function deleteMultiple(iterable $keys): bool
{
if (! is_array($keys) && ! $keys instanceof Traversable) {
throw new InvalidArgumentException("keys must be either of type array or Traversable");
}

foreach ($keys as $key) {
$this->validateKey($key);
$this->delete($key);
}

return true;
}

public function has($key)
public function has(string $key): bool
{
return $this->get($key, $this) !== $this;
}
Expand Down
7 changes: 4 additions & 3 deletions tests/unit/MockCacheCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use DateInterval;
use Kodus\Cache\MockCache;
use Kodus\Cache\InvalidArgumentException;
use TypeError;
use UnitTester;

class MockCacheCest
Expand Down Expand Up @@ -130,11 +131,11 @@ public function testGetAndSetMultiple(UnitTester $I): void

$I->assertSame(["key1" => "value1", "key2" => "value2", "key3" => false], $results);

$I->expectThrowable(InvalidArgumentException::class, function () {
$I->expectThrowable(TypeError::class, function () {
$this->cache->getMultiple("Invalid type");
});

$I->expectThrowable(InvalidArgumentException::class, function () {
$I->expectThrowable(TypeError::class, function () {
$this->cache->setMultiple("Invalid type");
});

Expand All @@ -157,7 +158,7 @@ public function testDeleteMultiple(UnitTester $I): void

$I->assertSame("value3", $this->cache->get("key3"));

$I->expectThrowable(InvalidArgumentException::class, function () {
$I->expectThrowable(TypeError::class, function () {
$this->cache->deleteMultiple("Invalid type");
});

Expand Down

0 comments on commit 02e86b8

Please sign in to comment.