diff --git a/src/ClientContextInterface.php b/src/ClientContextInterface.php index f68ad04d..ea9c0640 100644 --- a/src/ClientContextInterface.php +++ b/src/ClientContextInterface.php @@ -104,6 +104,7 @@ use Predis\Command\CommandInterface; * @method $this jsonarrinsert(string $key, string $path, int $index, string ...$value) * @method $this jsonarrlen(string $key, string $path = '$') * @method $this jsonarrpop(string $key, string $path = '$', int $index = -1) + * @method $this jsonarrtrim(string $key, string $path, int $start, int $stop) * @method $this jsondel(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) diff --git a/src/ClientInterface.php b/src/ClientInterface.php index 782b6472..25fabece 100644 --- a/src/ClientInterface.php +++ b/src/ClientInterface.php @@ -113,6 +113,7 @@ use Predis\Response\Status; * @method array jsonarrinsert(string $key, string $path, int $index, string ...$value) * @method array jsonarrlen(string $key, string $path = '$') * @method array jsonarrpop(string $key, string $path = '$', int $index = -1) + * @method array jsonarrtrim(string $key, string $path, int $start, int $stop) * @method int jsondel(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) diff --git a/src/Command/Redis/Json/JSONARRTRIM.php b/src/Command/Redis/Json/JSONARRTRIM.php new file mode 100644 index 00000000..7ea5620d --- /dev/null +++ b/src/Command/Redis/Json/JSONARRTRIM.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 int $start + * @param int $stop + * @param array $expectedArrayLength + * @param string $expectedModifiedJson + * @return void + * @requiresRedisJsonVersion >= 1.0.0 + */ + public function testCorrectlyTrimGivenJsonArray( + array $jsonArguments, + string $key, + string $path, + int $start, + int $stop, + array $expectedArrayLength, + string $expectedModifiedJson + ): void { + $redis = $this->getClient(); + + $redis->jsonset(...$jsonArguments); + $actualResponse = $redis->jsonarrtrim($key, $path, $start, $stop); + + $this->assertSame($expectedArrayLength, $actualResponse); + $this->assertSame($expectedModifiedJson, $redis->jsonget($key)); + } + + public function jsonProvider(): array + { + return [ + 'trim array from start to stop' => [ + ['key', '$', '{"key1":"value1","key2":[1,2,3,4,5,6]}'], + 'key', + '$.key2', + 1, + 4, + [4], + '{"key1":"value1","key2":[2,3,4,5]}', + ], + 'trim all values except first with 0 start and stop' => [ + ['key', '$', '{"key1":"value1","key2":[1,2,3,4,5,6]}'], + 'key', + '$.key2', + 0, + 0, + [1], + '{"key1":"value1","key2":[1]}', + ], + 'trim arrays with same keys on root and nested levels' => [ + ['key', '$', '{"key1":{"key2":[1,2,3,4,5,6]},"key2":[1,2,3,4,5,6]}'], + 'key', + '$..key2', + 1, + 4, + [4, 4], + '{"key1":{"key2":[2,3,4,5]},"key2":[2,3,4,5]}', + ], + 'do not trim on non-array key' => [ + ['key', '$', '{"key1":"value1","key2":"value2"}'], + 'key', + '$.key2', + 1, + 4, + [null], + '{"key1":"value1","key2":"value2"}', + ], + ]; + } +}