From 89f20dc9688cc84c6aeffb82d8fc02ce4f5e6ea0 Mon Sep 17 00:00:00 2001 From: Hubert Lenoir Date: Mon, 23 May 2022 17:46:48 +0200 Subject: [PATCH] add TOUCH command (#767) --- src/ClientInterface.php | 1 + src/Command/Redis/TOUCH.php | 40 +++++++++ tests/Predis/Command/Redis/TOUCH_Test.php | 98 +++++++++++++++++++++++ 3 files changed, 139 insertions(+) create mode 100644 src/Command/Redis/TOUCH.php create mode 100644 tests/Predis/Command/Redis/TOUCH_Test.php diff --git a/src/ClientInterface.php b/src/ClientInterface.php index 9f1fff18..ae4af816 100644 --- a/src/ClientInterface.php +++ b/src/ClientInterface.php @@ -115,6 +115,7 @@ use Predis\Response\Status; * @method array sscan(string $key, int $cursor, array $options = null) * @method string[] sunion(array|string $keys) * @method int sunionstore(string $destination, array|string $keys) + * @method int touch(string[]|string $keyOrKeys, string ...$keys = null) * @method int zadd(string $key, array $membersAndScoresDictionary) * @method int zcard(string $key) * @method string zcount(string $key, int|string $min, int|string $max) diff --git a/src/Command/Redis/TOUCH.php b/src/Command/Redis/TOUCH.php new file mode 100644 index 00000000..01593b1c --- /dev/null +++ b/src/Command/Redis/TOUCH.php @@ -0,0 +1,40 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Predis\Command\Redis; + +use Predis\Command\Command as RedisCommand; + +/** + * @link http://redis.io/commands/touch + * + * @author Daniele Alessandri + */ +class TOUCH extends RedisCommand +{ + /** + * {@inheritdoc} + */ + public function getId() + { + return 'TOUCH'; + } + + /** + * {@inheritdoc} + */ + public function setArguments(array $arguments) + { + $arguments = self::normalizeArguments($arguments); + + parent::setArguments($arguments); + } +} diff --git a/tests/Predis/Command/Redis/TOUCH_Test.php b/tests/Predis/Command/Redis/TOUCH_Test.php new file mode 100644 index 00000000..98b8b662 --- /dev/null +++ b/tests/Predis/Command/Redis/TOUCH_Test.php @@ -0,0 +1,98 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Predis\Command\Redis; + +/** + * @group commands + * @group realm-server + */ +class TOUCH_Test extends PredisCommandTestCase +{ + /** + * {@inheritdoc} + */ + protected function getExpectedCommand(): string + { + return 'Predis\Command\Redis\TOUCH'; + } + + /** + * {@inheritdoc} + */ + protected function getExpectedId(): string + { + return 'TOUCH'; + } + + /** + * @requiresRedisVersion >= 3.2.1 + * + * @group disconnected + */ + public function testFilterArguments(): void + { + $arguments = ['key1', 'key2', 'key3']; + $expected = ['key1', 'key2', 'key3']; + + $command = $this->getCommand(); + $command->setArguments($arguments); + $this->assertSame($expected, $command->getArguments()); + } + + /** + * @requiresRedisVersion >= 3.2.1 + * + * @group disconnected + */ + public function testFilterArgumentsAsSingleArray(): void + { + $arguments = [['key1', 'key2', 'key3']]; + $expected = ['key1', 'key2', 'key3']; + + $command = $this->getCommand(); + $command->setArguments($arguments); + + $this->assertSame($expected, $command->getArguments()); + } + + /** + * @requiresRedisVersion >= 3.2.1 + * + * @group disconnected + */ + public function testParseResponse(): void + { + $command = $this->getCommand(); + + $this->assertSame(10, $command->parseResponse(10)); + } + + /** + * @requiresRedisVersion >= 3.2.1 + * + * @group connected + */ + public function testReturnsNumberOfDeletedKeys(): void + { + $redis = $this->getClient(); + + $this->assertSame(0, $redis->touch('foo')); + + $redis->set('foo', 'bar'); + $this->assertSame(1, $redis->touch('foo')); + $this->assertSame(1, $redis->touch('foo', 'hoge')); + + $redis->set('hoge', 'piyo'); + $this->assertSame(1, $redis->touch('foo')); + $this->assertSame(2, $redis->touch('foo', 'hoge')); + } +}