diff --git a/src/ClientContextInterface.php b/src/ClientContextInterface.php index b24cf198..5e9407d9 100644 --- a/src/ClientContextInterface.php +++ b/src/ClientContextInterface.php @@ -106,6 +106,7 @@ use Predis\Command\CommandInterface; * @method $this jsonobjlen(string $key, string $path = '$') * @method $this jsonset(string $key, string $path, string $value, ?string $subcommand = null) * @method $this jsonstrappend(string $key, string $path, string $value) + * @method $this jsonstrlen(string $key, string $path = '$') * @method $this blmove(string $source, string $destination, string $where, string $to, int $timeout) * @method $this blpop(array|string $keys, $timeout) * @method $this brpop(array|string $keys, $timeout) diff --git a/src/ClientInterface.php b/src/ClientInterface.php index 13c5d1ef..0fbb7725 100644 --- a/src/ClientInterface.php +++ b/src/ClientInterface.php @@ -115,6 +115,7 @@ use Predis\Response\Status; * @method array jsonobjlen(string $key, string $path = '$') * @method string jsonset(string $key, string $path, string $value, ?string $subcommand = null) * @method array jsonstrappend(string $key, string $path, string $value) + * @method array jsonstrlen(string $key, string $path = '$') * @method string blmove(string $source, string $destination, string $where, string $to, int $timeout) * @method array|null blpop(array|string $keys, int|float $timeout) * @method array|null brpop(array|string $keys, int|float $timeout) diff --git a/src/Command/Redis/Json/JSONSTRLEN.php b/src/Command/Redis/Json/JSONSTRLEN.php new file mode 100644 index 00000000..92ed80d3 --- /dev/null +++ b/src/Command/Redis/Json/JSONSTRLEN.php @@ -0,0 +1,28 @@ +getCommand(); + $command->setArguments($arguments); + + $this->assertSame($expected, $command->getArguments()); + } + + /** + * @group disconnected + */ + public function testParseResponse(): void + { + $this->assertSame(1, $this->getCommand()->parseResponse(1)); + } + + /** + * @group connected + * @dataProvider jsonProvider + * @param array $jsonArguments + * @param string $key + * @param string $path + * @param array $expectedStringLength + * @return void + * @requiresRedisJsonVersion >= 1.0.0 + */ + public function testReturnsLengthOfGivenJsonString( + array $jsonArguments, + string $key, + string $path, + array $expectedStringLength + ): void { + $redis = $this->getClient(); + + $redis->jsonset(...$jsonArguments); + + $this->assertSame($expectedStringLength, $redis->jsonstrlen($key, $path)); + } + + public function jsonProvider(): array + { + return [ + 'on root level' => [ + ['key', '$', '{"key1":"value1","key2":"value2"}'], + 'key', + '$.key2', + [6], + ], + 'on nested level' => [ + ['key', '$', '{"key1":{"key2":"value2"}}'], + 'key', + '$..key2', + [6], + ], + 'on both levels' => [ + ['key', '$', '{"key1":{"key2":"value2"},"key2":"value2"}'], + 'key', + '$..key2', + [6, 6], + ], + 'with non-json string' => [ + ['key', '$', '{"key1":{"key2":[1,2,3]}}'], + 'key', + '$..key2', + [null], + ], + ]; + } +}