Add new command: GEOPOS (Redis 3.2.0).

This commit is contained in:
Daniele Alessandri
2016-05-24 16:04:44 +02:00
parent e96f7f748a
commit 3d9588ec1a
14 changed files with 168 additions and 0 deletions
+1
View File
@@ -159,6 +159,7 @@ use Predis\Command\CommandInterface;
* @method $this command()
* @method $this geoadd($key, $longitude, $latitude, $member)
* @method $this geohash($key, array $members)
* @method $this geopos($key, array $members)
*
* @author Daniele Alessandri <suppakilla@gmail.com>
*/
+1
View File
@@ -167,6 +167,7 @@ use Predis\Profile\ProfileInterface;
* @method array command()
* @method int geoadd($key, $longitude, $latitude, $member)
* @method array geohash($key, array $members)
* @method array geopos($key, array $members)
*
* @author Daniele Alessandri <suppakilla@gmail.com>
*/
+1
View File
@@ -169,6 +169,7 @@ abstract class ClusterStrategy implements StrategyInterface
/* commands performing geospatial operations */
'GEOADD' => $getKeyFromFirstArgument,
'GEOHASH' => $getKeyFromFirstArgument,
'GEOPOS' => $getKeyFromFirstArgument,
);
}
+41
View File
@@ -0,0 +1,41 @@
<?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;
/**
* @link http://redis.io/commands/geopos
*
* @author Daniele Alessandri <suppakilla@gmail.com>
*/
class GeospatialGeoPos extends Command
{
/**
* {@inheritdoc}
*/
public function getId()
{
return 'GEOPOS';
}
/**
* {@inheritdoc}
*/
protected function filterArguments(array $arguments)
{
if (count($arguments) === 2 && is_array($arguments[1])) {
$members = array_pop($arguments);
$arguments = array_merge($arguments, $members);
}
return $arguments;
}
}
@@ -162,6 +162,7 @@ class KeyPrefixProcessor implements ProcessorInterface
'BITFIELD' => 'static::first',
'GEOADD' => 'static::first',
'GEOHASH' => 'static::first',
'GEOPOS' => 'static::first',
);
}
+1
View File
@@ -272,6 +272,7 @@ class RedisVersion320 extends RedisProfile
/* commands performing geospatial operations */
'GEOADD' => 'Predis\Command\GeospatialGeoAdd',
'GEOHASH' => 'Predis\Command\GeospatialGeoHash',
'GEOPOS' => 'Predis\Command\GeospatialGeoPos',
);
}
}
+1
View File
@@ -269,6 +269,7 @@ class ReplicationStrategy
'SORT' => array($this, 'isSortReadOnly'),
'BITFIELD' => array($this, 'isBitfieldReadOnly'),
'GEOHASH' => true,
'GEOPOS' => true,
);
}
}
@@ -399,6 +399,7 @@ class PredisStrategyTest extends PredisTestCase
/* commands performing geospatial operations */
'GEOADD' => 'keys-first',
'GEOHASH' => 'keys-first',
'GEOPOS' => 'keys-first',
);
if (isset($type)) {
@@ -409,6 +409,7 @@ class RedisStrategyTest extends PredisTestCase
/* commands performing geospatial operations */
'GEOADD' => 'keys-first',
'GEOHASH' => 'keys-first',
'GEOPOS' => 'keys-first',
);
if (isset($type)) {
@@ -0,0 +1,112 @@
<?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;
/**
* @group commands
* @group realm-geospatial
*/
class GeospatialGeoPosTest extends PredisCommandTestCase
{
/**
* {@inheritdoc}
*/
protected function getExpectedCommand()
{
return 'Predis\Command\GeospatialGeoPos';
}
/**
* {@inheritdoc}
*/
protected function getExpectedId()
{
return 'GEOPOS';
}
/**
* @group disconnected
*/
public function testFilterArguments()
{
$arguments = array('key', 'member:1', 'member:2');
$expected = array('key', 'member:1', 'member:2');
$command = $this->getCommand();
$command->setArguments($arguments);
$this->assertSame($expected, $command->getArguments());
}
/**
* @group disconnected
*/
public function testFilterArgumentsWithMembersAsSingleArray()
{
$arguments = array('key', array('member:1', 'member:2'));
$expected = array('key', 'member:1', 'member:2');
$command = $this->getCommand();
$command->setArguments($arguments);
$this->assertSame($expected, $command->getArguments());
}
/**
* @group disconnected
*/
public function testParseResponse()
{
$raw = array(
array("13.361389338970184", "38.115556395496299"),
array("15.087267458438873", "37.50266842333162"),
);
$expected = array(
array("13.361389338970184", "38.115556395496299"),
array("15.087267458438873", "37.50266842333162"),
);
$command = $this->getCommand();
$this->assertSame($expected, $command->parseResponse($raw));
}
/**
* @group connected
* @requiresRedisVersion >= 3.2.0
*/
public function testCommandReturnsGeoPositions()
{
$redis = $this->getClient();
$redis->geoadd('Sicily', '13.361389', '38.115556', 'Palermo', '15.087269', '37.502669', 'Catania');
$this->assertEquals(array(
array("13.361389338970184", "38.115556395496299"),
array("15.087267458438873", "37.50266842333162"),
), $redis->geopos('Sicily', 'Palermo', 'Catania'));
}
/**
* @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->geopos('Sicily', 'Palermo');
}
}
@@ -885,6 +885,10 @@ class KeyPrefixProcessorTest extends PredisTestCase
array('key', 'member:1', 'member:2'),
array('prefix:key', 'member:1', 'member:2'),
),
array('GEOPOS',
array('key', 'member:1', 'member:2'),
array('prefix:key', 'member:1', 'member:2'),
),
);
}
}
@@ -193,6 +193,7 @@ class RedisUnstableTest extends PredisProfileTestCase
152 => 'BITFIELD',
153 => 'GEOADD',
154 => 'GEOHASH',
155 => 'GEOPOS',
);
}
}
@@ -193,6 +193,7 @@ class RedisVersion320Test extends PredisProfileTestCase
152 => 'BITFIELD',
153 => 'GEOADD',
154 => 'GEOHASH',
155 => 'GEOPOS',
);
}
}
@@ -442,6 +442,7 @@ class ReplicationStrategyTest extends PredisTestCase
/* commands performing geospatial operations */
'GEOADD' => 'write',
'GEOHASH' => 'read',
'GEOPOS' => 'read',
);
if (isset($type)) {