From d72a1b5550153b651fe2008377b573a10df57067 Mon Sep 17 00:00:00 2001 From: Daniele Alessandri Date: Thu, 9 Jun 2016 12:10:39 +0200 Subject: [PATCH] Do not extend EVALSHA for ScriptCommand class. --- src/Client.php | 5 +- src/Command/ScriptCommand.php | 36 +++++- tests/Predis/Command/ScriptCommandTest.php | 138 ++++++++++++++------- 3 files changed, 124 insertions(+), 55 deletions(-) diff --git a/src/Client.php b/src/Client.php index 883f774e..e502726e 100644 --- a/src/Client.php +++ b/src/Client.php @@ -373,10 +373,7 @@ class Client implements ClientInterface, \IteratorAggregate protected function onErrorResponse(CommandInterface $command, ErrorResponseInterface $response) { if ($command instanceof ScriptCommand && $response->getErrorType() === 'NOSCRIPT') { - $eval = $this->createCommand('EVAL'); - $eval->setRawArguments($command->getEvalArguments()); - - $response = $this->executeCommand($eval); + $response = $this->executeCommand($command->getEvalCommand()); if (!$response instanceof ResponseInterface) { $response = $command->parseResponse($response); diff --git a/src/Command/ScriptCommand.php b/src/Command/ScriptCommand.php index 48002913..32d4625d 100644 --- a/src/Command/ScriptCommand.php +++ b/src/Command/ScriptCommand.php @@ -11,8 +11,6 @@ namespace Predis\Command; -use Predis\Command\Redis\EVALSHA; - /** * Base class used to implement an higher level abstraction for commands based * on Lua scripting with EVAL and EVALSHA. @@ -21,8 +19,16 @@ use Predis\Command\Redis\EVALSHA; * * @author Daniele Alessandri */ -abstract class ScriptCommand extends EVALSHA +abstract class ScriptCommand extends Command { + /** + * {@inheritdoc} + */ + public function getId() + { + return 'EVALSHA'; + } + /** * Gets the body of a Lua script. * @@ -30,6 +36,16 @@ abstract class ScriptCommand extends EVALSHA */ abstract public function getScript(); + /** + * Calculates the SHA1 hash of the body of the script. + * + * @return string SHA1 hash. + */ + public function getScriptHash() + { + return sha1($this->getScript()); + } + /** * Specifies the number of arguments that should be considered as keys. * @@ -63,12 +79,14 @@ abstract class ScriptCommand extends EVALSHA $numkeys = count($arguments) + $numkeys; } - $arguments = array_merge(array(sha1($this->getScript()), (int) $numkeys), $arguments); + $arguments = array_merge(array($this->getScriptHash(), (int) $numkeys), $arguments); parent::setArguments($arguments); } /** + * Returns arguments for EVAL command. + * * @return array */ public function getEvalArguments() @@ -78,4 +96,14 @@ abstract class ScriptCommand extends EVALSHA return $arguments; } + + /** + * Returns the equivalent EVAL command as a raw command instance. + * + * @return RawCommand + */ + public function getEvalCommand() + { + return new RawCommand('EVAL', $this->getEvalArguments()); + } } diff --git a/tests/Predis/Command/ScriptCommandTest.php b/tests/Predis/Command/ScriptCommandTest.php index dc6995f0..042638eb 100644 --- a/tests/Predis/Command/ScriptCommandTest.php +++ b/tests/Predis/Command/ScriptCommandTest.php @@ -24,12 +24,22 @@ class ScriptCommandTest extends PredisTestCase /** * @group disconnected */ - public function testGetArguments() + public function testGetId() + { + $command = $this->getMock('Predis\Command\ScriptCommand', array('getScript')); + + $this->assertSame('EVALSHA', $command->getId()); + } + + /** + * @group disconnected + */ + public function testGetScriptHash() { $arguments = array('key1', 'key2', 'value1', 'value2'); $command = $this->getMock('Predis\Command\ScriptCommand', array('getScript', 'getKeysCount')); - $command->expects($this->once()) + $command->expects($this->exactly(2)) ->method('getScript') ->will($this->returnValue(self::LUA_SCRIPT)); $command->expects($this->once()) @@ -37,45 +47,7 @@ class ScriptCommandTest extends PredisTestCase ->will($this->returnValue(2)); $command->setArguments($arguments); - $this->assertSame(array_merge(array(self::LUA_SCRIPT_SHA1, 2), $arguments), $command->getArguments()); - } - - /** - * @group disconnected - */ - public function testGetArgumentsWithNegativeKeysCount() - { - $arguments = array('key1', 'key2', 'value1', 'value2'); - - $command = $this->getMock('Predis\Command\ScriptCommand', array('getScript', 'getKeysCount')); - $command->expects($this->once()) - ->method('getScript') - ->will($this->returnValue(self::LUA_SCRIPT)); - $command->expects($this->once()) - ->method('getKeysCount') - ->will($this->returnValue(-2)); - $command->setArguments($arguments); - - $this->assertSame(array_merge(array(self::LUA_SCRIPT_SHA1, 2), $arguments), $command->getArguments()); - } - - /** - * @group disconnected - */ - public function testGetArgumentsWithZeroKeysCount() - { - $arguments = array('value1', 'value2', 'value3'); - - $command = $this->getMock('Predis\Command\ScriptCommand', array('getScript', 'getKeysCount')); - $command->expects($this->once()) - ->method('getScript') - ->will($this->returnValue(self::LUA_SCRIPT)); - $command->expects($this->once()) - ->method('getKeysCount') - ->will($this->returnValue(0)); - $command->setArguments($arguments); - - $this->assertSame(array_merge(array(self::LUA_SCRIPT_SHA1, 0), $arguments), $command->getArguments()); + $this->assertSame(self::LUA_SCRIPT_SHA1, $command->getScriptHash()); } /** @@ -104,13 +76,10 @@ class ScriptCommandTest extends PredisTestCase { $arguments = array('value1', 'value2', 'value3'); - $command = $this->getMock('Predis\Command\ScriptCommand', array('getScript', 'getKeysCount')); + $command = $this->getMock('Predis\Command\ScriptCommand', array('getScript')); $command->expects($this->once()) ->method('getScript') ->will($this->returnValue(self::LUA_SCRIPT)); - $command->expects($this->exactly(2)) - ->method('getKeysCount') - ->will($this->returnValue(0)); $command->setArguments($arguments); $this->assertSame(array(), $command->getKeys()); @@ -138,7 +107,7 @@ class ScriptCommandTest extends PredisTestCase /** * @group disconnected */ - public function testGetScriptHash() + public function testGetArguments() { $arguments = array('key1', 'key2', 'value1', 'value2'); @@ -151,6 +120,81 @@ class ScriptCommandTest extends PredisTestCase ->will($this->returnValue(2)); $command->setArguments($arguments); - $this->assertSame(self::LUA_SCRIPT_SHA1, $command->getScriptHash()); + $this->assertSame(array_merge(array(self::LUA_SCRIPT_SHA1, 2), $arguments), $command->getArguments()); + } + + /** + * @group disconnected + */ + public function testGetArgumentsWithZeroKeysCount() + { + $arguments = array('value1', 'value2', 'value3'); + + $command = $this->getMock('Predis\Command\ScriptCommand', array('getScript', 'getKeysCount')); + $command->expects($this->once()) + ->method('getScript') + ->will($this->returnValue(self::LUA_SCRIPT)); + $command->setArguments($arguments); + + $this->assertSame(array_merge(array(self::LUA_SCRIPT_SHA1, 0), $arguments), $command->getArguments()); + } + + /** + * @group disconnected + */ + public function testGetArgumentsWithNegativeKeysCount() + { + $arguments = array('key1', 'key2', 'value1', 'value2'); + + $command = $this->getMock('Predis\Command\ScriptCommand', array('getScript', 'getKeysCount')); + $command->expects($this->once()) + ->method('getScript') + ->will($this->returnValue(self::LUA_SCRIPT)); + $command->expects($this->once()) + ->method('getKeysCount') + ->will($this->returnValue(-2)); + $command->setArguments($arguments); + + $this->assertSame(array_merge(array(self::LUA_SCRIPT_SHA1, 2), $arguments), $command->getArguments()); + } + + /** + * @group disconnected + */ + public function testGetEvalArguments() + { + $arguments = array('key1', 'key2', 'value1', 'value2'); + + $command = $this->getMock('Predis\Command\ScriptCommand', array('getScript', 'getKeysCount')); + $command->expects($this->exactly(2)) + ->method('getScript') + ->will($this->returnValue(self::LUA_SCRIPT)); + $command->expects($this->once()) + ->method('getKeysCount') + ->will($this->returnValue(2)); + $command->setArguments($arguments); + + $this->assertSame(array_merge(array(self::LUA_SCRIPT, 2), $arguments), $command->getEvalArguments()); + } + + /** + * @group disconnected + */ + public function testGetEvalCommand() + { + $arguments = array('key1', 'key2', 'value1', 'value2'); + + $command = $this->getMock('Predis\Command\ScriptCommand', array('getScript', 'getKeysCount')); + $command->expects($this->exactly(2)) + ->method('getScript') + ->will($this->returnValue(self::LUA_SCRIPT)); + $command->expects($this->once()) + ->method('getKeysCount') + ->will($this->returnValue(2)); + $command->setArguments($arguments); + + $evalCMD = new RawCommand('EVAL', array_merge(array(self::LUA_SCRIPT, 2), $arguments)); + + $this->assertRedisCommand($evalCMD, $command->getEvalCommand()); } }