@@ -2884,6 +2884,253 @@ private function checkSerializer($mode) {
2884
2884
$ this ->assertTrue ($ this ->redis ->setOption (Redis::OPT_SERIALIZER , Redis::SERIALIZER_NONE ) === TRUE ); // set ok
2885
2885
$ this ->assertTrue ($ this ->redis ->getOption (Redis::OPT_SERIALIZER ) === Redis::SERIALIZER_NONE ); // get ok
2886
2886
}
2887
+
2888
+ public function testDumpRestore () {
2889
+ $ this ->redis ->del ('foo ' );
2890
+ $ this ->redis ->del ('bar ' );
2891
+
2892
+ $ this ->redis ->set ('foo ' , 'this-is-foo ' );
2893
+ $ this ->redis ->set ('bar ' , 'this-is-bar ' );
2894
+
2895
+ $ d_foo = $ this ->redis ->dump ('foo ' );
2896
+ $ d_bar = $ this ->redis ->dump ('bar ' );
2897
+
2898
+ $ this ->redis ->del ('foo ' );
2899
+ $ this ->redis ->del ('bar ' );
2900
+
2901
+ // Assert returns from restore
2902
+ $ this ->assertTrue ($ this ->redis ->restore ('foo ' , 0 , $ d_bar ));
2903
+ $ this ->assertTrue ($ this ->redis ->restore ('bar ' , 0 , $ d_foo ));
2904
+
2905
+ // Now check that the keys have switched
2906
+ $ this ->assertTrue ($ this ->redis ->get ('foo ' ) == 'this-is-bar ' );
2907
+ $ this ->assertTrue ($ this ->redis ->get ('bar ' ) == 'this-is-foo ' );
2908
+
2909
+ $ this ->redis ->del ('foo ' );
2910
+ $ this ->redis ->del ('bar ' );
2911
+ }
2912
+
2913
+ public function testGetLastError () {
2914
+ // We shouldn't have any errors now
2915
+ $ this ->assertTrue ($ this ->redis ->getLastError () == NULL );
2916
+
2917
+ // Throw some invalid lua at redis
2918
+ $ this ->redis ->eval ("not-a-lua-script " );
2919
+
2920
+ // Now we should have an error
2921
+ $ this ->assertTrue (strlen ($ this ->redis ->getLastError ()) > 0 );
2922
+ }
2923
+
2924
+ // Helper function to compare nested results -- from the php.net array_diff page, I believe
2925
+ private function array_diff_recursive ($ aArray1 , $ aArray2 ) {
2926
+ $ aReturn = array ();
2927
+
2928
+ foreach ($ aArray1 as $ mKey => $ mValue ) {
2929
+ if (array_key_exists ($ mKey , $ aArray2 )) {
2930
+ if (is_array ($ mValue )) {
2931
+ $ aRecursiveDiff = $ this ->array_diff_recursive ($ mValue , $ aArray2 [$ mKey ]);
2932
+ if (count ($ aRecursiveDiff )) {
2933
+ $ aReturn [$ mKey ] = $ aRecursiveDiff ;
2934
+ }
2935
+ } else {
2936
+ if ($ mValue != $ aArray2 [$ mKey ]) {
2937
+ $ aReturn [$ mKey ] = $ mValue ;
2938
+ }
2939
+ }
2940
+ } else {
2941
+ $ aReturn [$ mKey ] = $ mValue ;
2942
+ }
2943
+ }
2944
+
2945
+ return $ aReturn ;
2946
+ }
2947
+
2948
+ public function testScript () {
2949
+ // Flush any scripts we have
2950
+ $ this ->assertTrue ($ this ->redis ->script ('flush ' ));
2951
+
2952
+ // Silly scripts to test against
2953
+ $ s1_src = 'return 1 ' ;
2954
+ $ s1_sha = sha1 ($ s1_src );
2955
+ $ s2_src = 'return 2 ' ;
2956
+ $ s2_sha = sha1 ($ s2_src );
2957
+ $ s3_src = 'return 3 ' ;
2958
+ $ s3_sha = sha1 ($ s3_src );
2959
+
2960
+ // None should exist
2961
+ $ result = $ this ->redis ->script ('exists ' , $ s1_sha , $ s2_sha , $ s3_sha );
2962
+ $ this ->assertTrue (is_array ($ result ) && count ($ result ) == 3 );
2963
+ $ this ->assertTrue (count (array_filter ($ result )) == 0 );
2964
+
2965
+ // Load them up
2966
+ $ this ->assertTrue ($ this ->redis ->script ('load ' , $ s1_src ) == $ s1_sha );
2967
+ $ this ->assertTrue ($ this ->redis ->script ('load ' , $ s2_src ) == $ s2_sha );
2968
+ $ this ->assertTrue ($ this ->redis ->script ('load ' , $ s3_src ) == $ s3_sha );
2969
+
2970
+ // They should all exist
2971
+ $ result = $ this ->redis ->script ('exists ' , $ s1_sha , $ s2_sha , $ s3_sha );
2972
+ $ this ->assertTrue (count (array_filter ($ result )) == 3 );
2973
+ }
2974
+
2975
+ public function testEval () {
2976
+ // Basic single line response tests
2977
+ $ this ->assertTrue (1 == $ this ->redis ->eval ('return 1 ' ));
2978
+ $ this ->assertTrue (1.55 == $ this ->redis ->eval ("return '1.55' " ));
2979
+ $ this ->assertTrue ("hello, world " == $ this ->redis ->eval ("return 'hello, world' " ));
2980
+
2981
+ /*
2982
+ * Keys to be incorporated into lua results
2983
+ */
2984
+
2985
+ // Make a list
2986
+ $ this ->redis ->del ('mylist ' );
2987
+ $ this ->redis ->rpush ('mylist ' , 'a ' );
2988
+ $ this ->redis ->rpush ('mylist ' , 'b ' );
2989
+ $ this ->redis ->rpush ('mylist ' , 'c ' );
2990
+
2991
+ // Make a set
2992
+ $ this ->redis ->del ('myset ' );
2993
+ $ this ->redis ->sadd ('myset ' , 'd ' );
2994
+ $ this ->redis ->sadd ('myset ' , 'e ' );
2995
+ $ this ->redis ->sadd ('myset ' , 'f ' );
2996
+
2997
+ // Basic keys
2998
+ $ this ->redis ->del ('key1 ' );
2999
+ $ this ->redis ->set ('key1 ' , 'hello, world ' );
3000
+ $ this ->redis ->del ('key2 ' );
3001
+ $ this ->redis ->set ('key2 ' , 'hello again! ' );
3002
+
3003
+ // Use a script to return our list, and verify its response
3004
+ $ list = $ this ->redis ->eval ("return redis.call('lrange', 'mylist', 0, -1) " );
3005
+ $ this ->assertTrue ($ list === Array ('a ' ,'b ' ,'c ' ));
3006
+
3007
+ // Use a script to return our set
3008
+ $ set = $ this ->redis ->eval ("return redis.call('smembers', 'myset') " );
3009
+ $ this ->assertTrue ($ set == Array ('d ' ,'e ' ,'f ' ));
3010
+
3011
+ // Test an empty MULTI BULK response
3012
+ $ empty_resp = $ this ->redis ->eval ("return redis.call('lrange', 'not-any-kind-of-set', 0, -1) " );
3013
+ $ this ->assertTrue (is_array ($ empty_resp ) && empty ($ empty_resp ));
3014
+
3015
+ // Now test a nested reply
3016
+ $ nested_script = "
3017
+ return {
3018
+ 1,2,3, {
3019
+ redis.call('get', 'key1'),
3020
+ redis.call('get', 'key2'),
3021
+ redis.call('lrange', 'not-any-kind-of-list', 0, -1),
3022
+ {
3023
+ redis.call('smembers','myset'),
3024
+ redis.call('lrange', 'mylist', 0, -1)
3025
+ }
3026
+ }
3027
+ }
3028
+ " ;
3029
+
3030
+ $ expected = Array (
3031
+ 1 , 2 , 3 , Array (
3032
+ 'hello, world ' ,
3033
+ 'hello again! ' ,
3034
+ Array (),
3035
+ Array (
3036
+ Array ('d ' ,'e ' ,'f ' ),
3037
+ Array ('a ' ,'b ' ,'c ' )
3038
+ )
3039
+ )
3040
+ );
3041
+
3042
+ // Now run our script, and check our values against each other
3043
+ $ eval_result = $ this ->redis ->eval ($ nested_script );
3044
+ $ this ->assertTrue (count ($ this ->array_diff_recursive ($ eval_result , $ expected )) == 0 );
3045
+
3046
+ /*
3047
+ * KEYS/ARGV
3048
+ */
3049
+
3050
+ $ args_script = "return {KEYS[1],KEYS[2],KEYS[3],ARGV[1],ARGV[2],ARGV[3]} " ;
3051
+ $ args_args = Array ('k1 ' ,'k2 ' ,'k3 ' ,'v1 ' ,'v2 ' ,'v3 ' );
3052
+ $ args_result = $ this ->redis ->eval ($ args_script , $ args_args , 3 );
3053
+ $ this ->assertTrue ($ args_result === $ args_args );
3054
+
3055
+ // turn on key prefixing
3056
+ $ this ->redis ->setOption (Redis::OPT_PREFIX , 'prefix: ' );
3057
+ $ args_result = $ this ->redis ->eval ($ args_script , $ args_args , 3 );
3058
+
3059
+ // Make sure our first three are prefixed
3060
+ for ($ i =0 ;$ i <count ($ args_result );$ i ++) {
3061
+ if ($ i <3 ) {
3062
+ // Should be prefixed
3063
+ $ this ->assertTrue ($ args_result [$ i ] == 'prefix: ' . $ args_args [$ i ]);
3064
+ } else {
3065
+ // Should not be prefixed
3066
+ $ this ->assertTrue ($ args_result [$ i ] == $ args_args [$ i ]);
3067
+ }
3068
+ }
3069
+ }
3070
+
3071
+ public function testEvalSHA () {
3072
+ // Flush any loaded scripts
3073
+ $ this ->redis ->script ('flush ' );
3074
+
3075
+ // Non existant script (but proper sha1), and a random (not) sha1 string
3076
+ $ this ->assertFalse ($ this ->redis ->evalsha (sha1 (uniqid ())));
3077
+ $ this ->assertFalse ($ this ->redis ->evalsha ('some-random-data ' ));
3078
+
3079
+ // Load a script
3080
+ $ cb = uniqid ();
3081
+ $ scr = "local cb=' $ cb' return 1 " ;
3082
+ $ sha = sha1 ($ scr );
3083
+
3084
+ // Run it when it doesn't exist, run it with eval, and then run it with sha1
3085
+ $ this ->assertTrue (false == $ this ->redis ->evalsha ($ scr ));
3086
+ $ this ->assertTrue (1 == $ this ->redis ->eval ($ scr ));
3087
+ $ this ->assertTrue (1 == $ this ->redis ->evalsha ($ sha ));
3088
+ }
3089
+
3090
+ public function testUnserialize () {
3091
+ $ vals = Array (
3092
+ 1 ,1.5 ,'one ' ,Array ('this ' ,'is ' ,'an ' ,'array ' )
3093
+ );
3094
+
3095
+ foreach (Array (Redis::SERIALIZER_PHP , Redis::SERIALIZER_IGBINARY ) as $ mode ) {
3096
+ $ vals_enc = Array ();
3097
+
3098
+ // Pass them through redis so they're serialized
3099
+ foreach ($ vals as $ key => $ val ) {
3100
+ $ this ->redis ->setOption (Redis::OPT_SERIALIZER , $ mode );
3101
+
3102
+ $ key = "key " . ++$ key ;
3103
+ $ this ->redis ->del ($ key );
3104
+ $ this ->redis ->set ($ key , $ val );
3105
+
3106
+ // Clear serializer, get serialized value
3107
+ $ this ->redis ->setOption (Redis::OPT_SERIALIZER , Redis::SERIALIZER_NONE );
3108
+ $ vals_enc [] = $ this ->redis ->get ($ key );
3109
+ }
3110
+
3111
+ // Run through our array comparing values
3112
+ for ($ i =0 ;$ i <count ($ vals );$ i ++) {
3113
+ // reset serializer
3114
+ $ this ->redis ->setOption (Redis::OPT_SERIALIZER , $ mode );
3115
+ $ this ->assertTrue ($ vals [$ i ] == $ this ->redis ->_unserialize ($ vals_enc [$ i ]));
3116
+ $ this ->redis ->setOption (Redis::OPT_SERIALIZER , Redis::SERIALIZER_NONE );
3117
+ }
3118
+ }
3119
+ }
3120
+
3121
+ public function testPrefix () {
3122
+ // no prefix
3123
+ $ this ->redis ->setOption (Redis::OPT_PREFIX , '' );
3124
+ $ this ->assertTrue ('key ' == $ this ->redis ->_prefix ('key ' ));
3125
+
3126
+ // with a prefix
3127
+ $ this ->redis ->setOption (Redis::OPT_PREFIX , 'some-prefix: ' );
3128
+ $ this ->assertTrue ('some-prefix:key ' == $ this ->redis ->_prefix ('key ' ));
3129
+
3130
+ // Clear prefix
3131
+ $ this ->redis ->setOption (Redis::OPT_PREFIX , '' );
3132
+
3133
+ }
2887
3134
}
2888
3135
2889
3136
TestSuite::run ("Redis_Test " );
0 commit comments