From 0dd3a293ac3cf9f4608fb856d9cd97d1e10cef09 Mon Sep 17 00:00:00 2001 From: Vladyslav Vildanov Date: Mon, 3 Apr 2023 15:36:15 +0300 Subject: [PATCH] Resolve merge conflicts --- src/ClientContextInterface.php | 4 +- src/Command/Redis/Container/ACL.php | 28 ++++++ .../Redis/Container/AbstractContainer.php | 42 +++++++++ .../Redis/Container/ContainerFactory.php | 54 +++++++++++ .../Redis/Container/ContainerInterface.php | 33 +++++++ .../Redis/Container/FunctionContainer.php | 27 ++++++ .../Redis/Container/AbstractContainerTest.php | 90 +++++++++++++++++++ .../Redis/Container/ContainerFactoryTest.php | 64 +++++++++++++ tests/Predis/Transaction/MultiExecTest.php | 3 +- 9 files changed, 342 insertions(+), 3 deletions(-) create mode 100644 src/Command/Redis/Container/ACL.php create mode 100644 src/Command/Redis/Container/AbstractContainer.php create mode 100644 src/Command/Redis/Container/ContainerFactory.php create mode 100644 src/Command/Redis/Container/ContainerInterface.php create mode 100644 src/Command/Redis/Container/FunctionContainer.php create mode 100644 tests/Predis/Command/Redis/Container/AbstractContainerTest.php create mode 100644 tests/Predis/Command/Redis/Container/ContainerFactoryTest.php diff --git a/src/ClientContextInterface.php b/src/ClientContextInterface.php index 19406649..75b11004 100644 --- a/src/ClientContextInterface.php +++ b/src/ClientContextInterface.php @@ -70,6 +70,7 @@ use Predis\Command\Container\Search\FTCURSOR; * @method $this ttl($key) * @method $this type($key) * @method $this append($key, $value) + * @method $this bitcount($key, $start = null, $end = null, string $index = 'byte') * @method $this bfadd(string $key, $item) * @method $this bfexists(string $key, $item) * @method $this bfinfo(string $key, string $modifier = '') @@ -79,7 +80,6 @@ use Predis\Command\Container\Search\FTCURSOR; * @method $this bfmexists(string $key, ...$item) * @method $this bfreserve(string $key, float $errorRate, int $capacity, int $expansion = -1, bool $nonScaling = false) * @method $this bfscandump(string $key, int $iterator) - * @method $this bitcount(string $key, $start = null, $end = null, string $index = 'byte') * @method $this bitop($operation, $destkey, $key) * @method $this bitfield($key, $subcommand, ...$subcommandArg) * @method $this bitpos($key, $bit, $start = null, $end = null, string $index = 'byte') @@ -108,6 +108,7 @@ use Predis\Command\Container\Search\FTCURSOR; * @method $this decr($key) * @method $this decrby($key, $decrement) * @method $this failover(?To $to = null, bool $abort = false, int $timeout = -1) + * @method $this get($key) * @method $this fcall(string $function, array $keys, ...$args) * @method $this fcall_ro(string $function, array $keys, ...$args) * @method $this ftaggregate(string $index, string $query, ?AggregateArguments $arguments = null) @@ -132,7 +133,6 @@ use Predis\Command\Container\Search\FTCURSOR; * @method $this ftsyndump(string $index) * @method $this ftsynupdate(string $index, string $synonymGroupId, ?SynUpdateArguments $arguments = null, string ...$terms) * @method $this fttagvals(string $index, string $fieldName) - * @method $this get($key) * @method $this getbit($key, $offset) * @method $this getex(string $key, $modifier = '', $value = false) * @method $this getrange($key, $start, $end) diff --git a/src/Command/Redis/Container/ACL.php b/src/Command/Redis/Container/ACL.php new file mode 100644 index 00000000..2699d37e --- /dev/null +++ b/src/Command/Redis/Container/ACL.php @@ -0,0 +1,28 @@ +client = $client; + } + + /** + * {@inheritDoc} + */ + public function __call($subcommandID, $arguments) + { + array_unshift($arguments, strtoupper($subcommandID)); + + return $this->client->executeCommand( + $this->client->createCommand($this->getContainerCommandId(), $arguments) + ); + } + + abstract public function getContainerCommandId(): string; +} diff --git a/src/Command/Redis/Container/ContainerFactory.php b/src/Command/Redis/Container/ContainerFactory.php new file mode 100644 index 00000000..c9a0665f --- /dev/null +++ b/src/Command/Redis/Container/ContainerFactory.php @@ -0,0 +1,54 @@ + FunctionContainer::class, + ]; + + /** + * Creates container command. + * + * @param ClientInterface $client + * @param string $containerCommandID + * @return ContainerInterface + */ + public static function create(ClientInterface $client, string $containerCommandID): ContainerInterface + { + $containerCommandID = strtoupper($containerCommandID); + + if (class_exists($containerClass = self::CONTAINER_NAMESPACE . '\\' . $containerCommandID)) { + return new $containerClass($client); + } + + if (array_key_exists($containerCommandID, self::$specialMappings)) { + $containerClass = self::$specialMappings[$containerCommandID]; + + return new $containerClass($client); + } + + throw new UnexpectedValueException('Given command is not supported.'); + } +} diff --git a/src/Command/Redis/Container/ContainerInterface.php b/src/Command/Redis/Container/ContainerInterface.php new file mode 100644 index 00000000..ce3989b5 --- /dev/null +++ b/src/Command/Redis/Container/ContainerInterface.php @@ -0,0 +1,33 @@ +arguments = ['arg1', 'arg2']; + $this->expectedValue = ['value']; + $this->mockCommand = $this->getMockBuilder(CommandInterface::class)->getMock(); + $this->mockClient = $this->getMockBuilder(ClientInterface::class)->getMock(); + + $this->testClass = new class($this->mockClient) extends AbstractContainer { + public function getContainerCommandId(): string + { + return 'test'; + } + }; + } + + /** + * @return void + */ + public function testGetContainerId(): void + { + $this->assertSame('test', $this->testClass->getContainerCommandId()); + } + + /** + * @return void + */ + public function testCallReturnsValidCommandResponse(): void + { + $modifiedArguments = ['TEST', ['arg1', 'arg2']]; + + $this->mockClient + ->expects($this->once()) + ->method('createCommand') + ->with($this->equalTo('test'), $modifiedArguments) + ->willReturn($this->mockCommand); + + $this->mockClient + ->expects($this->once()) + ->method('executeCommand') + ->with($this->mockCommand) + ->willReturn($this->expectedValue); + + $this->assertSame($this->expectedValue, $this->testClass->test($this->arguments)); + } +} diff --git a/tests/Predis/Command/Redis/Container/ContainerFactoryTest.php b/tests/Predis/Command/Redis/Container/ContainerFactoryTest.php new file mode 100644 index 00000000..cf6d28c4 --- /dev/null +++ b/tests/Predis/Command/Redis/Container/ContainerFactoryTest.php @@ -0,0 +1,64 @@ +mockClient = $this->getMockBuilder(ClientInterface::class)->getMock(); + $this->expectedContainer = new FunctionContainer($this->mockClient); + $this->factory = new ContainerFactory(); + } + + /** + * @return void + */ + public function testCreatesReturnsExistingCommandContainerClass(): void + { + $this->assertEquals( + $this->expectedContainer, + $this->factory::create($this->mockClient, 'function') + ); + } + + /** + * @return void + */ + public function testThrowsExceptionOnNonExistingCommand(): void + { + $this->expectException(UnexpectedValueException::class); + $this->expectExceptionMessage('Given command is not supported.'); + + $this->factory::create($this->mockClient, 'foobar'); + } +} diff --git a/tests/Predis/Transaction/MultiExecTest.php b/tests/Predis/Transaction/MultiExecTest.php index e42d0251..95cac4a4 100644 --- a/tests/Predis/Transaction/MultiExecTest.php +++ b/tests/Predis/Transaction/MultiExecTest.php @@ -886,7 +886,8 @@ class MultiExecTest extends PredisTestCase ?array $expected = [], ?array &$commands = [], ?array &$cas = [] - ): callable { + ): callable + { $multi = $watch = $abort = false; return function (CommandInterface $command) use (&$expected, &$commands, &$cas, &$multi, &$watch, &$abort) {