From a93e1e5a998452c795cfb125b4282cdc2dbd0cf3 Mon Sep 17 00:00:00 2001 From: Vladyslav Vildanov <117659936+vladvildanov@users.noreply.github.com> Date: Tue, 28 Feb 2023 16:36:46 +0200 Subject: [PATCH] Extended Json support by implementing JSON.DEBUG command (#1182) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Added support for new arguments for BITPOS, BITCOUNT commands (#1045) * Added support for new arguments for EXPIRE, EXPIREAT commands (#1046) * Extended core support by implementing SORT_RO command (#1044) * Added support for SORT_RO command * Codestyle fixes * Added command description --------- Co-authored-by: Vladyslav Vildanov * fix deprecated call * Added support for container commands (#1049) * Added support for container commands FUNCTION LOAD, FUNCTION DELETE and FCALL * Changed ContainerInterface and AbstractContainer * Re-implement logic of abstract methods --------- Co-authored-by: Vladyslav Vildanov * Added stream commands to KeyPrefixProcessor (#1051) Co-authored-by: Vladyslav Vildanov * Fix return type of ReplicationInterface::getSlaves (#1111) * Codestyle fixes * Changed return annotation * Added support for JSON.DEBUG command * Codestyle fixes --------- Co-authored-by: Vladyslav Vildanov Co-authored-by: Till Krüss Co-authored-by: Stephan --- examples/Commands/Json/json_debug_memory.php | 28 ++++ src/ClientContextInterface.php | 2 + src/ClientInterface.php | 2 + src/Command/Container/Json/JSONDEBUG.php | 27 ++++ src/Command/Redis/Json/JSONDEBUG.php | 28 ++++ .../Command/Redis/Json/JSONDEBUG_Test.php | 130 ++++++++++++++++++ 6 files changed, 217 insertions(+) create mode 100644 examples/Commands/Json/json_debug_memory.php create mode 100644 src/Command/Container/Json/JSONDEBUG.php create mode 100644 src/Command/Redis/Json/JSONDEBUG.php create mode 100644 tests/Predis/Command/Redis/Json/JSONDEBUG_Test.php diff --git a/examples/Commands/Json/json_debug_memory.php b/examples/Commands/Json/json_debug_memory.php new file mode 100644 index 00000000..9746b00f --- /dev/null +++ b/examples/Commands/Json/json_debug_memory.php @@ -0,0 +1,28 @@ +jsonset('key', '$', '{"key1":"value1","key2":"value2"}'); + +// 2. Dump information about json memory usage in bytes +$response = $client->jsondebug->memory('key', '$'); + +echo 'Response:' . "\n"; +print_r($response); diff --git a/src/ClientContextInterface.php b/src/ClientContextInterface.php index 53ed3d55..4a9fe0d2 100644 --- a/src/ClientContextInterface.php +++ b/src/ClientContextInterface.php @@ -26,6 +26,7 @@ use Predis\Command\Argument\Server\LimitOffsetCount; use Predis\Command\Argument\Server\To; use Predis\Command\CommandInterface; use Predis\Command\Container\FUNCTIONS; +use Predis\Command\Container\Json\JSONDEBUG; use Predis\Command\Container\Search\FTCONFIG; /** @@ -299,6 +300,7 @@ use Predis\Command\Container\Search\FTCONFIG; * Container commands * @property FUNCTIONS $function * @property FTCONFIG $ftconfig + * @property JSONDEBUG $jsondebug */ interface ClientContextInterface { diff --git a/src/ClientInterface.php b/src/ClientInterface.php index 4b372ca5..ac436467 100644 --- a/src/ClientInterface.php +++ b/src/ClientInterface.php @@ -26,6 +26,7 @@ use Predis\Command\Argument\Server\LimitOffsetCount; use Predis\Command\Argument\Server\To; use Predis\Command\CommandInterface; use Predis\Command\Container\FUNCTIONS; +use Predis\Command\Container\Json\JSONDEBUG; use Predis\Command\Container\Search\FTCONFIG; use Predis\Command\FactoryInterface; use Predis\Configuration\OptionsInterface; @@ -317,6 +318,7 @@ use Predis\Response\Status; * Container commands * @property FUNCTIONS $function * @property FTCONFIG $ftconfig + * @property JSONDEBUG $jsondebug */ interface ClientInterface { diff --git a/src/Command/Container/Json/JSONDEBUG.php b/src/Command/Container/Json/JSONDEBUG.php new file mode 100644 index 00000000..0bf4e862 --- /dev/null +++ b/src/Command/Container/Json/JSONDEBUG.php @@ -0,0 +1,27 @@ +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 $expectedMemoryUsage + * @return void + * @requiresRedisJsonVersion >= 1.0.0 + */ + public function testMemoryReturnsCorrectMemoryUsageAboutJson( + array $jsonArguments, + string $key, + string $path, + array $expectedMemoryUsage + ): void { + $redis = $this->getClient(); + + $redis->jsonset(...$jsonArguments); + + $this->assertSame($expectedMemoryUsage, $redis->jsondebug->memory($key, $path)); + } + + /** + * @group connected + * @return void + * @requiresRedisJsonVersion >= 1.0.0 + */ + public function testHelpReturnsInformationAboutContainerCommands(): void + { + $redis = $this->getClient(); + + $actualResponse = $redis->jsondebug->help(); + + $this->assertStringContainsString('MEMORY', $actualResponse[0]); + $this->assertStringContainsString('HELP', $actualResponse[1]); + } + + public function jsonProvider(): array + { + return [ + 'on root level' => [ + ['key', '$', '{"key1":"value1","key2":"value2"}'], + 'key', + '$', + [44], + ], + 'on nested level' => [ + ['key', '$', '{"key1":{"key2":"value2"}}'], + 'key', + '$..key2', + [14], + ], + 'with same keys on both levels' => [ + ['key', '$', '{"key1":{"key2":"value2"},"key2":"value2"}'], + 'key', + '$..key2', + [14, 14], + ], + 'with wrong key' => [ + ['key', '$', '{"key1":{"key2":"value2"}}'], + 'key1', + '$', + [], + ], + 'with wrong path' => [ + ['key', '$', '{"key1":{"key2":"value2"}}'], + 'key', + '$.key3', + [], + ], + ]; + } +}