Skip to content

Commit

Permalink
Update psr/simple-cache version to ^2.0|^3.0 (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik committed Jun 29, 2022
1 parent bf04d1a commit 6e32e32
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 98 deletions.
16 changes: 8 additions & 8 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
# Yii Test Support Change Log

## 2.0.1 under development
## 3.0.0 under development

- no changes in this release.
- Chg #45: Update `psr/simple-cache` version to `^2.0|^3.0` (@vjik)

## 2.0.0 June 16, 2022

- Chg #43: Raise the minimum `psr/log` version to `^2.0|^3.0` and the minimum PHP version to 8.0 (@rustamwin)

## 1.4.0 March 24, 2022

- Enh #40: Add custom callback for method `has()` of `SimpleContainer` (vjik)
- Bug #40: Catch only `NotFoundException` instead of `Throwable` in `SimpleContainer::has()` method (vjik)
- Enh #40: Add custom callback for method `has()` of `SimpleContainer` (@vjik)
- Bug #40: Catch only `NotFoundException` instead of `Throwable` in `SimpleContainer::has()` method (@vjik)

## 1.3.0 March 15, 2021

- Enh #29: Add `SimpleLogger` class, which is an implementation of `LoggerInterface` (devanych)
- Enh #29: Add `SimpleLogger` class, which is an implementation of `LoggerInterface` (@devanych)

## 1.2.1 March 07, 2021

- Enh #26: Support PSR Container v2.0 (roxblnfk)
- Enh #26: Support PSR Container v2.0 (@roxblnfk)

## 1.2.0 February 23, 2021

- Enh #24: Add `SimpleEventDispatcher::getEventClasses()` that return classes of dispatched events (vjik)
- Enh #24: Add `SimpleEventDispatcher::getEventClasses()` that return classes of dispatched events (@vjik)

## 1.1.0 February 22, 2021

- Enh #23: Add `SimpleEventDispatcher::clearEvents()` that clear all events in event dispatcher (vjik)
- Enh #23: Add `SimpleEventDispatcher::clearEvents()` that clear all events in event dispatcher (@vjik)

## 1.0.0 December 24, 2020

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"psr/container": "^1.0|^2.0",
"psr/event-dispatcher": "^1.0",
"psr/log": "^2.0|^3.0",
"psr/simple-cache": "^1.0"
"psr/simple-cache": "^2.0|^3.0"
},
"require-dev": {
"phpunit/phpunit": "^9.5",
Expand Down
25 changes: 8 additions & 17 deletions src/SimpleCache/MemorySimpleCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Psr\SimpleCache\CacheInterface;
use Traversable;
use Yiisoft\Test\Support\SimpleCache\Exception\InvalidArgumentException;

use function array_key_exists;
use function is_object;
use function is_string;
Expand All @@ -30,7 +31,7 @@ public function __construct(array $cacheData = [])
$this->setMultiple($cacheData);
}

