8000 Add parameter opt out for setinfo commands by kylekatarnls · Pull Request #1370 · predis/predis · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add parameter opt out for setinfo commands #1370

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: v2.x
Choose a base branch
from
16 changes: 8 additions & 8 deletions src/Connection/Factory.php
Original file line number Diff line number Diff line change
8000 Expand Up @@ -13,7 +13,6 @@
namespace Predis\Connection;

use InvalidArgumentException;
use Predis\Client;
use Predis\Command\RawCommand;
use ReflectionClass;
use UnexpectedValueException;
Expand Down Expand Up @@ -176,13 +175,14 @@ protected function prepareConnection(NodeConnectionInterface $connection)
}

if (!$connection instanceof RelayConnection) {
$connection->addConnectCommand(
new RawCommand('CLIENT', ['SETINFO', 'LIB-NAME', 'predis'])
);

$connection->addConnectCommand(
new RawCommand('CLIENT', ['SETINFO', 'LIB-VER', Client::VERSION])
);
if ($parameters->set_client_info ?? false) {
$connection->addConnectCommand(
new RawCommand('CLIENT', ['SETINFO', 'LIB-NAME', $parameters->client_name])
);
$connection->addConnectCommand(
new RawCommand('CLIENT', ['SETINFO', 'LIB-VER', $parameters->client_version])
);
}
}

if (isset($parameters->database) && strlen($parameters->database)) {
Expand Down
18 changes: 18 additions & 0 deletions src/Connection/Parameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
namespace Predis\Connection;

use InvalidArgumentException;
use Predis\Client;

/**
* Container for connection parameters used to initialize connections to Redis.
Expand All @@ -25,6 +26,9 @@ class Parameters implements ParametersInterface
'scheme' => 'tcp',
'host' => '127.0.0.1',
'port' => 6379,
'set_client_info' => true,
'client_name' => 'predis',
'client_version' => Client::VERSION,
];

/**
Expand All @@ -41,6 +45,7 @@ class Parameters implements ParametersInterface
public function __construct(array $parameters = [])
{
$this->parameters = $this->filter($parameters + static::$defaults);
$this->assertCorrectVersion($this->parameters['client_version']);
}

/**
Expand Down Expand Up @@ -147,6 +152,19 @@ public static function parse($uri)
return $parsed;
}

/**
* Asserts that version matches correct version pattern.
*
* @param string $version
* @return void
*/
private function assertCorrectVersion(string $version): void
{
if (!preg_match('/^(\d+\.?)/', $version)) {
throw new InvalidArgumentException('Given version does not not match version pattern - xx.xx');
}
}

/**
* {@inheritdoc}
*/
Expand Down
3 changes: 3 additions & 0 deletions src/Connection/ParametersInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
* @property string $database Database index (see the SELECT command).
* @property bool $async_connect Performs the connect() operation asynchronously.
* @property bool $tcp_nodelay Toggles the Nagle's algorithm for coalescing.
* @property bool $set_client_info Sends the CLIENT SETINFO on connect. (Default: true)
* @property string $client_name Overrides default client name to be sent over CLIENT SETINFO.
* @property string $client_version Overrides default client version to be sent over CLIENT SETINFO.
* @property bool $cache (Relay only) Whether to use in-memory caching.
* @property string $serializer (Relay only) Serializer used for data serialization.
* @property string $compression (Relay only) Algorithm used for data compression.
Expand Down
43 changes: 43 additions & 0 deletions tests/Predis/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

namespace Predis;

use InvalidArgumentException;
use Iterator;
use PHPUnit\Framework\MockObject\MockObject;
use Predis\Command\Factory as CommandFactory;
Expand Down Expand Up @@ -1268,6 +1269,48 @@ public function testSetClientInfoOnConnection(): void
$this->assertSame(Client::VERSION, $libVer);
}

/**
* @group connected
* @group relay-incompatible
* @requiresRedisVersion >= 7.2.0
*/
public function testDoNotSetClientInfoOnConnection(): void
{
$client = new Client($this->getParameters(['set_client_info' => false]));
$libName = $client->client('LIST')[0]['lib-name'];
$libVer = $client->client('LIST')[0]['lib-ver'];

$this->assertEmpty($libName);
$this->assertEmpty($libVer);
}

/**
* @group connected
* @group relay-incompatible
* @requiresRedisVersion >= 7.2.0
*/
public function testOverridesDefaultClientInfoOnConnection(): void
{
$client = new Client($this->getParameters(['client_name' => 'test', 'client_version' => '1.0.0']));
$libName = $client->client('LIST')[0]['lib-name'];
$libVer = $client->client('LIST')[0]['lib-ver'];

$this->assertSame('test', $libName);
$this->assertSame('1.0.0', $libVer);
}

