diff --git a/src/ClientContextInterface.php b/src/ClientContextInterface.php index 0de20a32..b1dc6fbd 100644 --- a/src/ClientContextInterface.php +++ b/src/ClientContextInterface.php @@ -178,6 +178,7 @@ use Predis\Command\Redis\Container\Search\FTCURSOR; * @method $this jsonforget(string $key, string $path = '$') * @method $this jsonget(string $key, string $indent = '', string $newline = '', string $space = '', string ...$paths) * @method $this jsonnumincrby(string $key, string $path, int $value) + * @method $this jsonmerge(string $key, string $path, string $value) * @method $this jsonmget(array $keys, string $path) * @method $this jsonobjkeys(string $key, string $path = '$') * @method $this jsonobjlen(string $key, string $path = '$') diff --git a/src/ClientInterface.php b/src/ClientInterface.php index e46209df..3e5f579b 100644 --- a/src/ClientInterface.php +++ b/src/ClientInterface.php @@ -187,6 +187,7 @@ use Predis\Response\Status; * @method int jsonforget(string $key, string $path = '$') * @method string jsonget(string $key, string $indent = '', string $newline = '', string $space = '', string ...$paths) * @method string jsonnumincrby(string $key, string $path, int $value) + * @method Status jsonmerge(string $key, string $path, string $value) * @method array jsonmget(array $keys, string $path) * @method array jsonobjkeys(string $key, string $path = '$') * @method array jsonobjlen(string $key, string $path = '$') diff --git a/src/Command/Redis/Json/JSONMERGE.php b/src/Command/Redis/Json/JSONMERGE.php new file mode 100644 index 00000000..a1322283 --- /dev/null +++ b/src/Command/Redis/Json/JSONMERGE.php @@ -0,0 +1,29 @@ +getCommand(); + $command->setArguments($arguments); + + $this->assertSame($expected, $command->getArguments()); + } + + /** + * @group disconnected + */ + public function testParseResponse(): void + { + $this->assertSame(1, $this->getCommand()->parseResponse(1)); + } + + /** + * @dataProvider jsonProvider + * @group connected + * @param array $setArguments + * @param array $mergeArguments + * @param string $expectedResponse + * @return void + * @requiresRedisJsonVersion >= 2.6.0 + */ + public function testMergeCorrectlyMergeJsonValues( + array $setArguments, + array $mergeArguments, + string $expectedResponse + ): void { + $redis = $this->getClient(); + + $this->assertEquals('OK', $redis->jsonset(...$setArguments)); + $this->assertEquals('OK', $redis->jsonmerge(...$mergeArguments)); + $this->assertEquals($expectedResponse, $redis->jsonget('key')); + } + + public function jsonProvider(): array + { + return [ + 'create non-existing value' => [ + ['key', '$', '{"a":2}'], + ['key', '$.b', '8'], + '{"a":2,"b":8}', + ], + 'replace existing value' => [ + ['key', '$', '{"a":2}'], + ['key', '$.a', '3'], + '{"a":3}', + ], + 'replace an array' => [ + ['key', '$', '{"a":[2,4,6,8]}'], + ['key', '$.a', '[10,12]'], + '{"a":[10,12]}', + ], + 'merge in multiple-paths' => [ + ['key', '$', '{"f1": {"a":1}, "f2":{"a":2}}'], + ['key', '$', '{"f2":{"a":3, "b":4}, "f3":[2,4,6]}'], + '{"f1":{"a":1},"f2":{"a":3,"b":4},"f3":[2,4,6]}', + ], + ]; + } +}