Merge remote-tracking branch 'github/pr/655' into main

Local branch v2.0-commandfactory-raw
This commit is contained in:
Daniele Alessandri
2020-09-03 12:41:00 +02:00
4 changed files with 311 additions and 14 deletions
+44
View File
@@ -0,0 +1,44 @@
<?php
/*
* This file is part of the Predis package.
*
* (c) Daniele Alessandri <suppakilla@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Command;
/**
* Command factory creating raw command instances out of command IDs.
*
* Any command ID will produce a command instance even for unknown commands that
* are not implemented by Redis (the server will return a "-ERR unknown command"
* error responses).
*
* When using this factory the client does not process arguments before sending
* commands to Redis and server responses are not further processed before being
* returned to the caller.
*
* @author Daniele Alessandri <suppakilla@gmail.com>
*/
class RawFactory implements FactoryInterface
{
/**
* {@inheritdoc}
*/
public function supports(string ...$commandIDs): bool
{
return true;
}
/**
* {@inheritdoc}
*/
public function create(string $commandID, array $arguments = []): CommandInterface
{
return new RawCommand($commandID, $arguments);
}
}
+88 -14
View File
@@ -12,6 +12,7 @@
namespace Predis\Configuration\Option;
use Predis\Command\FactoryInterface;
use Predis\Command\RawFactory;
use Predis\Command\RedisFactory;
use Predis\Configuration\OptionInterface;
use Predis\Configuration\OptionsInterface;
@@ -32,27 +33,100 @@ class Commands implements OptionInterface
$value = call_user_func($value, $options);
}
if (is_array($value)) {
$commands = $this->getDefault($options);
if ($value instanceof FactoryInterface) {
return $value;
} elseif (is_array($value)) {
return $this->createFactoryByArray($options, $value);
} elseif(is_string($value)) {
return $this->createFactoryByString($options, $value);
} else {
throw new \InvalidArgumentException(sprintf(
'%s expects a valid command factory',
static::class
));
}
}
foreach ($value as $commandID => $commandClass) {
if ($commandClass === null) {
$commands->undefine($commandID);
} else {
$commands->define($commandID, $commandClass);
}
/**
* Creates a new default command factory from a named array.
*
* The factory instance is configured according to the supplied named array
* mapping command IDs (passed as keys) to the FCQN of classes implementing
* Predis\Command\CommandInterface.
*
* @param OptionsInterface $options Client options container
* @param array $value Named array mapping command IDs to classes
*
* @return FactoryInterface
*/
protected function createFactoryByArray(OptionsInterface $options, array $value)
{
/**
* @var FactoryInterface
*/
$commands = $this->getDefault($options);
foreach ($value as $commandID => $commandClass) {
if ($commandClass === null) {
$commands->undefine($commandID);
} else {
$commands->define($commandID, $commandClass);
}
return $commands;
}
if (!$value instanceof FactoryInterface) {
$class = get_called_class();
return $commands;
}
/**
* Creates a new command factory from a descriptive string.
*
* The factory instance is configured according to the supplied descriptive
* string that identifies specific configurations of schemes and connection
* classes. Supported configuration values are:
*
* - "predis" returns the default command factory used by Predis
* - "raw" returns a command factory that creates only raw commands
* - "default" is simply an alias of "predis"
*
* @param OptionsInterface $options Client options container
* @param string $value Descriptive string identifying the desired configuration
*
* @return FactoryInterface
*/
protected function createFactoryByString(OptionsInterface $options, string $value)
{
switch (strtolower($value)) {
case 'default':
case 'predis':
return $this->getDefault($options);
throw new \InvalidArgumentException("$class expects a valid command factory");
case 'raw':
return $this->createRawFactory($options);
default:
throw new \InvalidArgumentException(sprintf(
'%s does not recognize `%s` as a supported configuration string',
static::class,
$value
));
}
}
/**
* Creates a new raw command factory instance.
*
* @param OptionsInterface $options Client options container
*/
protected function createRawFactory(OptionsInterface $options): FactoryInterface
{
$commands = new RawFactory();
if (isset($options->prefix)) {
throw new \InvalidArgumentException(sprintf(
'%s does not support key prefixing', RawFactory::class
));
}
return $value;
return $commands;
}
/**
+94
View File
@@ -0,0 +1,94 @@
<?php
/*
* This file is part of the Predis package.
*
* (c) Daniele Alessandri <suppakilla@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Command;
use PredisTestCase;
/**
*
*/
class RawFactoryTest extends PredisTestCase
{
/**
* @group disconnected
*/
public function testSupportForAnyCommand(): void
{
$factory = new RawFactory();
$this->assertTrue($factory->supports('info'));
$this->assertTrue($factory->supports('INFO'));
$this->assertTrue($factory->supports('unknown'));
$this->assertTrue($factory->supports('UNKNOWN'));
}
/**
* @group disconnected
*/
public function testSupportForAnyCommands(): void
{
$factory = new RawFactory();
$this->assertTrue($factory->supports('get', 'set'));
$this->assertTrue($factory->supports('GET', 'SET'));
$this->assertTrue($factory->supports('get', 'unknown'));
$this->assertTrue($factory->supports('unknown1', 'unknown2'));
}
/**
* @group disconnected
*/
public function testCreateInstanceOfRawCommand(): void
{
$factory = new RawFactory();
$command = $factory->create('info');
$this->assertInstanceOf('Predis\Command\CommandInterface', $command);
$this->assertInstanceOf('Predis\Command\RawCommand', $command);
$command = $factory->create('unknown');
$this->assertInstanceOf('Predis\Command\CommandInterface', $command);
$this->assertInstanceOf('Predis\Command\RawCommand', $command);
}
/**
* @group disconnected
*/
public function testCreateCommandWithoutArguments(): void
{
$factory = new RawFactory();
$command = $factory->create('info');
$this->assertInstanceOf('Predis\Command\RawCommand', $command);
$this->assertEquals('INFO', $command->getId());
$this->assertEquals(array(), $command->getArguments());
}
/**
* @group disconnected
*/
public function testCreateCommandWithArguments(): void
{
$factory = new RawFactory();
$arguments = array('foo', 'bar');
$command = $factory->create('set', $arguments);
$this->assertInstanceOf('Predis\Command\RawCommand', $command);
$this->assertEquals('SET', $command->getId());
$this->assertEquals($arguments, $command->getArguments());
}
}
@@ -185,6 +185,70 @@ class CommandsTest extends PredisTestCase
$this->assertSame('Predis\Command\RawCommand', $commands->getCommandClass('BAR'));
}
/**
* @group disconnected
*/
public function testAcceptsStringPredisAsValue(): void
{
$option = new Commands();
/** @var OptionsInterface */
$options = $this->getMockBuilder('Predis\Configuration\OptionsInterface')->getMock();
$commands = $option->filter($options, 'predis');
$this->assertInstanceOf('Predis\Command\FactoryInterface', $commands);
$this->assertInstanceOf('Predis\Command\RedisFactory', $commands);
}
/**
* @group disconnected
*/
public function testAcceptsStringRawAsValue(): void
{
$option = new Commands();
/** @var OptionsInterface */
$options = $this->getMockBuilder('Predis\Configuration\OptionsInterface')->getMock();
$commands = $option->filter($options, 'raw');
$this->assertInstanceOf('Predis\Command\FactoryInterface', $commands);
$this->assertInstanceOf('Predis\Command\RawFactory', $commands);
}
/**
* @group disconnected
*/
public function testAcceptsStringDefaultAsValue(): void
{
$option = new Commands();
/** @var OptionsInterface */
$options = $this->getMockBuilder('Predis\Configuration\OptionsInterface')->getMock();
$commands = $option->filter($options, 'default');
$this->assertInstanceOf('Predis\Command\FactoryInterface', $commands);
$this->assertInstanceOf('Predis\Command\RedisFactory', $commands);
}
/**
* @group disconnected
*/
public function testThrowsExceptionOnInvalidStringAsValue(): void
{
$this->expectException('InvalidArgumentException');
$this->expectExceptionMessage('Predis\Configuration\Option\Commands does not recognize `unknown` as a supported configuration string');
$option = new Commands();
/** @var OptionsInterface */
$options = $this->getMockBuilder('Predis\Configuration\OptionsInterface')->getMock();
$option->filter($options, 'unknown');
}
/**
* @group disconnected
*/
@@ -227,4 +291,25 @@ class CommandsTest extends PredisTestCase
$option->filter($options, new \stdClass());
}
/**
* @group disconnected
*/
public function testThrowsExceptionOnPrefixWithRawFactory(): void
{
$this->expectException('InvalidArgumentException');
$this->expectExceptionMessage('Predis\Command\RawFactory does not support key prefixing');
$option = new Commands();
/** @var OptionsInterface|MockObject */
$options = $this->getMockBuilder('Predis\Configuration\OptionsInterface')->getMock();
$options
->expects($this->once())
->method('__isset')
->with('prefix')
->willReturn(true);
$option->filter($options, 'raw');
}
}