diff --git a/src/ClientContextInterface.php b/src/ClientContextInterface.php index bccb549a..f8095bc9 100644 --- a/src/ClientContextInterface.php +++ b/src/ClientContextInterface.php @@ -116,6 +116,7 @@ use Predis\Command\CommandInterface; * @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 jsontoggle(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 a3cda275..92ae5191 100644 --- a/src/ClientInterface.php +++ b/src/ClientInterface.php @@ -125,6 +125,7 @@ use Predis\Response\Status; * @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 array jsontoggle(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/JSONTOGGLE.php b/src/Command/Redis/Json/JSONTOGGLE.php new file mode 100644 index 00000000..7bd33da9 --- /dev/null +++ b/src/Command/Redis/Json/JSONTOGGLE.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 $expectedResponse + * @param string $expectedModifiedJson + * @return void + * @requiresRedisJsonVersion >= 2.0.0 + */ + public function testToggleChangesBooleanValueToOpposite( + array $jsonArguments, + string $key, + string $path, + array $expectedResponse, + string $expectedModifiedJson + ): void { + $redis = $this->getClient(); + + $redis->jsonset(...$jsonArguments); + $actualResponse = $redis->jsontoggle($key, $path); + + $this->assertSame($expectedResponse, $actualResponse); + $this->assertSame($expectedModifiedJson, $redis->jsonget($key)); + } + + public function jsonProvider(): array + { + return [ + 'on root level' => [ + ['key', '$', '{"key1":true}'], + 'key', + '$.key1', + [0], + '{"key1":false}', + ], + 'on nested level' => [ + ['key', '$', '{"key1":{"key2":false}}'], + 'key', + '$..key2', + [1], + '{"key1":{"key2":true}}', + ], + 'with same keys on both levels' => [ + ['key', '$', '{"key1":{"key2":false},"key2":true}'], + 'key', + '$..key2', + [0, 1], + '{"key1":{"key2":true},"key2":false}', + ], + 'on non-boolean value' => [ + ['key', '$', '{"key1":"value1"}'], + 'key', + '$.key1', + [null], + '{"key1":"value1"}', + ], + 'on wrong path' => [ + ['key', '$', '{"key1":"value1"}'], + 'key', + '$.key2', + [], + '{"key1":"value1"}', + ], + ]; + } +}