/**
* @group connected
* @requiresRedisVersion >= 7.2.0
*/
public function testThrowsExceptionOnIncorrectClientVersionPattern(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Given version does not not match version pattern - xx.xx');

new Client($this->getParameters(['client_name' => 'test', 'client_version' => 'v1.0.0']));
}

// ******************************************************************** //
// ---- HELPER METHODS ------------------------------------------------ //
// ******************************************************************** //
Expand Down
68 changes: 68 additions & 0 deletions tests/Predis/Connection/FactoryTest.php 6D40
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,39 @@ public function testCreateConnectionWithInitializationCommands(): void
$prepareConnection->invoke($factory, $connection);
}

/**
* @group disconnected
*/
public function testCreateConnectionWithSkippedSetInfo(): void
{
$parameters = new Parameters([
'database' => '0',
'password' => 'foobar',
'set_client_info' => false,
]);

$connection = $this->getMockBuilder('Predis\Connection\NodeConnectionInterface')->getMock();
$connection
->expects($this->once())
->method('getParameters')
->willReturn($parameters);
$connection
->expects($this->exactly(2))
->method('addConnectCommand')
->withConsecutive(
[$this->isRedisCommand('AUTH', ['foobar'])],
[$this->isRedisCommand('SELECT', ['0'])]
);

$factory = new Factory();

// TODO: using reflection to make a protected method accessible :facepalm:
$reflection = new ReflectionObject($factory);
$prepareConnection = $reflection->getMethod('prepareConnection');
$prepareConnection->setAccessible(true);
$prepareConnection->invoke($factory, $connection);
}

/**
* @group disconnected
*/
Expand Down Expand Up @@ -579,6 +612,41 @@ public function testSetClientNameAndVersionOnConnection(): void
$this->assertSame(['SETINFO', 'LIB-VER', Client::VERSION], $initCommands[1]->getArguments());
}

/**
* @group disconnected
* @return void
*/
public function testDoNotSetClientNameAndVersionOnConnection(): void
{
$parameters = ['set_client_info' => false];

$factory = new Factory();
$connection = $factory->create($parameters);

$this->assertEmpty($connection->getInitCommands());
}

/**
* @group disconnected
* @return void
*/
public function testOverridesDefaultNameAndVersionOnConnection(): void
{
$parameters = ['client_name' => 'test', 'client_version' => '1.0.0'];

$factory = new Factory();
$connection = $factory->create($parameters);
$initCommands = $connection->getInitCommands();

$this->assertInstanceOf(RawCommand::class, $initCommands[0]);
$this->assertSame('CLIENT', $initCommands[0]->getId());
$this->assertSame(['SETINFO', 'LIB-NAME', 'test'], $initCommands[0]->getArguments());

$this->assertInstanceOf(RawCommand::class, $initCommands[1]);
$this->assertSame('CLIENT', $initCommands[1]->getId());
$this->assertSame(['SETINFO', 'LIB-VER', '1.0.0'], $initCommands[1]->getArguments());
}

// ******************************************************************** //
// ---- HELPER METHODS ------------------------------------------------ //
// ******************************************************************** //
Expand Down
21 changes: 21 additions & 0 deletions tests/Predis/Connection/ParametersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

namespace Predis\Connection;

use InvalidArgumentException;
use Predis\Client;
use PredisTestCase;

class ParametersTest extends PredisTestCase
Expand All @@ -27,6 +29,9 @@ public function testDefaultValues(): void
$this->assertEquals($defaults['scheme'], $parameters->scheme);
$this->assertEquals($defaults['host'], $parameters->host);
$this->assertEquals($defaults['port'], $parameters->port);
$this->assertEquals($defaults['set_client_info'], $parameters->set_client_info);
$this->assertEquals($defaults['client_name'], $parameters->client_name);
$this->assertEquals($defaults['client_version'], $parameters->client_version);
}

/**
Expand Down Expand Up @@ -403,6 +408,19 @@ public function testSettingRelayOptions(): void
$this->assertSame($expected, Parameters::parse($uri));
}

/**
* @group disconnected
*/
public function testThrowsExceptionOnIncorrectVersionPattern(): void
{
$parameters = ['client_version' => 'v2.0.0'];

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Given version does not not match version pattern - xx.xx');

new Parameters($parameters);
}

// ******************************************************************** //
// ---- HELPER METHODS ------------------------------------------------ //
// ******************************************************************** //
Expand All @@ -418,6 +436,9 @@ protected function getDefaultParametersArray(): array
'scheme' => 'tcp',
'host' => '127.0.0.1',
'port' => 6379,
'set_client_info' => true,
'client_name' => 'predis',
'client_version' => Client::VERSION,
];
}

Expand Down
0