1
- <?php defined ('PHPREDIS_TESTRUN ' ) or die ("Use TestRedis.php to run tests! \n" );
1
+ <?php defined ('PHPREDIS_TESTRUN ' ) or die ("Use TestRedis.php to run tests! \n" );
2
2
3
3
require_once (dirname ($ _SERVER ['PHP_SELF ' ])."/TestSuite.php " );
4
4
@@ -7,6 +7,15 @@ class Redis_Test extends TestSuite
7
7
const PORT = 6379 ;
8
8
const AUTH = NULL ; //replace with a string to use Redis authentication
9
9
10
+ /* City lat/long */
11
+ protected $ cities = Array (
12
+ 'Chico ' => Array (-121.837478 , 39.728494 ),
13
+ 'Sacramento ' => Array (-121.494400 , 38.581572 ),
14
+ 'Gridley ' => Array (-121.693583 , 39.363777 ),
15
+ 'Marysville ' => Array (-121.591355 , 39.145725 ),
16
+ 'Cupertino ' => Array (-122.032182 , 37.322998 )
17
+ );
18
+
10
19
/**
11
20
* @var Redis
12
21
*/
@@ -2414,7 +2423,7 @@ public function testMultiExec() {
2414
2423
2415
2424
public function testFailedTransactions () {
2416
2425
$ this ->redis ->set ('x ' , 42 );
2417
-
2426
+
2418
2427
// failed transaction
2419
2428
$ this ->redis ->watch ('x ' );
2420
2429
@@ -2539,7 +2548,7 @@ protected function sequence($mode) {
2539
2548
->ttl ('key ' )
2540
2549
->expireAt ('key ' , '0000 ' )
2541
2550
->exec ();
2542
-
2551
+
2543
2552
$ this ->assertTrue (is_array ($ ret ));
2544
2553
$ i = 0 ;
2545
2554
$ ttl = $ ret [$ i ++];
@@ -4756,6 +4765,136 @@ public function testPFCommands() {
4756
4765
}
4757
4766
}
4758
4767
4768
+ //
4769
+ // GEO* command tests
4770
+ //
4771
+
4772
+ protected function addCities ($ key ) {
4773
+ $ this ->redis ->del ($ key );
4774
+ foreach ($ this ->cities as $ city => $ longlat ) {
4775
+ $ this ->redis ->geoadd ($ key , $ longlat [0 ], $ longlat [1 ], $ city );
4776
+ }
4777
+ }
4778
+
4779
+ /* GEOADD */
4780
+ public function testGeoAdd () {
4781
+ $ this ->redis ->del ('geokey ' );
4782
+
4783
+ /* Add them one at a time */
4784
+ foreach ($ this ->cities as $ city => $ longlat ) {
4785
+ $ this ->assertEquals ($ this ->redis ->geoadd ('geokey ' , $ longlat [0 ], $ longlat [1 ], $ city ), 1 );
4786
+ }
4787
+
4788
+ /* Add them again, all at once */
4789
+ $ args = ['geokey ' ];
4790
+ foreach ($ this ->cities as $ city => $ longlat ) {
4791
+ $ args = array_merge ($ args , Array ($ longlat [0 ], $ longlat [1 ], $ city ));
4792
+ }
4793
+
4794
+ /* They all exist, should be nothing added */
4795
+ $ this ->assertEquals (call_user_func_array (Array ($ this ->redis , 'geoadd ' ), $ args ), 0 );
4796
+ }
4797
+
4798
+ /* GEORADIUS */
4799
+ public function genericGeoRadiusTest ($ cmd ) {
4800
+ /* Chico */
4801
+ $ city = 'Chico ' ;
4802
+ $ lng = -121.837478 ;
4803
+ $ lat = 39.728494 ;
4804
+
4805
+ $ this ->addCities ('gk ' );
4806
+
4807
+ /* Pre tested with redis-cli. We're just verifying proper delivery of distance and unit */
4808
+ if ($ cmd == 'georadius ' ) {
4809
+ $ this ->assertEquals ($ this ->redis ->georadius ('gk ' , $ lng , $ lat , 10 , 'mi ' ), Array ('Chico ' ));
4810
+ $ this ->assertEquals ($ this ->redis ->georadius ('gk ' , $ lng , $ lat , 30 , 'mi ' ), Array ('Gridley ' ,'Chico ' ));
4811
+ $ this ->assertEquals ($ this ->redis ->georadius ('gk ' , $ lng , $ lat , 50 , 'km ' ), Array ('Gridley ' ,'Chico ' ));
4812
+ $ this ->assertEquals ($ this ->redis ->georadius ('gk ' , $ lng , $ lat , 50000 , 'm ' ), Array ('Gridley ' ,'Chico ' ));
4813
+ $ this ->assertEquals ($ this ->redis ->georadius ('gk ' , $ lng , $ lat , 150000 , 'ft ' ), Array ('Gridley ' , 'Chico ' ));
4814
+ $ args = Array ('georadius ' , 'gk ' , $ lng , $ lat , 500 , 'mi ' );
4815
+ } else {
4816
+ $ this ->assertEquals ($ this ->redis ->georadiusbymember ('gk ' , $ city , 10 , 'mi ' ), Array ('Chico ' ));
4817
+ $ this ->assertEquals ($ this ->redis ->georadiusbymember ('gk ' , $ city , 30 , 'mi ' ), Array ('Gridley ' ,'Chico ' ));
4818
+ $ this ->assertEquals ($ this ->redis ->georadiusbymember ('gk ' , $ city , 50 , 'km ' ), Array ('Gridley ' ,'Chico ' ));
4819
+ $ this ->assertEquals ($ this ->redis ->georadiusbymember ('gk ' , $ city , 50000 , 'm ' ), Array ('Gridley ' ,'Chico ' ));
4820
+ $ this ->assertEquals ($ this ->redis ->georadiusbymember ('gk ' , $ city , 150000 , 'ft ' ), Array ('Gridley ' , 'Chico ' ));
4821
+ $ args = Array ('georadiusbymember ' , 'gk ' , $ city , 500 , 'mi ' );
4822
+ }
4823
+
4824
+ /* Options */
4825
+ $ opts = Array ('WITHCOORD ' , 'WITHDIST ' , 'WITHHASH ' );
4826
+ $ sortopts = Array ('' , 'ASC ' , 'DESC ' );
4827
+
4828
+ for ($ i = 0 ; $ i < count ($ opts ); $ i ++) {
4829
+ $ subopts = array_slice ($ opts , 0 , $ i );
4830
+ shuffle ($ subopts );
4831
+
4832
+ $ subargs = $ args ;
4833
+ foreach ($ subopts as $ opt ) {
4834
+ $ subargs [] = $ opt ;
4835
+ }
4836
+
4837
+ for ($ c = 0 ; $ c < 3 ; $ c ++) {
4838
+ /* Add a count if we're past first iteration */
4839
+ if ($ c > 0 ) {
4840
+ $ subopts ['count ' ] = $ c ;
4841
+ $ subargs [] = 'count ' ;
4842
+ $ subargs [] = $ c ;
4843
+ }
4844
+
4845
+ /* Adding optional sort */
4846
+ foreach ($ sortopts as $ sortopt ) {
4847
+ $ realargs = $ subargs ;
4848
+ $ realopts = $ subopts ;
4849
+ if ($ sortopt ) {
4850
+ $ realargs [] = $ sortopt ;
4851
+ $ realopts [] = $ sortopt ;
4852
+ }
4853
+
4854
+ $ ret1 = call_user_func_array (Array ($ this ->redis , 'rawcommand ' ), $ realargs );
4855
+ if ($ cmd == 'georadius ' ) {
4856
+ $ ret2 = $ this ->redis ->$ cmd ('gk ' , $ lng , $ lat , 500 , 'mi ' , $ realopts );
4857
+ } else {
4858
+ $ ret2 = $ this ->redis ->$ cmd ('gk ' , $ city , 500 , 'mi ' , $ realopts );
4859
+ }
4860
+ $ this ->assertEquals ($ ret1 , $ ret2 );
4861
+ }
4862
+ }
4863
+ }
4864
+ }
4865
+
4866
+ public function testGeoRadius () {
4867
+ $ this ->genericGeoRadiusTest ('georadius ' );
4868
+ }
4869
+
4870
+ public function testGeoRadiusByMember () {
4871
+ $ this ->genericGeoRadiusTest ('georadiusbymember ' );
4872
+ }
4873
+
4874
+ public function testGeoPos () {
4875
+ $ this ->addCities ('gk ' );
4876
+ $ this ->assertEquals ($ this ->redis ->geopos ('gk ' , 'Chico ' , 'Sacramento ' ), $ this ->redis ->rawCommand ('geopos ' , 'gk ' , 'Chico ' , 'Sacramento ' ));
4877
+ $ this ->assertEquals ($ this ->redis ->geopos ('gk ' , 'Cupertino ' ), $ this ->redis ->rawCommand ('geopos ' , 'gk ' , 'Cupertino ' ));
4878
+ }
4879
+
4880
+ public function testGeoHash () {
4881
+ $ this ->addCities ('gk ' );
4882
+ $ this ->assertEquals ($ this ->redis ->geohash ('gk ' , 'Chico ' , 'Sacramento ' ), $ this ->redis ->rawCommand ('geohash ' , 'gk ' , 'Chico ' , 'Sacramento ' ));
4883
+ $ this ->assertEquals ($ this ->redis ->geohash ('gk ' , 'Chico ' ), $ this ->redis ->rawCommand ('geohash ' , 'gk ' , 'Chico ' ));
4884
+ }
4885
+
4886
+ public function testGeoDist () {
4887
+ $ this ->addCities ('gk ' );
4888
+
4889
+ $ r1 = $ this ->redis ->geodist ('gk ' , 'Chico ' , 'Cupertino ' );
4890
+ $ r2 = $ this ->redis ->rawCommand ('geodist ' , 'gk ' , 'Chico ' , 'Cupertino ' );
4891
+ $ this ->assertEquals (round ($ r1 , 8 ), round ($ r2 , 8 ));
4892
+
4893
+ $ r1 = $ this ->redis ->geodist ('gk ' , 'Chico ' , 'Cupertino ' , 'km ' );
4894
+ $ r2 = $ this ->redis ->rawCommand ('geodist ' , 'gk ' , 'Chico ' , 'Cupertino ' , 'km ' );
4895
+ $ this ->assertEquals (round ($ r1 , 8 ), round ($ r2 , 8 ));
4896
+ }
4897
+
4759
4898
/* Test a 'raw' command */
4760
4899
public function testRawCommand () {
4761
4900
$ this ->redis ->set ('mykey ' ,'some-value ' );
0 commit comments