mirror of
https://github.com/predis/predis.git
synced 2026-08-31 12:43:31 +00:00
Extended RedisJson support by implementing JSON.TOGGLE command (#1008)
* 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.TOGGLE 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:
committed by
GitHub
parent
e29ee802d6
commit
fbd52c26e1
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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.toggle/
|
||||
*
|
||||
* Toggle a Boolean value stored at path
|
||||
*/
|
||||
class JSONTOGGLE extends RedisCommand
|
||||
{
|
||||
public function getId()
|
||||
{
|
||||
return 'JSON.TOGGLE';
|
||||
}
|
||||
}
|
||||
@@ -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 JSONTOGGLE_Test extends PredisCommandTestCase
|
||||
{
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
protected function getExpectedCommand(): string
|
||||
{
|
||||
return JSONTOGGLE::class;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
protected function getExpectedId(): string
|
||||
{
|
||||
return 'JSONTOGGLE';
|
||||
}
|
||||
|
||||
/**
|
||||
* @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 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"}',
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user