Compare commits

...

34 Commits

Author SHA1 Message Date
Daniele Alessandri 7db5e650ef Bump version and... release! 2010-11-28 20:23:39 +01:00
Daniele Alessandri 975bd98933 Update CHANGELOG. 2010-11-28 19:22:26 +01:00
Daniele Alessandri 7d5cccaf7c Overridden methods for UNWATCH and DISCARD in Predis\MultiExecBlock must return $this to not break method chaining. 2010-11-28 19:19:48 +01:00
Daniele Alessandri 08b1761e10 Fix a bunch of broken tests. 2010-11-28 17:21:46 +01:00
Daniele Alessandri 24f49abc14 Update CHANGELOG. 2010-11-28 16:54:02 +01:00
Daniele Alessandri b530f9f613 New command: PERSIST (Redis v2.2-dev). 2010-11-28 16:49:20 +01:00
Daniele Alessandri d677c72ae3 New command: ZREVRANGEBYSCORE (Redis v2.2-dev). 2010-11-28 16:39:07 +01:00
Daniele Alessandri c3cf2acb51 Update CHANGELOG. 2010-09-25 17:54:52 +02:00
Daniele Alessandri 8bfae69bf0 Make WATCH accept an array of keys that must be watched. 2010-09-25 17:52:23 +02:00
Daniele Alessandri 92d6ad62ce Test suite: add test for WATCH in Predis\MultiExecBlock. 2010-09-25 17:08:30 +02:00
Daniele Alessandri fa4d654d38 Update CHANGELOG. 2010-09-25 17:04:29 +02:00
Daniele Alessandri 9ac4571f91 Fix improper handling of a few corner cases for Predis\MultiExecBlock and send DISCARD to the server on uncaught exceptions inside a callable block. 2010-09-25 16:55:31 +02:00
Daniele Alessandri fea5e1c602 Test suite: add missing tests for Predis\MultiExecBlock. 2010-09-25 16:53:21 +02:00
Daniele Alessandri ec51b14fbb Fix an infinite recursion loop when calling Predis\Client::multiExec() (ISSUE #14). 2010-09-25 16:52:59 +02:00
Daniele Alessandri dc56526256 New commands: RPUSHX, LPUSHX (Redis v2.2-dev). 2010-09-25 16:38:16 +02:00
Daniele Alessandri 195c6eae99 New command: LINSERT (Redis v2.2-dev). 2010-09-25 16:34:18 +02:00
Daniele Alessandri 9ae47c865e New command: STRLEN (Redis v2.2-dev). 2010-09-25 16:31:35 +02:00
Daniele Alessandri d3ba25436c Slight micro-optimization. 2010-09-25 16:26:55 +02:00
Daniele Alessandri 580cf80436 Get the connection out of a cluster only once inside Predis\Client::getClientFor(). 2010-09-18 18:56:39 +02:00
Daniele Alessandri d6bea4390e Perform (p)unsubscribtion only if (p)subscribed to a channel when the destructor for Predis\PubSubContext is invoked. 2010-09-17 22:24:59 +02:00
Daniele Alessandri fd7f4a7b48 Make arguments handling more solid for the SORT command. 2010-09-17 22:11:37 +02:00
Daniele Alessandri 92f7e8bf9b Remove useless code duplication. 2010-09-17 21:56:31 +02:00
Daniele Alessandri 006b44e4bf Fix checks on the value for the STORE argument (ISSUE #13). 2010-09-17 11:36:47 +02:00
Daniele Alessandri bd961a767b Update CHANGELOG. It's release time! 2010-07-11 17:12:28 +02:00
Daniele Alessandri b3599a8e6b Update CHANGELOG. 2010-07-11 17:11:58 +02:00
Daniele Alessandri 0b46de424e Test suite: add a test for the LIMIT modifier of ZRANGEBYSCORE configured via associative array. 2010-07-11 17:02:33 +02:00
Daniele Alessandri 73bdfc2d72 Fix handling of the WEIGHT modifier for ZUNIONSTORE and ZINTERSTORE with more than two weights specified. 2010-07-11 16:35:03 +02:00
Daniele Alessandri 3077e76708 Update CHANGELOG. 2010-07-11 16:24:06 +02:00
Daniele Alessandri 208fdf6daf Update CHANGELOG. 2010-07-11 15:32:14 +02:00
Daniele Alessandri 7d7995ff8f Update CHANGELOG. 2010-07-07 20:07:15 +02:00
Daniele Alessandri 418197af75 Add tests for the new options handling for the ZRANGE commands family. 2010-07-07 19:59:16 +02:00
Daniele Alessandri 90cb9d437d Add support for the LIMIT modifier in ZRANGEBYSCORE. 2010-07-07 19:58:52 +02:00
Daniele Alessandri 4668bdca83 ZRANGE, ZREVRANGE and ZRANGEBYSCORE accepts an array instance for optional modifiers. 2010-07-07 19:58:41 +02:00
Daniele Alessandri 93a3c184ed Bump version number. 2010-07-02 22:31:39 +02:00
6 changed files with 529 additions and 105 deletions
+45 -12
View File
@@ -1,13 +1,43 @@
v0.6.1 (2010-xx-xx) v0.6.2 (2010-11-28)
* Minor internal improvements and clean ups.
* New commands available in the Redis v2.2 profile (dev):
- Strings: STRLEN
- Lists : LINSERT, RPUSHX, LPUSHX
- ZSets : ZREVRANGEBYSCORE
- Misc. : PERSIST
* WATCH also accepts a single array parameter with the keys that should be
monitored during a transaction.
* Improved the behaviour of Predis\MultiExecBlock in certain corner cases.
* Improved parameters checking for the SORT command.
* FIX: the STORE parameter for the SORT command didn't work correctly when
using '0' as the target key (ISSUE #13).
* FIX: the methods for UNWATCH and DISCARD do not break anymore method
chaining with Predis\MultiExecBlock.
v0.6.1 (2010-07-11)
* Minor internal improvements and clean ups.
* New commands available in the Redis v2.2 profile (dev): * New commands available in the Redis v2.2 profile (dev):
- Misc. : WATCH, UNWATCH - Misc. : WATCH, UNWATCH
* Minor internal improvements and clean ups. * Optional modifiers for ZRANGE, ZREVRANGE and ZRANGEBYSCORE queries are
supported using an associative array passed as the last argument of their
respective methods.
* The constructor of Predis\Client::__construct now accepts also instances * The LIMIT modifier for ZRANGEBYSCORE can be specified using either:
of Predis\ConnectionParameters. - an indexed array: array($offset, $count)
- an associative array: array('offset' => $offset, 'count' => $count)
* Predis\MultiExecBlock and Predis\PubSubContext will throw an exception * The method Predis\Client::__construct() now accepts also instances of
Predis\ConnectionParameters.
* Predis\MultiExecBlock and Predis\PubSubContext now throw an exception
when trying to create their instances using a profile that does not when trying to create their instances using a profile that does not
support the required Redis commands or when the client is connected to support the required Redis commands or when the client is connected to
a cluster of connections. a cluster of connections.
@@ -17,14 +47,17 @@ v0.6.1 (2010-xx-xx)
- support for WATCH and UNWATCH when using the current development - support for WATCH and UNWATCH when using the current development
profile (Redis v2.2) and aborted transactions. profile (Redis v2.2) and aborted transactions.
* New method signature for Predis\Client::multiExec(). Now it is able to * New signature for Predis\Client::multiExec() which is now able to accept
accept an array of options for the underlying Predis\MultiExecBlock, but an array of options for the underlying instance of Predis\MultiExecBlock.
it is still backwards compatible with previous releases of Predis. Backwards compatibility with previous releases of Predis is ensured.
* New method signature for Predis\Client::pipeline(). Now it is able to * New signature for Predis\Client::pipeline() which is now able to accept
accept an array of options for the underlying Predis\CommandPipeline, an array of options for the underlying instance of Predis\CommandPipeline.
but it is still backwards compatible with previous releases of Predis. Backwards compatibility with previous releases of Predis is ensured.
Predis\Client::pipelineSafe() is to be considered obsolete. The method Predis\Client::pipelineSafe() is to be considered deprecated.
* FIX: The WEIGHT modifier for ZUNIONSTORE and ZINTERSTORE was handled
incorrectly with more than two weights specified.
v0.6.0 (2010-05-24) v0.6.0 (2010-05-24)
* Switched to the new multi-bulk request protocol for all of the commands * Switched to the new multi-bulk request protocol for all of the commands
+1 -1
View File
@@ -1 +1 @@
0.6.0 0.6.2
+162 -86
View File
@@ -170,7 +170,7 @@ class Client {
$newClient = new Client(); $newClient = new Client();
$newClient->setupClient($this->_options); $newClient->setupClient($this->_options);
$newClient->setConnection($this->getConnection($connectionAlias)); $newClient->setConnection($connection);
return $newClient; return $newClient;
} }
@@ -230,21 +230,24 @@ class Client {
return $this->_connection->rawCommand($rawCommandData, $closesConnection); return $this->_connection->rawCommand($rawCommandData, $closesConnection);
} }
public function pipeline(/* arguments */) { private function sharedInitializer($argv, $initializer) {
$argv = func_get_args(); $argc = count($argv);
$argc = func_num_args();
if ($argc === 0) { if ($argc === 0) {
return $this->initPipeline(); return $this->$initializer();
} }
else if ($argc === 1) { else if ($argc === 1) {
list($arg0) = $argv; list($arg0) = $argv;
return is_array($arg0) ? $this->initPipeline($arg0) : $this->initPipeline(null, $arg0); return is_array($arg0) ? $this->$initializer($arg0) : $this->$initializer(null, $arg0);
} }
else if ($argc === 2) { else if ($argc === 2) {
list($arg0, $arg1) = $argv; list($arg0, $arg1) = $argv;
return $this->initPipeline($arg0, $arg1); return $this->$initializer($arg0, $arg1);
} }
return $this->$initializer($this, $arguments);
}
public function pipeline(/* arguments */) {
return $this->sharedInitializer(func_get_args(), 'initPipeline');
} }
public function pipelineSafe($pipelineBlock = null) { public function pipelineSafe($pipelineBlock = null) {
@@ -276,20 +279,7 @@ class Client {
} }
public function multiExec(/* arguments */) { public function multiExec(/* arguments */) {
$argv = func_get_args(); return $this->sharedInitializer(func_get_args(), 'initMultiExec');
$argc = func_num_args();
if ($argc === 0) {
return $this->initMultiExec();
}
else if ($argc === 1) {
list($arg0) = $argv;
return is_array($arg0) ? $this->initMultiExec($arg0) : $this->initMultiExec(null, $arg0);
}
else if ($argc === 2) {
list($arg0, $arg1) = $argv;
return $this->initMultiExec($arg0, $arg1);
}
} }
private function initMultiExec(Array $options = null, $transBlock = null) { private function initMultiExec(Array $options = null, $transBlock = null) {
@@ -470,12 +460,12 @@ abstract class Command {
public function setArguments(/* arguments */) { public function setArguments(/* arguments */) {
$this->_arguments = $this->filterArguments(func_get_args()); $this->_arguments = $this->filterArguments(func_get_args());
$this->_hash = null; unset($this->_hash);
} }
public function setArgumentsArray(Array $arguments) { public function setArgumentsArray(Array $arguments) {
$this->_arguments = $this->filterArguments($arguments); $this->_arguments = $this->filterArguments($arguments);
$this->_hash = null; unset($this->_hash);
} }
public function getArguments() { public function getArguments() {
@@ -906,6 +896,7 @@ class MultiExecBlock {
public function unwatch() { public function unwatch() {
$this->isWatchSupported(); $this->isWatchSupported();
$this->_redisClient->unwatch(); $this->_redisClient->unwatch();
return $this;
} }
public function discard() { public function discard() {
@@ -913,6 +904,7 @@ class MultiExecBlock {
$this->_commands = array(); $this->_commands = array();
$this->_initialized = false; $this->_initialized = false;
$this->_discarded = true; $this->_discarded = true;
return $this;
} }
public function exec() { public function exec() {
@@ -933,45 +925,52 @@ class MultiExecBlock {
$blockException = null; $blockException = null;
$returnValues = array(); $returnValues = array();
try { if ($block !== null) {
if ($block !== null) { $this->setInsideBlock(true);
$this->setInsideBlock(true); try {
$block($this); $block($this);
$this->setInsideBlock(false);
} }
catch (CommunicationException $exception) {
if ($this->_discarded === true) { $blockException = $exception;
return;
} }
catch (ServerException $exception) {
$reply = $this->_redisClient->exec(); $blockException = $exception;
if ($reply === null) {
throw new AbortedMultiExec('The current transaction has been aborted by the server');
} }
catch (\Exception $exception) {
$execReply = $reply instanceof \Iterator ? iterator_to_array($reply) : $reply; $blockException = $exception;
$commands = &$this->_commands; if ($this->_initialized === true) {
$sizeofReplies = count($execReply); $this->discard();
}
if ($sizeofReplies !== count($commands)) {
$this->malformedServerResponse('Unexpected number of responses for a MultiExecBlock');
} }
for ($i = 0; $i < $sizeofReplies; $i++) {
$returnValues[] = $commands[$i]->parseResponse($execReply[$i] instanceof \Iterator
? iterator_to_array($execReply[$i])
: $execReply[$i]
);
unset($commands[$i]);
}
}
catch (\Exception $exception) {
$this->setInsideBlock(false); $this->setInsideBlock(false);
$blockException = $exception; if ($blockException !== null) {
throw $blockException;
}
} }
if ($blockException !== null) { if ($this->_initialized === false) {
throw $blockException; return;
}
$reply = $this->_redisClient->exec();
if ($reply === null) {
throw new AbortedMultiExec('The current transaction has been aborted by the server');
}
$execReply = $reply instanceof \Iterator ? iterator_to_array($reply) : $reply;
$commands = &$this->_commands;
$sizeofReplies = count($execReply);
if ($sizeofReplies !== count($commands)) {
$this->malformedServerResponse('Unexpected number of responses for a MultiExecBlock');
}
for ($i = 0; $i < $sizeofReplies; $i++) {
$returnValues[] = $commands[$i]->parseResponse($execReply[$i] instanceof \Iterator
? iterator_to_array($execReply[$i])
: $execReply[$i]
);
unset($commands[$i]);
} }
return $returnValues; return $returnValues;
@@ -1008,10 +1007,7 @@ class PubSubContext implements \Iterator {
} }
public function __destruct() { public function __destruct() {
if ($this->valid()) { $this->closeContext();
$this->_redisClient->unsubscribe();
$this->_redisClient->punsubscribe();
}
} }
private function checkCapabilities(Client $redisClient) { private function checkCapabilities(Client $redisClient) {
@@ -1824,6 +1820,20 @@ class RedisServer_vNext extends RedisServer_v2_0 {
/* transactions */ /* transactions */
'watch' => '\Predis\Commands\Watch', 'watch' => '\Predis\Commands\Watch',
'unwatch' => '\Predis\Commands\Unwatch', 'unwatch' => '\Predis\Commands\Unwatch',
/* commands operating on string values */
'strlen' => '\Predis\Commands\Strlen',
/* commands operating on the key space */
'persist' => '\Predis\Commands\Persist',
/* commands operating on lists */
'rpushx' => '\Predis\Commands\ListPushTailX',
'lpushx' => '\Predis\Commands\ListPushHeadX',
'linsert' => '\Predis\Commands\ListInsert',
/* commands operating on sorted sets */
'zrevrangebyscore' => '\Predis\Commands\ZSetReverseRangeByScore',
)); ));
} }
} }
@@ -2351,6 +2361,10 @@ class Substr extends \Predis\MultiBulkCommand {
public function getCommandId() { return 'SUBSTR'; } public function getCommandId() { return 'SUBSTR'; }
} }
class Strlen extends \Predis\MultiBulkCommand {
public function getCommandId() { return 'STRLEN'; }
}
/* commands operating on the key space */ /* commands operating on the key space */
class Keys extends \Predis\MultiBulkCommand { class Keys extends \Predis\MultiBulkCommand {
public function canBeHashed() { return false; } public function canBeHashed() { return false; }
@@ -2391,6 +2405,11 @@ class ExpireAt extends \Predis\MultiBulkCommand {
public function parseResponse($data) { return (bool) $data; } public function parseResponse($data) { return (bool) $data; }
} }
class Persist extends \Predis\MultiBulkCommand {
public function getCommandId() { return 'PERSIST'; }
public function parseResponse($data) { return (bool) $data; }
}
class DatabaseSize extends \Predis\MultiBulkCommand { class DatabaseSize extends \Predis\MultiBulkCommand {
public function canBeHashed() { return false; } public function canBeHashed() { return false; }
public function getCommandId() { return 'DBSIZE'; } public function getCommandId() { return 'DBSIZE'; }
@@ -2405,10 +2424,18 @@ class ListPushTail extends \Predis\MultiBulkCommand {
public function getCommandId() { return 'RPUSH'; } public function getCommandId() { return 'RPUSH'; }
} }
class ListPushTailX extends \Predis\MultiBulkCommand {
public function getCommandId() { return 'RPUSHX'; }
}
class ListPushHead extends \Predis\MultiBulkCommand { class ListPushHead extends \Predis\MultiBulkCommand {
public function getCommandId() { return 'LPUSH'; } public function getCommandId() { return 'LPUSH'; }
} }
class ListPushHeadX extends \Predis\MultiBulkCommand {
public function getCommandId() { return 'LPUSHX'; }
}
class ListLength extends \Predis\MultiBulkCommand { class ListLength extends \Predis\MultiBulkCommand {
public function getCommandId() { return 'LLEN'; } public function getCommandId() { return 'LLEN'; }
} }
@@ -2457,6 +2484,10 @@ class ListPopLastBlocking extends \Predis\MultiBulkCommand {
public function getCommandId() { return 'BRPOP'; } public function getCommandId() { return 'BRPOP'; }
} }
class ListInsert extends \Predis\MultiBulkCommand {
public function getCommandId() { return 'LINSERT'; }
}
/* commands operating on sets */ /* commands operating on sets */
class SetAdd extends \Predis\MultiBulkCommand { class SetAdd extends \Predis\MultiBulkCommand {
public function getCommandId() { return 'SADD'; } public function getCommandId() { return 'SADD'; }
@@ -2550,8 +2581,9 @@ class ZSetUnionStore extends \Predis\MultiBulkCommand {
$finalizedOpts = array(); $finalizedOpts = array();
if (isset($opts['WEIGHTS']) && is_array($opts['WEIGHTS'])) { if (isset($opts['WEIGHTS']) && is_array($opts['WEIGHTS'])) {
$finalizedOpts[] = 'WEIGHTS'; $finalizedOpts[] = 'WEIGHTS';
$finalizedOpts[] = $opts['WEIGHTS'][0]; foreach ($opts['WEIGHTS'] as $weight) {
$finalizedOpts[] = $opts['WEIGHTS'][1]; $finalizedOpts[] = $weight;
}
} }
if (isset($opts['AGGREGATE'])) { if (isset($opts['AGGREGATE'])) {
$finalizedOpts[] = 'AGGREGATE'; $finalizedOpts[] = 'AGGREGATE';
@@ -2566,20 +2598,42 @@ class ZSetIntersectionStore extends \Predis\Commands\ZSetUnionStore {
} }
class ZSetRange extends \Predis\MultiBulkCommand { class ZSetRange extends \Predis\MultiBulkCommand {
private $_withScores = false;
public function getCommandId() { return 'ZRANGE'; } public function getCommandId() { return 'ZRANGE'; }
public function parseResponse($data) { public function filterArguments(Array $arguments) {
$arguments = $this->getArguments();
if (count($arguments) === 4) { if (count($arguments) === 4) {
if (strtolower($arguments[3]) === 'withscores') { $lastType = gettype($arguments[3]);
if ($data instanceof \Iterator) { if ($lastType === 'string' && strtolower($arguments[3]) === 'withscores') {
return new \Predis\Shared\MultiBulkResponseKVIterator($data); // used for compatibility with older versions
} $arguments[3] = array('WITHSCORES' => true);
$result = array(); $lastType = 'array';
for ($i = 0; $i < count($data); $i++) {
$result[] = array($data[$i], $data[++$i]);
}
return $result;
} }
if ($lastType === 'array') {
$options = $this->prepareOptions(array_pop($arguments));
return array_merge($arguments, $options);
}
}
return $arguments;
}
protected function prepareOptions($options) {
$opts = array_change_key_case($options, CASE_UPPER);
$finalizedOpts = array();
if (isset($opts['WITHSCORES'])) {
$finalizedOpts[] = 'WITHSCORES';
$this->_withScores = true;
}
return $finalizedOpts;
}
public function parseResponse($data) {
if ($this->_withScores) {
if ($data instanceof \Iterator) {
return new \Predis\Shared\MultiBulkResponseKVIterator($data);
}
$result = array();
for ($i = 0; $i < count($data); $i++) {
$result[] = array($data[$i], $data[++$i]);
}
return $result;
} }
return $data; return $data;
} }
@@ -2591,6 +2645,21 @@ class ZSetReverseRange extends \Predis\Commands\ZSetRange {
class ZSetRangeByScore extends \Predis\Commands\ZSetRange { class ZSetRangeByScore extends \Predis\Commands\ZSetRange {
public function getCommandId() { return 'ZRANGEBYSCORE'; } public function getCommandId() { return 'ZRANGEBYSCORE'; }
protected function prepareOptions($options) {
$opts = array_change_key_case($options, CASE_UPPER);
$finalizedOpts = array();
if (isset($opts['LIMIT']) && is_array($opts['LIMIT'])) {
$limit = array_change_key_case($opts['LIMIT'], CASE_UPPER);
$finalizedOpts[] = 'LIMIT';
$finalizedOpts[] = isset($limit['OFFSET']) ? $limit['OFFSET'] : $limit[0];
$finalizedOpts[] = isset($limit['COUNT']) ? $limit['COUNT'] : $limit[1];
}
return array_merge($finalizedOpts, parent::prepareOptions($options));
}
}
class ZSetReverseRangeByScore extends \Predis\Commands\ZSetRangeByScore {
public function getCommandId() { return 'ZREVRANGEBYSCORE'; }
} }
class ZSetCount extends \Predis\MultiBulkCommand { class ZSetCount extends \Predis\MultiBulkCommand {
@@ -2737,16 +2806,15 @@ class Sort extends \Predis\MultiBulkCommand {
return $arguments; return $arguments;
} }
// TODO: add more parameters checks
$query = array($arguments[0]); $query = array($arguments[0]);
$sortParams = $arguments[1]; $sortParams = array_change_key_case($arguments[1], CASE_UPPER);
if (isset($sortParams['by'])) { if (isset($sortParams['BY'])) {
$query[] = 'BY'; $query[] = 'BY';
$query[] = $sortParams['by']; $query[] = $sortParams['BY'];
} }
if (isset($sortParams['get'])) { if (isset($sortParams['GET'])) {
$getargs = $sortParams['get']; $getargs = $sortParams['GET'];
if (is_array($getargs)) { if (is_array($getargs)) {
foreach ($getargs as $getarg) { foreach ($getargs as $getarg) {
$query[] = 'GET'; $query[] = 'GET';
@@ -2758,20 +2826,22 @@ class Sort extends \Predis\MultiBulkCommand {
$query[] = $getargs; $query[] = $getargs;
} }
} }
if (isset($sortParams['limit']) && is_array($sortParams['limit'])) { if (isset($sortParams['LIMIT']) && is_array($sortParams['LIMIT'])
&& count($sortParams['LIMIT']) == 2) {
$query[] = 'LIMIT'; $query[] = 'LIMIT';
$query[] = $sortParams['limit'][0]; $query[] = $sortParams['LIMIT'][0];
$query[] = $sortParams['limit'][1]; $query[] = $sortParams['LIMIT'][1];
} }
if (isset($sortParams['sort'])) { if (isset($sortParams['SORT'])) {
$query[] = strtoupper($sortParams['sort']); $query[] = strtoupper($sortParams['SORT']);
} }
if (isset($sortParams['alpha']) && $sortParams['alpha'] == true) { if (isset($sortParams['ALPHA']) && $sortParams['ALPHA'] == true) {
$query[] = 'ALPHA'; $query[] = 'ALPHA';
} }
if (isset($sortParams['store']) && $sortParams['store'] == true) { if (isset($sortParams['STORE'])) {
$query[] = 'STORE'; $query[] = 'STORE';
$query[] = $sortParams['store']; $query[] = $sortParams['STORE'];
} }
return $query; return $query;
@@ -2797,6 +2867,12 @@ class Discard extends \Predis\MultiBulkCommand {
class Watch extends \Predis\MultiBulkCommand { class Watch extends \Predis\MultiBulkCommand {
public function canBeHashed() { return false; } public function canBeHashed() { return false; }
public function getCommandId() { return 'WATCH'; } public function getCommandId() { return 'WATCH'; }
public function filterArguments(Array $arguments) {
if (isset($arguments[0]) && is_array($arguments[0])) {
return $arguments[0];
}
return $arguments;
}
public function parseResponse($data) { return (bool) $data; } public function parseResponse($data) { return (bool) $data; }
} }
+143 -5
View File
@@ -261,16 +261,19 @@ class PredisClientFeaturesTestSuite extends PHPUnit_Framework_TestCase {
function testConnection_WriteCommandAndCloseConnection() { function testConnection_WriteCommandAndCloseConnection() {
$cmd = \Predis\RedisServerProfile::getDefault()->createCommand('quit'); $cmd = \Predis\RedisServerProfile::getDefault()->createCommand('quit');
$connection = new \Predis\Connection(RC::getConnectionParameters()); $connection = new \Predis\Connection(new \Predis\ConnectionParameters(
$connection->connect(); RC::getConnectionArguments() + array('read_write_timeout' => 0.5)
));
$connection->connect();
$this->assertTrue($connection->isConnected()); $this->assertTrue($connection->isConnected());
$connection->writeCommand($cmd); $connection->writeCommand($cmd);
$connection->disconnect();
$exceptionMessage = 'Error while reading line from the server'; $exceptionMessage = 'Error while reading line from the server';
RC::testForCommunicationException($this, $exceptionMessage, function() use($connection, $cmd) { RC::testForCommunicationException($this, $exceptionMessage, function() use($connection, $cmd) {
$connection->readResponse($cmd); $connection->readResponse($cmd);
}); });
//$this->assertFalse($connection->isConnected());
} }
function testConnection_GetSocketOpensConnection() { function testConnection_GetSocketOpensConnection() {
@@ -354,8 +357,8 @@ class PredisClientFeaturesTestSuite extends PHPUnit_Framework_TestCase {
function testResponseReader_OptionExceptionOnError() { function testResponseReader_OptionExceptionOnError() {
$connection = new \Predis\Connection(RC::getConnectionParameters()); $connection = new \Predis\Connection(RC::getConnectionParameters());
$responseReader = $connection->getResponseReader(); $responseReader = $connection->getResponseReader();
$connection->rawCommand("SET key 5\r\nvalue\r\n"); $connection->rawCommand("*3\r\n$3\r\nSET\r\n$3\r\nkey\r\n$5\r\nvalue\r\n");
$rawCmdUnexpected = "LPUSH key 5\r\nvalue\r\n"; $rawCmdUnexpected = "*3\r\n$5\r\nLPUSH\r\n$3\r\nkey\r\n$5\r\nvalue\r\n";
$responseReader->setHandler( $responseReader->setHandler(
\Predis\Protocol::PREFIX_ERROR, \Predis\Protocol::PREFIX_ERROR,
@@ -471,5 +474,140 @@ class PredisClientFeaturesTestSuite extends PHPUnit_Framework_TestCase {
$this->assertEquals('bar', $replies[3][0]); $this->assertEquals('bar', $replies[3][0]);
$this->assertEquals('piyo', $replies[3][1]); $this->assertEquals('piyo', $replies[3][1]);
} }
/* Client + MultiExecBlock */
function testMultiExecBlock_Simple() {
$client = RC::getConnection();
$client->flushdb();
$multi = $client->multiExec();
$this->assertType('\Predis\MultiExecBlock', $multi);
$this->assertType('\Predis\MultiExecBlock', $multi->set('foo', 'bar'));
$this->assertType('\Predis\MultiExecBlock', $multi->set('hoge', 'piyo'));
$this->assertType('\Predis\MultiExecBlock', $multi->mset(array(
'foofoo' => 'barbar', 'hogehoge' => 'piyopiyo'
)));
$this->assertType('\Predis\MultiExecBlock', $multi->mget(array(
'foo', 'hoge', 'foofoo', 'hogehoge'
)));
$replies = $multi->execute();
$this->assertType('array', $replies);
$this->assertEquals(4, count($replies));
$this->assertEquals(4, count($replies[3]));
$this->assertEquals('barbar', $replies[3][2]);
}
function testMultiExecBlock_FluentInterface() {
$client = RC::getConnection();
$client->flushdb();
$replies = $client->multiExec()->ping()->set('foo', 'bar')->get('foo')->execute();
$this->assertType('array', $replies);
$this->assertEquals('bar', $replies[2]);
}
function testMultiExecBlock_CallableAnonymousBlock() {
$client = RC::getConnection();
$client->flushdb();
$replies = $client->multiExec(function($multi) {
$multi->ping();
$multi->set('foo', 'bar');
$multi->get('foo');
});
$this->assertType('array', $replies);
$this->assertEquals('bar', $replies[2]);
}
function testMultiExecBlock_EmptyCallableBlock() {
$client = RC::getConnection();
$client->flushdb();
$replies = $client->multiExec(function($multi) { });
$this->assertEquals(0, count($replies));
}
function testMultiExecBlock_ClientExceptionInCallableBlock() {
$client = RC::getConnection();
$client->flushdb();
RC::testForClientException($this, 'TEST', function() use($client) {
$client->multiExec(function($multi) {
$multi->ping();
$multi->set('foo', 'bar');
throw new \Predis\ClientException("TEST");
});
});
$this->assertFalse($client->exists('foo'));
}
function testMultiExecBlock_ServerExceptionInCallableBlock() {
$client = RC::getConnection();
$client->flushdb();
$client->getResponseReader()->setHandler('-', new \Predis\ResponseErrorSilentHandler());
$replies = $client->multiExec(function($multi) {
$multi->set('foo', 'bar');
$multi->lpush('foo', 'piyo'); // LIST operation on STRING type returns an ERROR
$multi->set('hoge', 'piyo');
});
$this->assertType('array', $replies);
$this->assertType('\Predis\ResponseError', $replies[1]);
$this->assertTrue($client->exists('foo'));
$this->assertTrue($client->exists('hoge'));
}
function testMultiExecBlock_Discard() {
$client = RC::getConnection();
$client->flushdb();
$replies = $client->multiExec(function($multi) {
$multi->set('foo', 'bar');
$multi->discard();
$multi->set('hoge', 'piyo');
});
$this->assertEquals(1, count($replies));
$this->assertFalse($client->exists('foo'));
$this->assertTrue($client->exists('hoge'));
}
function testMultiExecBlock_DiscardEmpty() {
$client = RC::getConnection();
$client->flushdb();
$replies = $client->multiExec(function($multi) {
$multi->discard();
});
$this->assertEquals(0, count($replies));
}
function testMultiExecBlock_Watch() {
$client1 = RC::getConnection();
$client2 = RC::getConnection(true);
$client1->flushdb();
RC::testForAbortedMultiExecException($this, function()
use($client1, $client2) {
$client1->multiExec(array('watch' => 'sentinel'), function($multi)
use ($client2) {
$multi->set('sentinel', 'client1');
$multi->get('sentinel');
$client2->set('sentinel', 'client2');
});
});
$this->assertEquals('client2', $client1->get('sentinel'));
}
} }
?> ?>
+15 -1
View File
@@ -44,7 +44,10 @@ class RC {
return $connection; return $connection;
} }
public static function getConnection() { public static function getConnection($new = false) {
if ($new == true) {
return self::createConnection();
}
if (self::$_connection === null || !self::$_connection->isConnected()) { if (self::$_connection === null || !self::$_connection->isConnected()) {
self::$_connection = self::createConnection(); self::$_connection = self::createConnection();
} }
@@ -149,6 +152,17 @@ class RC {
} }
} }
public static function testForAbortedMultiExecException($testcaseInstance, $wrapFunction) {
$thrownException = null;
try {
$wrapFunction($testcaseInstance);
}
catch (Predis\AbortedMultiExec $exception) {
$thrownException = $exception;
}
$testcaseInstance->assertType('Predis\AbortedMultiExec', $thrownException);
}
public static function pushTailAndReturn(Predis\Client $client, $keyName, Array $values, $wipeOut = 0) { public static function pushTailAndReturn(Predis\Client $client, $keyName, Array $values, $wipeOut = 0) {
if ($wipeOut == true) { if ($wipeOut == true) {
$client->del($keyName); $client->del($keyName);
+163
View File
@@ -240,6 +240,19 @@ class RedisCommandTestSuite extends PHPUnit_Framework_TestCase {
}); });
} }
function testStrlen() {
$this->redis->set('var', 'foobar');
$this->assertEquals(6, $this->redis->strlen('var'));
$this->assertEquals(9, $this->redis->append('var', '___'));
$this->assertEquals(9, $this->redis->strlen('var'));
RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
$test->redis->rpush('metavars', 'foo');
$test->redis->strlen('metavars');
});
}
/* commands operating on the key space */ /* commands operating on the key space */
function testKeys() { function testKeys() {
@@ -324,6 +337,18 @@ class RedisCommandTestSuite extends PHPUnit_Framework_TestCase {
$this->assertEquals(-1, $this->redis->ttl('foo')); $this->assertEquals(-1, $this->redis->ttl('foo'));
} }
function testPersist() {
$this->redis->set('foo', 'bar');
$this->assertTrue($this->redis->expire('foo', 1));
$this->assertEquals(1, $this->redis->ttl('foo'));
$this->assertTrue($this->redis->persist('foo'));
$this->assertEquals(-1, $this->redis->ttl('foo'));
$this->assertFalse($this->redis->persist('foo'));
$this->assertFalse($this->redis->persist('foobar'));
}
function testSetExpire() { function testSetExpire() {
$this->assertTrue($this->redis->setex('foo', 10, 'bar')); $this->assertTrue($this->redis->setex('foo', 10, 'bar'));
$this->assertTrue($this->redis->exists('foo')); $this->assertTrue($this->redis->exists('foo'));
@@ -367,6 +392,20 @@ class RedisCommandTestSuite extends PHPUnit_Framework_TestCase {
}); });
} }
function testPushTailX() {
$this->assertEquals(0, $this->redis->rpushx('numbers', 1));
$this->assertEquals(1, $this->redis->rpush('numbers', 2));
$this->assertEquals(2, $this->redis->rpushx('numbers', 3));
$this->assertEquals(2, $this->redis->llen('numbers'));
$this->assertEquals(array(2, 3), $this->redis->lrange('numbers', 0, -1));
RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
$test->redis->set('foo', 'bar');
$test->redis->rpushx('foo', 'bar');
});
}
function testPushHead() { function testPushHead() {
// NOTE: List push operations return the list length since Redis commit 520b5a3 // NOTE: List push operations return the list length since Redis commit 520b5a3
$this->assertEquals(1, $this->redis->lpush('metavars', 'foo')); $this->assertEquals(1, $this->redis->lpush('metavars', 'foo'));
@@ -380,6 +419,20 @@ class RedisCommandTestSuite extends PHPUnit_Framework_TestCase {
}); });
} }
function testPushHeadX() {
$this->assertEquals(0, $this->redis->lpushx('numbers', 1));
$this->assertEquals(1, $this->redis->lpush('numbers', 2));
$this->assertEquals(2, $this->redis->lpushx('numbers', 3));
$this->assertEquals(2, $this->redis->llen('numbers'));
$this->assertEquals(array(3, 2), $this->redis->lrange('numbers', 0, -1));
RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
$test->redis->set('foo', 'bar');
$test->redis->lpushx('foo', 'bar');
});
}
function testListLength() { function testListLength() {
$this->assertEquals(1, $this->redis->rpush('metavars', 'foo')); $this->assertEquals(1, $this->redis->rpush('metavars', 'foo'));
$this->assertEquals(2, $this->redis->rpush('metavars', 'hoge')); $this->assertEquals(2, $this->redis->rpush('metavars', 'hoge'));
@@ -680,6 +733,22 @@ class RedisCommandTestSuite extends PHPUnit_Framework_TestCase {
$this->assertEquals((float)(time() - $start), 2, '', 1); $this->assertEquals((float)(time() - $start), 2, '', 1);
} }
function testListInsert() {
$numbers = RC::pushTailAndReturn($this->redis, 'numbers', RC::getArrayOfNumbers());
$this->assertEquals(11, $this->redis->linsert('numbers', 'before', 0, -2));
$this->assertEquals(12, $this->redis->linsert('numbers', 'after', -2, -1));
$this->assertEquals(array(-2, -1, 0, 1), $this->redis->lrange('numbers', 0, 3));
$this->assertEquals(-1, $this->redis->linsert('numbers', 'after', 100, 200));
$this->assertEquals(-1, $this->redis->linsert('numbers', 'before', 100, 50));
RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
$test->redis->set('foo', 'bar');
$test->redis->lset('foo', 0, 0);
});
}
/* commands operating on sets */ /* commands operating on sets */
@@ -1095,6 +1164,11 @@ class RedisCommandTestSuite extends PHPUnit_Framework_TestCase {
$this->redis->zrange('zset', 0, 2, 'withscores') $this->redis->zrange('zset', 0, 2, 'withscores')
); );
$this->assertEquals(
array(array('a', -10), array('b', 0), array('c', 10)),
$this->redis->zrange('zset', 0, 2, array('withscores' => true))
);
RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) { RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
$test->redis->set('foo', 'bar'); $test->redis->set('foo', 'bar');
$test->redis->zrange('foo', 0, -1); $test->redis->zrange('foo', 0, -1);
@@ -1149,6 +1223,11 @@ class RedisCommandTestSuite extends PHPUnit_Framework_TestCase {
$this->redis->zrevrange('zset', 0, 2, 'withscores') $this->redis->zrevrange('zset', 0, 2, 'withscores')
); );
$this->assertEquals(
array(array('f', 30), array('e', 20), array('d', 20)),
$this->redis->zrevrange('zset', 0, 2, array('withscores' => true))
);
RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) { RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
$test->redis->set('foo', 'bar'); $test->redis->set('foo', 'bar');
$test->redis->zrevrange('foo', 0, -1); $test->redis->zrevrange('foo', 0, -1);
@@ -1183,12 +1262,96 @@ class RedisCommandTestSuite extends PHPUnit_Framework_TestCase {
$this->redis->zrangebyscore('zset', 10, 20, 'withscores') $this->redis->zrangebyscore('zset', 10, 20, 'withscores')
); );
$this->assertEquals(
array(array('c', 10), array('d', 20), array('e', 20)),
$this->redis->zrangebyscore('zset', 10, 20, array('withscores' => true))
);
$this->assertEquals(
array('d', 'e'),
$this->redis->zrangebyscore('zset', 10, 20, array('limit' => array(1, 2)))
);
$this->assertEquals(
array('d', 'e'),
$this->redis->zrangebyscore('zset', 10, 20, array(
'limit' => array('offset' => 1, 'count' => 2)
))
);
$this->assertEquals(
array(array('d', 20), array('e', 20)),
$this->redis->zrangebyscore('zset', 10, 20, array(
'limit' => array(1, 2),
'withscores' => true,
))
);
RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) { RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
$test->redis->set('foo', 'bar'); $test->redis->set('foo', 'bar');
$test->redis->zrangebyscore('foo', 0, 0); $test->redis->zrangebyscore('foo', 0, 0);
}); });
} }
function testZsetReverseRangeByScore() {
$zset = RC::zsetAddAndReturn($this->redis, 'zset', RC::getZSetArray());
$this->assertEquals(
array('a'),
$this->redis->zrevrangebyscore('zset', -10, -10)
);
$this->assertEquals(
array('b', 'a'),
$this->redis->zrevrangebyscore('zset', 0, -10)
);
$this->assertEquals(
array('e', 'd'),
$this->redis->zrevrangebyscore('zset', 20, 20)
);
$this->assertEquals(
array('f', 'e', 'd', 'c', 'b'),
$this->redis->zrevrangebyscore('zset', 30, 0)
);
$this->assertEquals(
array(array('e', 20), array('d', 20), array('c', 10)),
$this->redis->zrevrangebyscore('zset', 20, 10, 'withscores')
);
$this->assertEquals(
array(array('e', 20), array('d', 20), array('c', 10)),
$this->redis->zrevrangebyscore('zset', 20, 10, array('withscores' => true))
);
$this->assertEquals(
array('d', 'c'),
$this->redis->zrevrangebyscore('zset', 20, 10, array('limit' => array(1, 2)))
);
$this->assertEquals(
array('d', 'c'),
$this->redis->zrevrangebyscore('zset', 20, 10, array(
'limit' => array('offset' => 1, 'count' => 2)
))
);
$this->assertEquals(
array(array('d', 20), array('c', 10)),
$this->redis->zrevrangebyscore('zset', 20, 10, array(
'limit' => array(1, 2),
'withscores' => true,
))
);
RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
$test->redis->set('foo', 'bar');
$test->redis->zrevrangebyscore('foo', 0, 0);
});
}
function testZsetUnionStore() { function testZsetUnionStore() {
$zsetA = RC::zsetAddAndReturn($this->redis, 'zseta', array('a' => 1, 'b' => 2, 'c' => 3)); $zsetA = RC::zsetAddAndReturn($this->redis, 'zseta', array('a' => 1, 'b' => 2, 'c' => 3));
$zsetB = RC::zsetAddAndReturn($this->redis, 'zsetb', array('b' => 1, 'c' => 2, 'd' => 3)); $zsetB = RC::zsetAddAndReturn($this->redis, 'zsetb', array('b' => 1, 'c' => 2, 'd' => 3));