GEORADIUS_Test.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /*
  3. * This file is part of the Predis package.
  4. *
  5. * (c) Daniele Alessandri <suppakilla@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Predis\Command\Redis;
  11. /**
  12. * @group commands
  13. * @group realm-geospatial
  14. */
  15. class GEORADIUS_Test extends PredisCommandTestCase
  16. {
  17. /**
  18. * {@inheritdoc}
  19. */
  20. protected function getExpectedCommand()
  21. {
  22. return 'Predis\Command\Redis\GEORADIUS';
  23. }
  24. /**
  25. * {@inheritdoc}
  26. */
  27. protected function getExpectedId()
  28. {
  29. return 'GEORADIUS';
  30. }
  31. /**
  32. * @group disconnected
  33. */
  34. public function testFilterArguments()
  35. {
  36. $arguments = array(
  37. 'Sicily', 15, 37, 200, 'km',
  38. 'WITHCOORD', 'WITHDIST', 'WITHHASH', 'COUNT', 1, 'ASC', 'STORE', 'key:store', 'STOREDIST', 'key:storedist',
  39. );
  40. $expected = array(
  41. 'Sicily', 15, 37, 200, 'km',
  42. 'WITHCOORD', 'WITHDIST', 'WITHHASH', 'COUNT', 1, 'ASC', 'STORE', 'key:store', 'STOREDIST', 'key:storedist',
  43. );
  44. $command = $this->getCommand();
  45. $command->setArguments($arguments);
  46. $this->assertSame($expected, $command->getArguments());
  47. }
  48. /**
  49. * @group disconnected
  50. */
  51. public function testFilterArgumentsWithComplexOptions()
  52. {
  53. $arguments = array(
  54. 'Sicily', 15, 37, 200, 'km', array(
  55. 'store' => 'key:store',
  56. 'storedist' => 'key:storedist',
  57. 'withdist' => true,
  58. 'withcoord' => true,
  59. 'withhash' => true,
  60. 'count' => 1,
  61. 'sort' => 'asc',
  62. ),
  63. );
  64. $expected = array(
  65. 'Sicily', 15, 37, 200, 'km',
  66. 'WITHCOORD', 'WITHDIST', 'WITHHASH', 'COUNT', 1, 'ASC', 'STORE', 'key:store', 'STOREDIST', 'key:storedist',
  67. );
  68. $command = $this->getCommand();
  69. $command->setArguments($arguments);
  70. $this->assertSame($expected, $command->getArguments());
  71. }
  72. /**
  73. * @group disconnected
  74. */
  75. public function testFilterArgumentsWithSpecificOptionsSetToFalse()
  76. {
  77. $arguments = array(
  78. 'Sicily', 15, 37, 200, 'km', array(
  79. 'store' => 'key:store',
  80. 'storedist' => 'key:storedist',
  81. 'withdist' => false,
  82. 'withcoord' => false,
  83. 'withhash' => false,
  84. 'count' => 1,
  85. 'sort' => 'asc',
  86. ),
  87. );
  88. $expected = array('Sicily', 15, 37, 200, 'km', 'COUNT', 1, 'ASC', 'STORE', 'key:store', 'STOREDIST', 'key:storedist');
  89. $command = $this->getCommand();
  90. $command->setArguments($arguments);
  91. $this->assertSame($expected, $command->getArguments());
  92. }
  93. /**
  94. * @group disconnected
  95. */
  96. public function testParseResponseWithNoOptions()
  97. {
  98. $raw = array(
  99. array('Palermo', '190.4424'),
  100. array('Catania', '56.4413'),
  101. );
  102. $expected = array(
  103. array('Palermo', '190.4424'),
  104. array('Catania', '56.4413'),
  105. );
  106. $command = $this->getCommand();
  107. $this->assertSame($expected, $command->parseResponse($raw));
  108. }
  109. /**
  110. * @group connected
  111. * @requiresRedisVersion >= 3.2.0
  112. */
  113. public function testCommandReturnsGeoRadiusInfoWithNoOptions()
  114. {
  115. $redis = $this->getClient();
  116. $redis->geoadd('Sicily', '13.361389', '38.115556', 'Palermo', '15.087269', '37.502669', 'Catania');
  117. $this->assertEquals(array('Palermo', 'Catania'), $redis->georadius('Sicily', 15, 37, 200, 'km'));
  118. }
  119. /**
  120. * @group connected
  121. * @requiresRedisVersion >= 3.2.0
  122. */
  123. public function testCommandReturnsGeoRadiusInfoWithOptions()
  124. {
  125. $redis = $this->getClient();
  126. $redis->geoadd('Sicily', '13.361389', '38.115556', 'Palermo', '15.087269', '37.502669', 'Catania');
  127. $this->assertEquals(array(
  128. array('Palermo', '190.4424', array('13.361389338970184', '38.115556395496299')),
  129. array('Catania', '56.4413', array('15.087267458438873', '37.50266842333162')),
  130. ), $redis->georadius('Sicily', 15, 37, 200, 'km', 'WITHDIST', 'WITHCOORD'));
  131. }
  132. /**
  133. * @group connected
  134. * @requiresRedisVersion >= 3.2.0
  135. * @expectedException \Predis\Response\ServerException
  136. * @expectedExceptionMessage Operation against a key holding the wrong kind of value
  137. */
  138. public function testThrowsExceptionOnWrongType()
  139. {
  140. $redis = $this->getClient();
  141. $redis->lpush('Sicily', 'Palermo');
  142. $redis->georadius('Sicily', 15, 37, 200, 'km');
  143. }
  144. }