diff --git a/src/ClientContextInterface.php b/src/ClientContextInterface.php index a2b54ad5..b24cf198 100644 --- a/src/ClientContextInterface.php +++ b/src/ClientContextInterface.php @@ -102,9 +102,10 @@ use Predis\Command\CommandInterface; * @method $this jsondel(string $key, string $path = '$') * @method $this jsonget(string $key, string $indent = '', string $newline = '', string $space = '', string ...$paths) * @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 jsonset(string $key, string $path, string $value, ?string $subcommand = null) + * @method $this jsonstrappend(string $key, string $path, string $value) * @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 1dcae748..13c5d1ef 100644 --- a/src/ClientInterface.php +++ b/src/ClientInterface.php @@ -111,9 +111,10 @@ use Predis\Response\Status; * @method int jsondel(string $key, string $path = '$') * @method string jsonget(string $key, string $indent = '', string $newline = '', string $space = '', string ...$paths) * @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 jsonset(string $key, string $path, string $value, ?string $subcommand = null) + * @method array jsonstrappend(string $key, string $path, string $value) * @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/JSONSTRAPPEND.php b/src/Command/Redis/Json/JSONSTRAPPEND.php new file mode 100644 index 00000000..9b11458e --- /dev/null +++ b/src/Command/Redis/Json/JSONSTRAPPEND.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 string $value + * @param array $expectedStringLength + * @param string $expectedModifiedJson + * @return void + * @requiresRedisJsonVersion >= 1.0.0 + */ + public function testAppendStringToExistingJsonString( + array $jsonArguments, + string $key, + string $path, + string $value, + array $expectedStringLength, + string $expectedModifiedJson + ): void { + $redis = $this->getClient(); + + $redis->jsonset(...$jsonArguments); + $actualResponse = $redis->jsonstrappend($key, $path, $value); + + $this->assertSame($expectedStringLength, $actualResponse); + $this->assertSame($expectedModifiedJson, $redis->jsonget($key)); + } + + public function jsonProvider(): array + { + return [ + 'appends to json string on root level' => [ + ['key', '$', '{"key1":"value1","key2":"value2"}'], + 'key', + '$.key2', + '"foo"', + [9], + '{"key1":"value1","key2":"value2foo"}', + ], + 'appends to json string on nested level' => [ + ['key', '$', '{"key1":{"key2":"value2"}}'], + 'key', + '$..key2', + '"foo"', + [9], + '{"key1":{"key2":"value2foo"}}', + ], + 'appends to json string on both levels' => [ + ['key', '$', '{"key1":{"key2":"value2"},"key2":"value2"}'], + 'key', + '$..key2', + '"foo"', + [9, 9], + '{"key1":{"key2":"value2foo"},"key2":"value2foo"}', + ], + 'appends to non-json string' => [ + ['key', '$', '{"key1":{"key2":[1,2,3]}}'], + 'key', + '$..key2', + '"foo"', + [null], + '{"key1":{"key2":[1,2,3]}}', + ], + ]; + } +}