diff --git a/src/ClientContextInterface.php b/src/ClientContextInterface.php index eb865be5..a2b54ad5 100644 --- a/src/ClientContextInterface.php +++ b/src/ClientContextInterface.php @@ -104,6 +104,7 @@ use Predis\Command\CommandInterface; * @method $this jsonmget(array $keys, string $path) * @method $this jsonset(string $key, string $path, string $value, ?string $subcommand = null) * @method $this jsonobjkeys(string $key, string $path = '$') + * @method $this jsonobjlen(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 c5c3854d..1dcae748 100644 --- a/src/ClientInterface.php +++ b/src/ClientInterface.php @@ -113,6 +113,7 @@ use Predis\Response\Status; * @method array jsonmget(array $keys, string $path) * @method string jsonset(string $key, string $path, string $value, ?string $subcommand = null) * @method array jsonobjkeys(string $key, string $path = '$') + * @method array jsonobjlen(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/JSONOBJLEN.php b/src/Command/Redis/Json/JSONOBJLEN.php new file mode 100644 index 00000000..f6b164a7 --- /dev/null +++ b/src/Command/Redis/Json/JSONOBJLEN.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 $expectedObjectLength + * @return void + * @requiresRedisJsonVersion >= 1.0.0 + */ + public function testReturnsLengthOfGivenJsonObject( + array $jsonArguments, + string $key, + string $path, + array $expectedObjectLength + ): void { + $redis = $this->getClient(); + + $redis->jsonset(...$jsonArguments); + + $this->assertSame($expectedObjectLength, $redis->jsonobjlen($key, $path)); + } + + public function jsonProvider(): array + { + return [ + 'on root level' => [ + ['key', '$', '{"key1":"value1","key2":"value2"}'], + 'key', + '$', + [2], + ], + 'on nested level' => [ + ['key', '$', '{"key1":"value1","key2":{"key1":"value1","key2":"value2"}}'], + 'key', + '$.key2', + [2], + ], + 'with same key on both levels' => [ + ['key', '$', '{"key1":{"key3":"value3","key4":"value4"},"key2":{"key1":{"key2":"value2"}}}'], + 'key', + '$..key1', + [2, 1], + ], + 'with one of the keys not a JSON object' => [ + ['key', '$', '{"key1":"value1","key2":{"key1":{"key2":"value2"}}}'], + 'key', + '$..key1', + [null, 1], + ], + ]; + } +}