From b2284d015f6d351bc3d42daa28b94c5dbfc9e7d9 Mon Sep 17 00:00:00 2001 From: Daniele Alessandri Date: Tue, 24 May 2016 16:23:07 +0200 Subject: [PATCH] Add new command: GEODIST (Redis 3.2.0). --- src/ClientContextInterface.php | 1 + src/ClientInterface.php | 1 + src/Cluster/ClusterStrategy.php | 1 + src/Command/GeospatialGeoDist.php | 28 ++++++ src/Command/Processor/KeyPrefixProcessor.php | 1 + src/Profile/RedisVersion320.php | 1 + src/Replication/ReplicationStrategy.php | 1 + tests/Predis/Cluster/PredisStrategyTest.php | 1 + tests/Predis/Cluster/RedisStrategyTest.php | 1 + .../Predis/Command/GeospatialGeoDistTest.php | 88 +++++++++++++++++++ .../Processor/KeyPrefixProcessorTest.php | 4 + tests/Predis/Profile/RedisUnstableTest.php | 1 + tests/Predis/Profile/RedisVersion320Test.php | 1 + .../Replication/ReplicationStrategyTest.php | 1 + 14 files changed, 131 insertions(+) create mode 100644 src/Command/GeospatialGeoDist.php create mode 100644 tests/Predis/Command/GeospatialGeoDistTest.php diff --git a/src/ClientContextInterface.php b/src/ClientContextInterface.php index 73a52848..06df7920 100644 --- a/src/ClientContextInterface.php +++ b/src/ClientContextInterface.php @@ -159,6 +159,7 @@ use Predis\Command\CommandInterface; * @method $this geoadd($key, $longitude, $latitude, $member) * @method $this geohash($key, array $members) * @method $this geopos($key, array $members) + * @method $this geodist($key, $member1, $member2, $unit = null) * * @author Daniele Alessandri */ diff --git a/src/ClientInterface.php b/src/ClientInterface.php index 1d7a5da0..bf136598 100644 --- a/src/ClientInterface.php +++ b/src/ClientInterface.php @@ -167,6 +167,7 @@ use Predis\Profile\ProfileInterface; * @method int geoadd($key, $longitude, $latitude, $member) * @method array geohash($key, array $members) * @method array geopos($key, array $members) + * @method string geodist($key, $member1, $member2, $unit = null) * * @author Daniele Alessandri */ diff --git a/src/Cluster/ClusterStrategy.php b/src/Cluster/ClusterStrategy.php index 3180cb39..eec40762 100644 --- a/src/Cluster/ClusterStrategy.php +++ b/src/Cluster/ClusterStrategy.php @@ -170,6 +170,7 @@ abstract class ClusterStrategy implements StrategyInterface 'GEOADD' => $getKeyFromFirstArgument, 'GEOHASH' => $getKeyFromFirstArgument, 'GEOPOS' => $getKeyFromFirstArgument, + 'GEODIST' => $getKeyFromFirstArgument, ); } diff --git a/src/Command/GeospatialGeoDist.php b/src/Command/GeospatialGeoDist.php new file mode 100644 index 00000000..17c5f549 --- /dev/null +++ b/src/Command/GeospatialGeoDist.php @@ -0,0 +1,28 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Predis\Command; + +/** + * @link http://redis.io/commands/geodist + * + * @author Daniele Alessandri + */ +class GeospatialGeoDist extends Command +{ + /** + * {@inheritdoc} + */ + public function getId() + { + return 'GEODIST'; + } +} diff --git a/src/Command/Processor/KeyPrefixProcessor.php b/src/Command/Processor/KeyPrefixProcessor.php index fd217408..6ce6cf9f 100644 --- a/src/Command/Processor/KeyPrefixProcessor.php +++ b/src/Command/Processor/KeyPrefixProcessor.php @@ -163,6 +163,7 @@ class KeyPrefixProcessor implements ProcessorInterface 'GEOADD' => 'static::first', 'GEOHASH' => 'static::first', 'GEOPOS' => 'static::first', + 'GEODIST' => 'static::first', ); } diff --git a/src/Profile/RedisVersion320.php b/src/Profile/RedisVersion320.php index 4e135718..465d7c97 100644 --- a/src/Profile/RedisVersion320.php +++ b/src/Profile/RedisVersion320.php @@ -273,6 +273,7 @@ class RedisVersion320 extends RedisProfile 'GEOADD' => 'Predis\Command\GeospatialGeoAdd', 'GEOHASH' => 'Predis\Command\GeospatialGeoHash', 'GEOPOS' => 'Predis\Command\GeospatialGeoPos', + 'GEODIST' => 'Predis\Command\GeospatialGeoDist', ); } } diff --git a/src/Replication/ReplicationStrategy.php b/src/Replication/ReplicationStrategy.php index 38245c32..4cde9e72 100644 --- a/src/Replication/ReplicationStrategy.php +++ b/src/Replication/ReplicationStrategy.php @@ -260,6 +260,7 @@ class ReplicationStrategy 'BITFIELD' => array($this, 'isBitfieldReadOnly'), 'GEOHASH' => true, 'GEOPOS' => true, + 'GEODIST' => true, ); } } diff --git a/tests/Predis/Cluster/PredisStrategyTest.php b/tests/Predis/Cluster/PredisStrategyTest.php index 68ad69f3..7d4a020c 100644 --- a/tests/Predis/Cluster/PredisStrategyTest.php +++ b/tests/Predis/Cluster/PredisStrategyTest.php @@ -382,6 +382,7 @@ class PredisStrategyTest extends PredisTestCase 'GEOADD' => 'keys-first', 'GEOHASH' => 'keys-first', 'GEOPOS' => 'keys-first', + 'GEODIST' => 'keys-first', ); if (isset($type)) { diff --git a/tests/Predis/Cluster/RedisStrategyTest.php b/tests/Predis/Cluster/RedisStrategyTest.php index 25c9d352..54cb43cd 100644 --- a/tests/Predis/Cluster/RedisStrategyTest.php +++ b/tests/Predis/Cluster/RedisStrategyTest.php @@ -392,6 +392,7 @@ class RedisStrategyTest extends PredisTestCase 'GEOADD' => 'keys-first', 'GEOHASH' => 'keys-first', 'GEOPOS' => 'keys-first', + 'GEODIST' => 'keys-first', ); if (isset($type)) { diff --git a/tests/Predis/Command/GeospatialGeoDistTest.php b/tests/Predis/Command/GeospatialGeoDistTest.php new file mode 100644 index 00000000..33250a3c --- /dev/null +++ b/tests/Predis/Command/GeospatialGeoDistTest.php @@ -0,0 +1,88 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Predis\Command; + +/** + * @group commands + * @group realm-geospatial + */ +class GeospatialGeoDistTest extends PredisCommandTestCase +{ + /** + * {@inheritdoc} + */ + protected function getExpectedCommand() + { + return 'Predis\Command\GeospatialGeoDist'; + } + + /** + * {@inheritdoc} + */ + protected function getExpectedId() + { + return 'GEODIST'; + } + + /** + * @group disconnected + */ + public function testFilterArguments() + { + $arguments = array('key', 'member:1', 'member:2', 'km'); + $expected = array('key', 'member:1', 'member:2', 'km'); + + $command = $this->getCommand(); + $command->setArguments($arguments); + + $this->assertSame($expected, $command->getArguments()); + } + + /** + * @group disconnected + */ + public function testParseResponse() + { + $raw = array('103.31822459492736'); + $expected = array('103.31822459492736'); + + $command = $this->getCommand(); + + $this->assertSame($expected, $command->parseResponse($raw)); + } + + /** + * @group connected + * @requiresRedisVersion >= 3.2.0 + */ + public function testCommandReturnsGeoDistance() + { + $redis = $this->getClient(); + + $redis->geoadd('Sicily', '13.361389', '38.115556', 'Palermo', '15.087269', '37.502669', 'Catania'); + $this->assertSame('166.2742', $redis->geodist('Sicily', 'Palermo', 'Catania', 'km')); + } + + /** + * @group connected + * @requiresRedisVersion >= 3.2.0 + * @expectedException \Predis\Response\ServerException + * @expectedExceptionMessage Operation against a key holding the wrong kind of value + */ + public function testThrowsExceptionOnWrongType() + { + $redis = $this->getClient(); + + $redis->lpush('Sicily', 'Palermo'); + $redis->geodist('Sicily', 'Palermo', 'Catania'); + } +} diff --git a/tests/Predis/Command/Processor/KeyPrefixProcessorTest.php b/tests/Predis/Command/Processor/KeyPrefixProcessorTest.php index d2d2f777..9357c0f0 100644 --- a/tests/Predis/Command/Processor/KeyPrefixProcessorTest.php +++ b/tests/Predis/Command/Processor/KeyPrefixProcessorTest.php @@ -885,6 +885,10 @@ class KeyPrefixProcessorTest extends PredisTestCase array('key', 'member:1', 'member:2'), array('prefix:key', 'member:1', 'member:2'), ), + array('GEODIST', + array('key', 'member:1', 'member:2', 'km'), + array('prefix:key', 'member:1', 'member:2', 'km'), + ), ); } } diff --git a/tests/Predis/Profile/RedisUnstableTest.php b/tests/Predis/Profile/RedisUnstableTest.php index 5e07bccd..cf412282 100644 --- a/tests/Predis/Profile/RedisUnstableTest.php +++ b/tests/Predis/Profile/RedisUnstableTest.php @@ -194,6 +194,7 @@ class RedisUnstableTest extends PredisProfileTestCase 153 => 'GEOADD', 154 => 'GEOHASH', 155 => 'GEOPOS', + 156 => 'GEODIST', ); } } diff --git a/tests/Predis/Profile/RedisVersion320Test.php b/tests/Predis/Profile/RedisVersion320Test.php index d9346ad7..99bff7f2 100644 --- a/tests/Predis/Profile/RedisVersion320Test.php +++ b/tests/Predis/Profile/RedisVersion320Test.php @@ -194,6 +194,7 @@ class RedisVersion320Test extends PredisProfileTestCase 153 => 'GEOADD', 154 => 'GEOHASH', 155 => 'GEOPOS', + 156 => 'GEODIST', ); } } diff --git a/tests/Predis/Replication/ReplicationStrategyTest.php b/tests/Predis/Replication/ReplicationStrategyTest.php index b7cedaf8..ce2dcbbe 100644 --- a/tests/Predis/Replication/ReplicationStrategyTest.php +++ b/tests/Predis/Replication/ReplicationStrategyTest.php @@ -443,6 +443,7 @@ class ReplicationStrategyTest extends PredisTestCase 'GEOADD' => 'write', 'GEOHASH' => 'read', 'GEOPOS' => 'read', + 'GEODIST' => 'read', ); if (isset($type)) {