From fc8f363fe5012e8677bc01a90b7e7f7a5a8f8ca0 Mon Sep 17 00:00:00 2001 From: Daniele Alessandri Date: Tue, 21 Sep 2010 17:33:24 +0200 Subject: [PATCH] New commands: RPUSHX, LPUSHX (Redis v2.2-dev). --- lib/Predis.php | 10 ++++++++++ test/RedisCommandsTest.php | 28 ++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/lib/Predis.php b/lib/Predis.php index 9f1550b3..c7974181 100644 --- a/lib/Predis.php +++ b/lib/Predis.php @@ -1751,6 +1751,8 @@ class RedisServer_vNext extends RedisServer_v2_0 { 'strlen' => '\Predis\Commands\Strlen', /* commands operating on lists */ + 'rpushx' => '\Predis\Commands\ListPushTailX', + 'lpushx' => '\Predis\Commands\ListPushHeadX', 'linsert' => '\Predis\Commands\ListInsert', )); } @@ -2354,10 +2356,18 @@ class ListPushTail extends Command { public function getCommandId() { return 'RPUSH'; } } +class ListPushTailX extends Command { + public function getCommandId() { return 'RPUSHX'; } +} + class ListPushHead extends Command { public function getCommandId() { return 'LPUSH'; } } +class ListPushHeadX extends Command { + public function getCommandId() { return 'LPUSHX'; } +} + class ListLength extends Command { public function getCommandId() { return 'LLEN'; } } diff --git a/test/RedisCommandsTest.php b/test/RedisCommandsTest.php index 4aa0e504..5eabf699 100644 --- a/test/RedisCommandsTest.php +++ b/test/RedisCommandsTest.php @@ -380,6 +380,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() { // NOTE: List push operations return the list length since Redis commit 520b5a3 $this->assertEquals(1, $this->redis->lpush('metavars', 'foo')); @@ -393,6 +407,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() { $this->assertEquals(1, $this->redis->rpush('metavars', 'foo')); $this->assertEquals(2, $this->redis->rpush('metavars', 'hoge'));