diff --git a/.codespellrc b/.codespellrc index 70f77356..c9bef245 100644 --- a/.codespellrc +++ b/.codespellrc @@ -3,4 +3,4 @@ skip=./.git check-hidden= check-filenames= builtin=clear,rare,informal,usage,code,names -ignore-words-list=master,masters,slave,slaves,whitelist,cas,exat,smove,SUGGET,sugget,ro +ignore-words-list=master,masters,slave,slaves,whitelist,cas,exat,smove,SUGGET,sugget,ro,DOF,dof diff --git a/src/ClientContextInterface.php b/src/ClientContextInterface.php index d6cb7451..e6099321 100644 --- a/src/ClientContextInterface.php +++ b/src/ClientContextInterface.php @@ -154,6 +154,13 @@ use Predis\Command\Redis\Container\Search\FTCURSOR; * @method $this strlen($key) * @method $this hdel($key, array $fields) * @method $this hexists($key, $field) + * @method $this hexpire(string $key, int $seconds, array $fields, string $flag = null) + * @method $this hexpireat(string $key, int $unixTimeSeconds, array $fields, string $flag = null) + * @method $this hexpiretime(string $key, array $fields) + * @method $this hpersist(string $key, array $fields) + * @method $this hpexpire(string $key, int $milliseconds, array $fields, string $flag = null) + * @method $this hpexpireat(string $key, int $unixTimeMilliseconds, array $fields, string $flag = null) + * @method $this hpexpiretime(string $key, array $fields) * @method $this hget($key, $field) * @method $this hgetall($key) * @method $this hincrby($key, $field, $increment) @@ -166,6 +173,8 @@ use Predis\Command\Redis\Container\Search\FTCURSOR; * @method $this hscan($key, $cursor, ?array $options = null) * @method $this hset($key, $field, $value) * @method $this hsetnx($key, $field, $value) + * @method $this httl(string $key, array $fields) + * @method $this hpttl(string $key, array $fields) * @method $this hvals($key) * @method $this hstrlen($key, $field) * @method $this jsonarrappend(string $key, string $path = '$', ...$value) diff --git a/src/ClientInterface.php b/src/ClientInterface.php index 91408c5b..2a45dea3 100644 --- a/src/ClientInterface.php +++ b/src/ClientInterface.php @@ -163,6 +163,13 @@ use Predis\Response\Status; * @method int strlen(string $key) * @method int hdel(string $key, array $fields) * @method int hexists(string $key, string $field) + * @method array|null hexpire(string $key, int $seconds, array $fields, string $flag = null) + * @method array|null hexpireat(string $key, int $unixTimeSeconds, array $fields, string $flag = null) + * @method array|null hexpiretime(string $key, array $fields) + * @method array|null hpersist(string $key, array $fields) + * @method array|null hpexpire(string $key, int $milliseconds, array $fields, string $flag = null) + * @method array|null hpexpireat(string $key, int $unixTimeMilliseconds, array $fields, string $flag = null) + * @method array|null hpexpiretime(string $key, array $fields) * @method string|null hget(string $key, string $field) * @method array hgetall(string $key) * @method int hincrby(string $key, string $field, int $increment) @@ -175,6 +182,8 @@ use Predis\Response\Status; * @method array hscan(string $key, $cursor, ?array $options = null) * @method int hset(string $key, string $field, string $value) * @method int hsetnx(string $key, string $field, string $value) + * @method array|null httl(string $key, array $fields) + * @method array|null hpttl(string $key, array $fields) * @method array hvals(string $key) * @method int hstrlen(string $key, string $field) * @method array jsonarrappend(string $key, string $path = '$', ...$value) diff --git a/src/Command/Redis/HEXPIRE.php b/src/Command/Redis/HEXPIRE.php new file mode 100644 index 00000000..ea09bff3 --- /dev/null +++ b/src/Command/Redis/HEXPIRE.php @@ -0,0 +1,51 @@ +flagsEnum, true)) { + $processedArguments[] = strtoupper($arguments[3]); + } else { + throw new UnexpectedValueException('Unsupported flag value'); + } + } + + if (array_key_exists(2, $arguments) && null !== $arguments[2]) { + array_push($processedArguments, 'FIELDS', count($arguments[2])); + $processedArguments = array_merge($processedArguments, $arguments[2]); + } + + parent::setArguments($processedArguments); + } +} diff --git a/src/Command/Redis/HEXPIREAT.php b/src/Command/Redis/HEXPIREAT.php new file mode 100644 index 00000000..fb1e3d7b --- /dev/null +++ b/src/Command/Redis/HEXPIREAT.php @@ -0,0 +1,21 @@ +getCommand(); + $command->setArguments($actualArguments); + + $this->assertSame($expectedArguments, $command->getArguments()); + } + + /** + * @group disconnected + */ + public function testFilterArgumentsThrowsExceptionOnIncorrectFlagValue(): void + { + $command = $this->getCommand(); + + $this->expectException(UnexpectedValueException::class); + $this->expectExceptionMessage('Unsupported flag value'); + + $command->setArguments(['key', 1000, null, 'wrong']); + } + + /** + * @group disconnected + */ + public function testParseResponse(): void + { + $command = $this->getCommand(); + + $this->assertSame(0, $command->parseResponse(0)); + $this->assertSame(1, $command->parseResponse(1)); + } + + /** + * @dataProvider hashProvider + * @group connected + * @group slow + * @requiresRedisVersion >= 7.3.0 + */ + public function testHashExpiresCorrectlyWithNoFlags( + array $hashArgs, + array $expireArgs, + array $expectedResponse, + array $expectedHash + ): void { + $redis = $this->getClient(); + + $redis->hset(...$hashArgs); + + // Time should be calculated within a test, in data provider it will be pre-calculated before test execution. + $expireArgs[1] = time() + $expireArgs[1]; + $this->assertSame($expectedResponse, $redis->hexpireat(...$expireArgs)); + $this->sleep(2); + $this->assertSameValues($expectedHash, $redis->hgetall('hashkey')); + } + + /** + * @group connected + * @requiresRedisVersion >= 7.3.0 + */ + public function testHashExpiresCorrectlyWithFlags(): void + { + $redis = $this->getClient(); + + $redis->hset('hashkey', 'field1', 'value1', 'field2', 'value2'); + + $this->assertSame([1, 1], $redis->hexpireat('hashkey', time() + 2, ['field1', 'field2'])); + $this->assertSame([0, 0], $redis->hexpireat('hashkey', time() + 2, ['field1', 'field2'], 'NX')); + $this->assertSame([1, 1], $redis->hexpireat('hashkey', time() + 2, ['field1', 'field2'], 'XX')); + $this->assertSame([1, 1], $redis->hexpireat('hashkey', time() + 3, ['field1', 'field2'], 'GT')); + $this->assertSame([0, 0], $redis->hexpireat('hashkey', time() + 1, ['field1', 'field2'], 'GT')); + $this->assertSame([1, 1], $redis->hexpireat('hashkey', time() + 2, ['field1', 'field2'], 'LT')); + $this->assertSame([0, 0], $redis->hexpireat('hashkey', time() + 3, ['field1', 'field2'], 'LT')); + $this->assertSame([-2, -2], $redis->hexpireat('wrongkey', time() + 2, ['field1', 'field2'])); + } + + public function hashProvider(): array + { + return [ + 'with all fields expired' => [ + ['hashkey', 'field1', 'value1', 'field2', 'value2'], + ['hashkey', 1, ['field1', 'field2']], + [1, 1], + [], + ], + 'with partial fields expired' => [ + ['hashkey', 'field1', 'value1', 'field2', 'value2'], + ['hashkey', 1, ['field1']], + [1], + ['field2' => 'value2'], + ], + 'with incorrect fields' => [ + ['hashkey', 'field1', 'value1', 'field2', 'value2'], + ['hashkey', 1, ['field3', 'field4']], + [-2, -2], + ['field2' => 'value2', 'field1' => 'value1'], + ], + ]; + } + + public function argumentsProvider(): array + { + return [ + 'with specified fields' => [ + ['key', 100, ['field1', 'field2']], + ['key', 100, 'FIELDS', 2, 'field1', 'field2'], + ], + 'with specified flag' => [ + ['key', 100, null, 'NX'], + ['key', 100, 'NX'], + ], + 'with all arguments' => [ + ['key', 100, ['field1', 'field2'], 'XX'], + ['key', 100, 'XX', 'FIELDS', 2, 'field1', 'field2'], + ], + ]; + } +} diff --git a/tests/Predis/Command/Redis/HEXPIRETIME_Test.php b/tests/Predis/Command/Redis/HEXPIRETIME_Test.php new file mode 100644 index 00000000..60d16c10 --- /dev/null +++ b/tests/Predis/Command/Redis/HEXPIRETIME_Test.php @@ -0,0 +1,71 @@ +getCommand(); + $command->setArguments(['key', ['field1', 'field2']]); + + $this->assertSame(['key', 'FIELDS', 2, 'field1', 'field2'], $command->getArguments()); + } + + /** + * @group disconnected + */ + public function testParseResponse(): void + { + $command = $this->getCommand(); + + $this->assertSame(0, $command->parseResponse(0)); + $this->assertSame(1, $command->parseResponse(1)); + } + + /** + * @group connected + * @return void + * @requiresRedisVersion >= 7.3.0 + */ + public function testReturnsExpirationTimestamp(): void + { + $redis = $this->getClient(); + + $redis->hset('hashkey', 'field1', 'value1', 'field2', 'value2'); + + $expireAt = time() + 10; + $this->assertSame([1, 1], $redis->hexpireat('hashkey', $expireAt, ['field1', 'field2'])); + $this->assertSame([$expireAt, $expireAt], $redis->hexpiretime('hashkey', ['field1', 'field2'])); + $this->assertSame([-2], $redis->hexpiretime('wrongkey', ['field1'])); + } +} diff --git a/tests/Predis/Command/Redis/HEXPIRE_Test.php b/tests/Predis/Command/Redis/HEXPIRE_Test.php new file mode 100644 index 00000000..e1c2e03e --- /dev/null +++ b/tests/Predis/Command/Redis/HEXPIRE_Test.php @@ -0,0 +1,153 @@ +getCommand(); + $command->setArguments($actualArguments); + + $this->assertSame($expectedArguments, $command->getArguments()); + } + + /** + * @group disconnected + */ + public function testFilterArgumentsThrowsExceptionOnIncorrectFlagValue(): void + { + $command = $this->getCommand(); + + $this->expectException(UnexpectedValueException::class); + $this->expectExceptionMessage('Unsupported flag value'); + + $command->setArguments(['key', 1000, null, 'wrong']); + } + + /** + * @group disconnected + */ + public function testParseResponse(): void + { + $command = $this->getCommand(); + + $this->assertSame(0, $command->parseResponse(0)); + $this->assertSame(1, $command->parseResponse(1)); + } + + /** + * @dataProvider hashProvider + * @group connected + * @group slow + * @requiresRedisVersion >= 7.3.0 + */ + public function testHashExpiresCorrectlyWithNoFlags( + array $hashArgs, + array $expireArgs, + array $expectedResponse, + array $expectedHash + ): void { + $redis = $this->getClient(); + + $redis->hset(...$hashArgs); + + $this->assertSame($expectedResponse, $redis->hexpire(...$expireArgs)); + $this->sleep(2); + $this->assertSameValues($expectedHash, $redis->hgetall('hashkey')); + } + + /** + * @group connected + * @requiresRedisVersion >= 7.3.0 + */ + public function testHashExpiresCorrectlyWithFlags(): void + { + $redis = $this->getClient(); + + $redis->hset('hashkey', 'field1', 'value1', 'field2', 'value2'); + + $this->assertSame([1, 1], $redis->hexpire('hashkey', 2, ['field1', 'field2'])); + $this->assertSame([0, 0], $redis->hexpire('hashkey', 2, ['field1', 'field2'], 'NX')); + $this->assertSame([1, 1], $redis->hexpire('hashkey', 2, ['field1', 'field2'], 'XX')); + $this->assertSame([1, 1], $redis->hexpire('hashkey', 3, ['field1', 'field2'], 'GT')); + $this->assertSame([0, 0], $redis->hexpire('hashkey', 1, ['field1', 'field2'], 'GT')); + $this->assertSame([1, 1], $redis->hexpire('hashkey', 2, ['field1', 'field2'], 'LT')); + $this->assertSame([0, 0], $redis->hexpire('hashkey', 3, ['field1', 'field2'], 'LT')); + $this->assertSame([-2, -2], $redis->hexpire('wrongkey', 2, ['field1', 'field2'])); + } + + public function hashProvider(): array + { + return [ + 'with all fields expired' => [ + ['hashkey', 'field1', 'value1', 'field2', 'value2'], + ['hashkey', 1, ['field1', 'field2']], + [1, 1], + [], + ], + 'with partial fields expired' => [ + ['hashkey', 'field1', 'value1', 'field2', 'value2'], + ['hashkey', 1, ['field1']], + [1], + ['field2' => 'value2'], + ], + 'with incorrect fields' => [ + ['hashkey', 'field1', 'value1', 'field2', 'value2'], + ['hashkey', 1, ['field3', 'field4']], + [-2, -2], + ['field2' => 'value2', 'field1' => 'value1'], + ], + ]; + } + + public function argumentsProvider(): array + { + return [ + 'with specified fields' => [ + ['key', 100, ['field1', 'field2']], + ['key', 100, 'FIELDS', 2, 'field1', 'field2'], + ], + 'with specified flag' => [ + ['key', 100, null, 'NX'], + ['key', 100, 'NX'], + ], + 'with all arguments' => [ + ['key', 100, ['field1', 'field2'], 'XX'], + ['key', 100, 'XX', 'FIELDS', 2, 'field1', 'field2'], + ], + ]; + } +} diff --git a/tests/Predis/Command/Redis/HPERSIST_Test.php b/tests/Predis/Command/Redis/HPERSIST_Test.php new file mode 100644 index 00000000..4f17a3b4 --- /dev/null +++ b/tests/Predis/Command/Redis/HPERSIST_Test.php @@ -0,0 +1,73 @@ +getCommand(); + $command->setArguments(['key', ['field1', 'field2']]); + + $this->assertSame(['key', 'FIELDS', 2, 'field1', 'field2'], $command->getArguments()); + } + + /** + * @group disconnected + */ + public function testParseResponse(): void + { + $command = $this->getCommand(); + + $this->assertSame(0, $command->parseResponse(0)); + $this->assertSame(1, $command->parseResponse(1)); + } + + /** + * @group connected + * @group medium + * @return void + * @requiresRedisVersion >= 7.3.0 + */ + public function testRemovesExpirationFromGivenField(): void + { + $redis = $this->getClient(); + + $redis->hset('hashkey', 'field1', 'value1', 'field2', 'value2'); + + $this->assertSame([1, 1], $redis->hpexpire('hashkey', 100, ['field1', 'field2'])); + $this->assertSame([1], $redis->hpersist('hashkey', ['field1'])); + $this->sleep(0.2); + $this->assertSame(['field1' => 'value1'], $redis->hgetall('hashkey')); + $this->assertSame([-2], $redis->hpersist('wrongkey', ['field1'])); + } +} diff --git a/tests/Predis/Command/Redis/HPEXPIREAT_Test.php b/tests/Predis/Command/Redis/HPEXPIREAT_Test.php new file mode 100644 index 00000000..e3576ca8 --- /dev/null +++ b/tests/Predis/Command/Redis/HPEXPIREAT_Test.php @@ -0,0 +1,155 @@ +getCommand(); + $command->setArguments($actualArguments); + + $this->assertSame($expectedArguments, $command->getArguments()); + } + + /** + * @group disconnected + */ + public function testFilterArgumentsThrowsExceptionOnIncorrectFlagValue(): void + { + $command = $this->getCommand(); + + $this->expectException(UnexpectedValueException::class); + $this->expectExceptionMessage('Unsupported flag value'); + + $command->setArguments(['key', 1000, null, 'wrong']); + } + + /** + * @group disconnected + */ + public function testParseResponse(): void + { + $command = $this->getCommand(); + + $this->assertSame(0, $command->parseResponse(0)); + $this->assertSame(1, $command->parseResponse(1)); + } + + /** + * @dataProvider hashProvider + * @group connected + * @group slow + * @requiresRedisVersion >= 7.3.0 + */ + public function testHashExpiresCorrectlyWithNoFlags( + array $hashArgs, + array $expireArgs, + array $expectedResponse, + array $expectedHash + ): void { + $redis = $this->getClient(); + + $redis->hset(...$hashArgs); + + // Time should be calculated within a test, in data provider it will be pre-calculated before test execution. + $expireArgs[1] = (time() + $expireArgs[1]) * 1000; + $this->assertSame($expectedResponse, $redis->hpexpireat(...$expireArgs)); + $this->sleep(2); + $this->assertSameValues($expectedHash, $redis->hgetall('hashkey')); + } + + /** + * @group connected + * @requiresRedisVersion >= 7.3.0 + */ + public function testHashExpiresCorrectlyWithFlags(): void + { + $redis = $this->getClient(); + + $redis->hset('hashkey', 'field1', 'value1', 'field2', 'value2'); + + $this->assertSame([1, 1], $redis->hpexpireat('hashkey', (time() + 2) * 1000, ['field1', 'field2'])); + $this->assertSame([0, 0], $redis->hpexpireat('hashkey', (time() + 2) * 1000, ['field1', 'field2'], 'NX')); + $this->assertSame([1, 1], $redis->hpexpireat('hashkey', (time() + 2) * 1000, ['field1', 'field2'], 'XX')); + $this->assertSame([1, 1], $redis->hpexpireat('hashkey', (time() + 3) * 1000, ['field1', 'field2'], 'GT')); + $this->assertSame([0, 0], $redis->hpexpireat('hashkey', (time() + 1) * 1000, ['field1', 'field2'], 'GT')); + $this->assertSame([1, 1], $redis->hpexpireat('hashkey', (time() + 2) * 1000, ['field1', 'field2'], 'LT')); + $this->assertSame([0, 0], $redis->hpexpireat('hashkey', (time() + 3) * 1000, ['field1', 'field2'], 'LT')); + $this->assertSame([-2, -2], $redis->hpexpireat('wrongkey', (time() + 2) * 1000, ['field1', 'field2'])); + } + + public function hashProvider(): array + { + return [ + 'with all fields expired' => [ + ['hashkey', 'field1', 'value1', 'field2', 'value2'], + ['hashkey', 1, ['field1', 'field2']], + [1, 1], + [], + ], + 'with partial fields expired' => [ + ['hashkey', 'field1', 'value1', 'field2', 'value2'], + ['hashkey', 1, ['field1']], + [1], + ['field2' => 'value2'], + ], + 'with incorrect fields' => [ + ['hashkey', 'field1', 'value1', 'field2', 'value2'], + ['hashkey', 1, ['field3', 'field4']], + [-2, -2], + ['field2' => 'value2', 'field1' => 'value1'], + ], + ]; + } + + public function argumentsProvider(): array + { + return [ + 'with specified fields' => [ + ['key', 100, ['field1', 'field2']], + ['key', 100, 'FIELDS', 2, 'field1', 'field2'], + ], + 'with specified flag' => [ + ['key', 100, null, 'NX'], + ['key', 100, 'NX'], + ], + 'with all arguments' => [ + ['key', 100, ['field1', 'field2'], 'XX'], + ['key', 100, 'XX', 'FIELDS', 2, 'field1', 'field2'], + ], + ]; + } +} diff --git a/tests/Predis/Command/Redis/HPEXPIRETIME_Test.php b/tests/Predis/Command/Redis/HPEXPIRETIME_Test.php new file mode 100644 index 00000000..12085d6c --- /dev/null +++ b/tests/Predis/Command/Redis/HPEXPIRETIME_Test.php @@ -0,0 +1,71 @@ +getCommand(); + $command->setArguments(['key', ['field1', 'field2']]); + + $this->assertSame(['key', 'FIELDS', 2, 'field1', 'field2'], $command->getArguments()); + } + + /** + * @group disconnected + */ + public function testParseResponse(): void + { + $command = $this->getCommand(); + + $this->assertSame(0, $command->parseResponse(0)); + $this->assertSame(1, $command->parseResponse(1)); + } + + /** + * @group connected + * @return void + * @requiresRedisVersion >= 7.3.0 + */ + public function testReturnsExpirationTimestamp(): void + { + $redis = $this->getClient(); + + $redis->hset('hashkey', 'field1', 'value1', 'field2', 'value2'); + + $expireAt = (time() + 10) * 1000; + $this->assertSame([1, 1], $redis->hpexpireat('hashkey', $expireAt, ['field1', 'field2'])); + $this->assertSame([$expireAt, $expireAt], $redis->hpexpiretime('hashkey', ['field1', 'field2'])); + $this->assertSame([-2], $redis->hexpiretime('wrongkey', ['field1'])); + } +} diff --git a/tests/Predis/Command/Redis/HPEXPIRE_Test.php b/tests/Predis/Command/Redis/HPEXPIRE_Test.php new file mode 100644 index 00000000..f405eda1 --- /dev/null +++ b/tests/Predis/Command/Redis/HPEXPIRE_Test.php @@ -0,0 +1,154 @@ +getCommand(); + $command->setArguments($actualArguments); + + $this->assertSame($expectedArguments, $command->getArguments()); + } + + /** + * @group disconnected + */ + public function testFilterArgumentsThrowsExceptionOnIncorrectFlagValue(): void + { + $command = $this->getCommand(); + + $this->expectException(UnexpectedValueException::class); + $this->expectExceptionMessage('Unsupported flag value'); + + $command->setArguments(['key', 1000, null, 'wrong']); + } + + /** + * @group disconnected + */ + public function testParseResponse(): void + { + $command = $this->getCommand(); + + $this->assertSame(0, $command->parseResponse(0)); + $this->assertSame(1, $command->parseResponse(1)); + } + + /** + * @dataProvider hashProvider + * @group connected + * @group medium + * @requiresRedisVersion >= 7.3.0 + */ + public function testHashExpiresCorrectlyWithNoFlags( + array $hashArgs, + array $expireArgs, + array $expectedResponse, + array $expectedHash + ): void { + $redis = $this->getClient(); + + $redis->hset(...$hashArgs); + + $this->assertSame($expectedResponse, $redis->hpexpire(...$expireArgs)); + $this->sleep(0.2); + $this->assertSameValues($expectedHash, $redis->hgetall('hashkey')); + } + + /** + * @group connected + * @group medium + * @requiresRedisVersion >= 7.3.0 + */ + public function testHashExpiresCorrectlyWithFlags(): void + { + $redis = $this->getClient(); + + $redis->hset('hashkey', 'field1', 'value1', 'field2', 'value2'); + + $this->assertSame([1, 1], $redis->hpexpire('hashkey', 200, ['field1', 'field2'])); + $this->assertSame([0, 0], $redis->hpexpire('hashkey', 200, ['field1', 'field2'], 'NX')); + $this->assertSame([1, 1], $redis->hpexpire('hashkey', 200, ['field1', 'field2'], 'XX')); + $this->assertSame([1, 1], $redis->hpexpire('hashkey', 300, ['field1', 'field2'], 'GT')); + $this->assertSame([0, 0], $redis->hpexpire('hashkey', 100, ['field1', 'field2'], 'GT')); + $this->assertSame([1, 1], $redis->hpexpire('hashkey', 200, ['field1', 'field2'], 'LT')); + $this->assertSame([0, 0], $redis->hpexpire('hashkey', 300, ['field1', 'field2'], 'LT')); + $this->assertSame([-2, -2], $redis->hpexpire('wrongkey', 200, ['field1', 'field2'])); + } + + public function argumentsProvider(): array + { + return [ + 'with specified fields' => [ + ['key', 100, ['field1', 'field2']], + ['key', 100, 'FIELDS', 2, 'field1', 'field2'], + ], + 'with specified flag' => [ + ['key', 100, null, 'NX'], + ['key', 100, 'NX'], + ], + 'with all arguments' => [ + ['key', 100, ['field1', 'field2'], 'XX'], + ['key', 100, 'XX', 'FIELDS', 2, 'field1', 'field2'], + ], + ]; + } + + public function hashProvider(): array + { + return [ + 'with all fields expired' => [ + ['hashkey', 'field1', 'value1', 'field2', 'value2'], + ['hashkey', 100, ['field1', 'field2']], + [1, 1], + [], + ], + 'with partial fields expired' => [ + ['hashkey', 'field1', 'value1', 'field2', 'value2'], + ['hashkey', 100, ['field1']], + [1], + ['field2' => 'value2'], + ], + 'with incorrect fields' => [ + ['hashkey', 'field1', 'value1', 'field2', 'value2'], + ['hashkey', 100, ['field3', 'field4']], + [-2, -2], + ['field2' => 'value2', 'field1' => 'value1'], + ], + ]; + } +} diff --git a/tests/Predis/Command/Redis/HPTTL_Test.php b/tests/Predis/Command/Redis/HPTTL_Test.php new file mode 100644 index 00000000..26bd89be --- /dev/null +++ b/tests/Predis/Command/Redis/HPTTL_Test.php @@ -0,0 +1,70 @@ +getCommand(); + $command->setArguments(['key', ['field1', 'field2']]); + + $this->assertSame(['key', 'FIELDS', 2, 'field1', 'field2'], $command->getArguments()); + } + + /** + * @group disconnected + */ + public function testParseResponse(): void + { + $command = $this->getCommand(); + + $this->assertSame(0, $command->parseResponse(0)); + $this->assertSame(1, $command->parseResponse(1)); + } + + /** + * @group connected + * @return void + * @requiresRedisVersion >= 7.3.0 + */ + public function testReturnsRemainingTTL(): void + { + $redis = $this->getClient(); + + $redis->hset('hashkey', 'field1', 'value1', 'field2', 'value2'); + + $this->assertSame([1, 1], $redis->hexpire('hashkey', 10, ['field1', 'field2'])); + $this->assertEqualsWithDelta([10000, 10000], $redis->hpttl('hashkey', ['field1', 'field2']), 10); + $this->assertSame([-2], $redis->hpttl('wrongkey', ['field1'])); + } +} diff --git a/tests/Predis/Command/Redis/HTTL_Test.php b/tests/Predis/Command/Redis/HTTL_Test.php new file mode 100644 index 00000000..a4ea66a0 --- /dev/null +++ b/tests/Predis/Command/Redis/HTTL_Test.php @@ -0,0 +1,70 @@ +getCommand(); + $command->setArguments(['key', ['field1', 'field2']]); + + $this->assertSame(['key', 'FIELDS', 2, 'field1', 'field2'], $command->getArguments()); + } + + /** + * @group disconnected + */ + public function testParseResponse(): void + { + $command = $this->getCommand(); + + $this->assertSame(0, $command->parseResponse(0)); + $this->assertSame(1, $command->parseResponse(1)); + } + + /** + * @group connected + * @return void + * @requiresRedisVersion >= 7.3.0 + */ + public function testReturnsRemainingTTL(): void + { + $redis = $this->getClient(); + + $redis->hset('hashkey', 'field1', 'value1', 'field2', 'value2'); + + $this->assertSame([1, 1], $redis->hexpire('hashkey', 10, ['field1', 'field2'])); + $this->assertSame([10, 10], $redis->httl('hashkey', ['field1', 'field2'])); + $this->assertSame([-2], $redis->httl('wrongkey', ['field1'])); + } +}