Extended RedisJson support by implementing JSON.CLEAR command (#1004)

* Added CommandResolver, moved resolve command logic there, added ClientConfiguration object

* Fixed test, added dependecies

* Added resolved command to commands array

* Used aggregation approach for modules

* Added decorator to check Redis JSON module version

* Added support for JSON.SET and JSON.GET commands

* Added separate workflow for redis-stack tests

* Changed docker imange name to correct one

* Fixed indentation

* Added test coverage for JSON.GET command

* Changed module version resolving using annotations mapping

* Re-written CommandResolver test

* Update ClientInterface.php

* Changes to CI, readme, removed unused modules from configuration

* Fixed build badge URL

* Refactored annotation check to be generic for each module

* Fixed bug with incorrect tests skip

* Added CommandResolver, moved resolve command logic there, added ClientConfiguration object

* Fixed test, added dependecies

* Added resolved command to commands array

* Used aggregation approach for modules

* Added decorator to check Redis JSON module version

* Added support for JSON.SET and JSON.GET commands

* Added separate workflow for redis-stack tests

* Changed docker imange name to correct one

* Fixed indentation

* Added test coverage for JSON.GET command

* Changed module version resolving using annotations mapping

* Re-written CommandResolver test

* Update ClientInterface.php

* Changes to CI, readme, removed unused modules from configuration

* Fixed build badge URL

* Refactored annotation check to be generic for each module

* Fixed bug with incorrect tests skip

* Fixed naming issue with nxXx argument

* Removed redundant trait

* Fixed NxXxArgument test

* Added support for JSON.CLEAR command

Co-authored-by: Vladyslav Vildanov <vladyslavvildanov@Vladyslav-Vildanov-MacBook-Pro.local>
Co-authored-by: Till Krüss <tillkruss@users.noreply.github.com>
This commit is contained in:
Vladyslav Vildanov
2023-01-26 17:12:05 +02:00
committed by GitHub
parent c72fda22e1
commit 9a7490e3af
4 changed files with 154 additions and 0 deletions
+1
View File
@@ -105,6 +105,7 @@ use Predis\Command\CommandInterface;
* @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 jsonclear(string $key, string $path = '$')
* @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)
+1
View File
@@ -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 int jsonclear(string $key, string $path = '$')
* @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)
+28
View File
@@ -0,0 +1,28 @@
<?php
/*
* This file is part of the Predis package.
*
* (c) 2009-2020 Daniele Alessandri
* (c) 2021-2023 Till Krüss
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Command\Redis\Json;
use Predis\Command\Command as RedisCommand;
/**
* @see https://redis.io/commands/json.clear/
*
* Clear container values (arrays/objects) and set numeric values to 0
*/
class JSONCLEAR extends RedisCommand
{
public function getId()
{
return 'JSON.CLEAR';
}
}
@@ -0,0 +1,124 @@
<?php
/*
* This file is part of the Predis package.
*
* (c) 2009-2020 Daniele Alessandri
* (c) 2021-2023 Till Krüss
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Command\Redis\Json;
use Predis\Command\Redis\PredisCommandTestCase;
class JSONCLEAR_Test extends PredisCommandTestCase
{
/**
* {@inheritDoc}
*/
protected function getExpectedCommand(): string
{
return JSONCLEAR::class;
}
/**
* {@inheritDoc}
*/
protected function getExpectedId(): string
{
return 'JSONCLEAR';
}
/**
* @group disconnected
*/
public function testFilterArguments(): void
{
$arguments = ['key', '$..'];
$expected = ['key', '$..'];
$command = $this->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 $expectedClearValues
* @param string $expectedModifiedJson
* @return void
* @requiresRedisJsonVersion >= 2.0.0
*/
public function testClearValuesOnArraysAndObjects(
array $jsonArguments,
string $key,
string $path,
int $expectedClearValues,
string $expectedModifiedJson
): void {
$redis = $this->getClient();
$redis->jsonset(...$jsonArguments);
$actualResponse = $redis->jsonclear($key, $path);
$this->assertSame($expectedClearValues, $actualResponse);
$this->assertSame($expectedModifiedJson, $redis->jsonget($key));
}
public function jsonProvider(): array
{
return [
'with array values' => [
['key', '$', '{"key1":"value1","key2":[1,2,3,4,5,6]}'],
'key',
'$.key2',
1,
'{"key1":"value1","key2":[]}',
],
'with json object' => [
['key', '$', '{"key1":"value1","key2":{"key3":"value3"}}'],
'key',
'$.key2',
1,
'{"key1":"value1","key2":{}}',
],
'with numeric values' => [
['key', '$', '{"key1":"value1","key2":1}'],
'key',
'$.key2',
1,
'{"key1":"value1","key2":0}',
],
'with all accepted values' => [
['key', '$', '{"key1":1,"key2":{"key3":"value3"},"key3":[1,2,3]}'],
'key',
'$.*',
3,
'{"key1":0,"key2":{},"key3":[]}',
],
'with string and boolean value' => [
['key', '$', '{"key1":"value1","key2":true}'],
'key',
'$.*',
0,
'{"key1":"value1","key2":true}',
],
];
}
}