public function get($key, $default = null)
public function get(string $key, mixed $default = null): mixed
{
$this->validateKey($key);
if (array_key_exists($key, $this->cache) && !$this->isExpired($key)) {
Expand All @@ -46,7 +47,7 @@ public function get($key, $default = null)
return $default;
}

public function set($key, $value, $ttl = null): bool
public function set(string $key, mixed $value, null|int|DateInterval $ttl = null): bool
{
$this->validateKey($key);
$expiration = $this->ttlToExpiration($ttl);
Expand All @@ -60,7 +61,7 @@ public function set($key, $value, $ttl = null): bool
return $this->returnOnSet;
}

public function delete($key): bool
public function delete(string $key): bool
{
$this->validateKey($key);
unset($this->cache[$key]);
Expand All @@ -73,13 +74,7 @@ public function clear(): bool
return $this->returnOnClear;
}

/**
* @param iterable $keys
* @param mixed $default
*
* @return mixed[]
*/
public function getMultiple($keys, $default = null): iterable
public function getMultiple(iterable $keys, mixed $default = null): iterable
{
$keys = $this->iterableToArray($keys);
$this->validateKeys($keys);
Expand All @@ -92,11 +87,7 @@ public function getMultiple($keys, $default = null): iterable
return $result;
}

/**
* @param iterable $values
* @param DateInterval|int|null $ttl
*/
public function setMultiple($values, $ttl = null): bool
public function setMultiple(iterable $values, null|int|DateInterval $ttl = null): bool
{
$values = $this->iterableToArray($values);
$this->validateKeysOfValues($values);
Expand All @@ -109,7 +100,7 @@ public function setMultiple($values, $ttl = null): bool
return $this->returnOnSet;
}

public function deleteMultiple($keys): bool
public function deleteMultiple(iterable $keys): bool
{
$keys = $this->iterableToArray($keys);
$this->validateKeys($keys);
Expand All @@ -120,7 +111,7 @@ public function deleteMultiple($keys): bool
return $this->returnOnDelete;
}

public function has($key): bool
public function has(string $key): bool
{
$this->validateKey($key);
/** @psalm-var string $key */
Expand Down
34 changes: 16 additions & 18 deletions src/SimpleCache/SimpleCacheActionLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace Yiisoft\Test\Support\SimpleCache;

use DateInterval;
use Psr\SimpleCache\CacheInterface;
use Traversable;
use Yiisoft\Test\Support\SimpleCache\Exception\InvalidArgumentException;

/**
* @template TCacheService as CacheInterface
Expand All @@ -19,9 +19,8 @@ final class SimpleCacheActionLogger implements CacheInterface
private CacheInterface $cacheService;

/**
* SimpleCacheActionLogger constructor.
* `SimpleCacheActionLogger` constructor.
*
* @param array $cacheData
* @param TCacheService $cacheService
*/
public function __construct(CacheInterface $cacheService, array $cacheData = [])
Expand All @@ -31,19 +30,19 @@ public function __construct(CacheInterface $cacheService, array $cacheData = [])
$this->actions = [];
}

public function get($key, $default = null)
public function get(string $key, mixed $default = null): mixed
{
$this->actions[] = Action::createGetAction($key);
return $this->cacheService->get($key, $default);
}

public function delete($key): bool
public function delete(string $key): bool
{
$this->actions[] = Action::createDeleteAction($key);
return $this->cacheService->delete($key);
}

public function has($key): bool
public function has(string $key): bool
{
$this->actions[] = Action::createHasAction($key);
return $this->cacheService->has($key);
Expand All @@ -55,23 +54,23 @@ public function clear(): bool
return $this->cacheService->clear();
}

public function set($key, $value, $ttl = null): bool
public function set(string $key, mixed $value, null|int|DateInterval $ttl = null): bool
{
$this->actions[] = Action::createSetAction($key, $value, $ttl);
return $this->cacheService->set($key, $value, $ttl);
}

public function getMultiple($keys, $default = null): iterable
public function getMultiple(iterable $keys, mixed $default = null): iterable
{
$keys = $this->iterableToArray($keys);
/** @psalm-var mixed $key */
/** @var mixed $key */
foreach ($keys as $key) {
$this->actions[] = Action::createGetAction($key);
}
return $this->cacheService->getMultiple($keys, $default);
}

public function setMultiple($values, $ttl = null): bool
public function setMultiple(iterable $values, null|int|DateInterval $ttl = null): bool
{
$values = $this->iterableToArray($values);
/** @psalm-var mixed $value */
Expand All @@ -81,10 +80,10 @@ public function setMultiple($values, $ttl = null): bool
return $this->cacheService->setMultiple($values, $ttl);
}

public function deleteMultiple($keys): bool
public function deleteMultiple(iterable $keys): bool
{
$keys = $this->iterableToArray($keys);
/** @psalm-var mixed $key */
/** @var mixed $key */
foreach ($keys as $key) {
$this->actions[] = Action::createDeleteAction($key);
}
Expand Down Expand Up @@ -120,15 +119,14 @@ public function getCacheService(): CacheInterface
}

/**
* @param mixed $iterable
* Converts iterable to array.
*
* Converts iterable to array. If provided value is not iterable it throws an InvalidArgumentException
* @psalm-template T
* @psalm-param iterable<T> $iterable
* @psalm-return array<array-key,T>
*/
private function iterableToArray($iterable): array
private function iterableToArray(iterable $iterable): array
{
if (!is_iterable($iterable)) {
throw new InvalidArgumentException(sprintf('Iterable is expected, got %s.', gettype($iterable)));
}
return $iterable instanceof Traversable ? iterator_to_array($iterable) : $iterable;
}
}
63 changes: 9 additions & 54 deletions tests/SimpleCache/BaseSimpleCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@

namespace Yiisoft\Test\Support\Tests\SimpleCache;

use ArrayIterator;
use DateInterval;
use IteratorAggregate;
use PHPUnit\Framework\TestCase;
use Psr\SimpleCache\CacheInterface;
use Psr\SimpleCache\InvalidArgumentException;
use stdClass;

use function is_object;

abstract class BaseSimpleCacheTest extends TestCase
Expand Down Expand Up @@ -224,14 +228,14 @@ public function iterableProvider(): array
],
'ArrayIterator' => [
['a' => 1, 'b' => 2,],
new \ArrayIterator(['a' => 1, 'b' => 2,]),
new ArrayIterator(['a' => 1, 'b' => 2,]),
],
'IteratorAggregate' => [
['a' => 1, 'b' => 2,],
new class () implements \IteratorAggregate {
public function getIterator()
new class () implements IteratorAggregate {
public function getIterator(): ArrayIterator
{
return new \ArrayIterator(['a' => 1, 'b' => 2,]);
return new ArrayIterator(['a' => 1, 'b' => 2,]);
}
},
],
Expand All @@ -257,58 +261,9 @@ public function testSetWithDateIntervalTtl()
$this->assertSame(['b' => 2], $cache->getMultiple(['b']));
}

public function testGetInvalidKey(): void
{
$this->expectException(InvalidArgumentException::class);
$cache = $this->createCacheInstance();
$cache->get(1);
}

public function testSetInvalidKey(): void
{
$this->expectException(InvalidArgumentException::class);
$cache = $this->createCacheInstance();
$cache->set(1, 1);
}

public function testDeleteInvalidKey(): void
{
$this->expectException(InvalidArgumentException::class);
$cache = $this->createCacheInstance();
$cache->delete(1);
}

public function testGetMultipleInvalidKeysNotIterable(): void
{
$this->expectException(InvalidArgumentException::class);
$cache = $this->createCacheInstance();
$cache->getMultiple(1);
}

public function testSetMultipleInvalidKeysNotIterable(): void
{
$this->expectException(InvalidArgumentException::class);
$cache = $this->createCacheInstance();
$cache->setMultiple(1);
}

public function testDeleteMultipleInvalidKeysNotIterable(): void
{
$this->expectException(InvalidArgumentException::class);
$cache = $this->createCacheInstance();
$cache->deleteMultiple(1);
}

public function testHasInvalidKey(): void
{
$this->expectException(InvalidArgumentException::class);
$cache = $this->createCacheInstance();
$cache->has(1);
}

public function dataProvider(): array
{
$object = new \stdClass();
$object = new stdClass();
$object->test_field = 'test_value';
return [
'integer' => ['test_integer', 1],
Expand Down

0 comments on commit 6e32e32

Please sign in to comment.