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', + [], + ], + ]; + } +}