From d18e20c98fa89de0b89986d06c94d7f6062b6073 Mon Sep 17 00:00:00 2001 From: Vladyslav Vildanov <117659936+vladvildanov@users.noreply.github.com> Date: Fri, 5 May 2023 11:52:11 +0300 Subject: [PATCH] Added RESP3 protocol option, establish RESP3 connection (#1267) * Added RESP3 protocol option, establish RESP3 connection * Removed method from the interface --- src/Connection/AbstractConnection.php | 8 ++++++++ src/Connection/Factory.php | 6 ++++++ src/Connection/Parameters.php | 1 + src/Connection/ParametersInterface.php | 1 + tests/Predis/Connection/FactoryTest.php | 17 +++++++++++++++++ tests/Predis/Connection/ParametersTest.php | 1 + .../Predis/Connection/StreamConnectionTest.php | 17 +++++++++++++++++ 7 files changed, 51 insertions(+) diff --git a/src/Connection/AbstractConnection.php b/src/Connection/AbstractConnection.php index 5383924c..84798e1c 100644 --- a/src/Connection/AbstractConnection.php +++ b/src/Connection/AbstractConnection.php @@ -101,6 +101,14 @@ abstract class AbstractConnection implements NodeConnectionInterface $this->initCommands[] = $command; } + /** + * {@inheritdoc} + */ + public function getInitCommands(): array + { + return $this->initCommands; + } + /** * {@inheritdoc} */ diff --git a/src/Connection/Factory.php b/src/Connection/Factory.php index ad472360..2fa446a3 100644 --- a/src/Connection/Factory.php +++ b/src/Connection/Factory.php @@ -174,6 +174,12 @@ class Factory implements FactoryInterface ); } + if (isset($parameters->protocol) && (int) $parameters->protocol > 2) { + $connection->addConnectCommand( + new RawCommand('HELLO', [$parameters->protocol, 'SETNAME', 'predis']) + ); + } + if (isset($parameters->database) && strlen($parameters->database)) { $connection->addConnectCommand( new RawCommand('SELECT', [$parameters->database]) diff --git a/src/Connection/Parameters.php b/src/Connection/Parameters.php index 170d7e28..c03fc543 100644 --- a/src/Connection/Parameters.php +++ b/src/Connection/Parameters.php @@ -25,6 +25,7 @@ class Parameters implements ParametersInterface 'scheme' => 'tcp', 'host' => '127.0.0.1', 'port' => 6379, + 'protocol' => 2, ]; /** diff --git a/src/Connection/ParametersInterface.php b/src/Connection/ParametersInterface.php index 25c7e1ae..d33197c6 100644 --- a/src/Connection/ParametersInterface.php +++ b/src/Connection/ParametersInterface.php @@ -22,6 +22,7 @@ namespace Predis\Connection; * @property string $scheme Connection scheme, such as 'tcp' or 'unix'. * @property string $host IP address or hostname of Redis. * @property int $port TCP port on which Redis is listening to. + * @property int $protocol Version of RESP protocol. * @property string $path Path of a UNIX domain socket file. * @property string $alias Alias for the connection. * @property float $timeout Timeout for the connect() operation. diff --git a/tests/Predis/Connection/FactoryTest.php b/tests/Predis/Connection/FactoryTest.php index 4d98dd6f..59f58eed 100644 --- a/tests/Predis/Connection/FactoryTest.php +++ b/tests/Predis/Connection/FactoryTest.php @@ -12,6 +12,7 @@ namespace Predis\Connection; +use Predis\Command\RawCommand; use PredisTestCase; use ReflectionObject; use stdClass; @@ -538,6 +539,22 @@ class FactoryTest extends PredisTestCase $factory->create('test://127.0.0.1'); } + /** + * @group disconnected + * @return void + */ + public function testCreatesResp3ConnectionOnProtocolParameterGiven(): void + { + $parameters = ['protocol' => 3]; + + $factory = new Factory(); + $connection = $factory->create($parameters); + $initCommands = $connection->getInitCommands(); + + $this->assertInstanceOf(RawCommand::class, $initCommands[0]); + $this->assertSame('HELLO', $initCommands[0]->getId()); + } + // ******************************************************************** // // ---- HELPER METHODS ------------------------------------------------ // // ******************************************************************** // diff --git a/tests/Predis/Connection/ParametersTest.php b/tests/Predis/Connection/ParametersTest.php index 700ec4a4..ab4bf802 100644 --- a/tests/Predis/Connection/ParametersTest.php +++ b/tests/Predis/Connection/ParametersTest.php @@ -401,6 +401,7 @@ class ParametersTest extends PredisTestCase 'scheme' => 'tcp', 'host' => '127.0.0.1', 'port' => 6379, + 'protocol' => 2, ]; } diff --git a/tests/Predis/Connection/StreamConnectionTest.php b/tests/Predis/Connection/StreamConnectionTest.php index 55c30c08..ffcbf5d1 100644 --- a/tests/Predis/Connection/StreamConnectionTest.php +++ b/tests/Predis/Connection/StreamConnectionTest.php @@ -195,4 +195,21 @@ class StreamConnectionTest extends PredisConnectionTestCase $this->assertArrayHasKey('tcp_nodelay', $options['socket']); $this->assertFalse($options['socket']['tcp_nodelay']); } + + /** + * @group disconnected + * @return void + */ + public function testGetInitCommandsReturnsGivenInitCommands(): void + { + $command = new RawCommand('HELLO', [3]); + + $connection = $this->createConnection(); + $connection->addConnectCommand($command); + + $initCommands = $connection->getInitCommands(); + + $this->assertInstanceOf(RawCommand::class, $initCommands[0]); + $this->assertSame('HELLO', $initCommands[0]->getId()); + } }