12
12
namespace Symfony \Component \VarDumper \Tests \Caster ;
13
13
14
14
use PHPUnit \Framework \TestCase ;
15
+ use Symfony \Component \VarDumper \Caster \MemcachedCaster ;
15
16
use Symfony \Component \VarDumper \Test \VarDumperTestTrait ;
16
17
17
18
class MemcachedCasterTest extends TestCase
@@ -25,49 +26,24 @@ public function testCastMemcachedWithDefaultOptions()
25
26
}
26
27
27
28
$ var = new \Memcached ();
28
-
29
- $ expectedServerHost1 = '127.0.0.1 ' ;
30
- $ expectedServerPort1 = 11211 ;
31
- $ expectedConnectionType1 = 'TCP ' ;
32
-
33
- $ expectedServerHost2 = '127.0.0.2 ' ;
34
- $ expectedServerPort2 = 11211 ;
35
- $ expectedConnectionType2 = 'TCP ' ;
36
-
37
- $ var ->addServer ($ expectedServerHost1 , $ expectedServerPort1 );
38
- $ var ->addServer ($ expectedServerHost2 , $ expectedServerPort2 );
39
-
40
- $ expectedOption11 = $ var ->getOption (\Memcached::OPT_SOCKET_SEND_SIZE );
41
- $ expectedOption12 = $ var ->getOption (\Memcached::OPT_SOCKET_RECV_SIZE );
42
- $ expectedOption13 = $ var ->getOption (\Memcached::OPT_CONNECT_TIMEOUT );
43
- $ expectedOption14 = $ var ->getOption (\Memcached::OPT_RETRY_TIMEOUT );
44
- $ expectedOption17 = $ var ->getOption (\Memcached::OPT_POLL_TIMEOUT );
45
- $ expectedOption18 = $ var ->getOption (\Memcached::OPT_CACHE_LOOKUPS );
46
- $ expectedOption19 = $ var ->getOption (\Memcached::OPT_SERVER_FAILURE_LIMIT );
29
+ $ var ->addServer ('127.0.0.1 ' , 11211 );
30
+ $ var ->addServer ('127.0.0.2 ' , 11212 );
47
31
48
32
$ expected = <<<EOTXT
49
33
Memcached {
50
34
servers: array:2 [
51
35
0 => array:3 [
52
- "host" => " $ expectedServerHost1 "
53
- "port" => $ expectedServerPort1
54
- "type" => " $ expectedConnectionType1 "
36
+ "host" => "127.0.0.1 "
37
+ "port" => 11211
38
+ "type" => "TCP "
55
39
]
56
40
1 => array:3 [
57
- "host" => " $ expectedServerHost2 "
58
- "port" => $ expectedServerPort2
59
- "type" => " $ expectedConnectionType2 "
41
+ "host" => "127.0.0.2 "
42
+ "port" => 11212
43
+ "type" => "TCP "
60
44
]
61
45
]
62
- options: {
63
- OPT_SOCKET_SEND_SIZE: $ expectedOption11
64
- OPT_SOCKET_RECV_SIZE: $ expectedOption12
65
- OPT_CONNECT_TIMEOUT: $ expectedOption13
66
- OPT_RETRY_TIMEOUT: $ expectedOption14
67
- OPT_POLL_TIMEOUT: $ expectedOption17
68
- OPT_CACHE_LOOKUPS: $ expectedOption18
69
- OPT_SERVER_FAILURE_LIMIT: $ expectedOption19
70
- }
46
+ options: {}
71
47
}
72
48
EOTXT ;
73
49
$ this ->assertDumpEquals ($ expected , $ var );
@@ -80,97 +56,90 @@ public function testCastMemcachedWithCustomOptions()
80
56
}
81
57
82
58
$ var = new \Memcached ();
59
+ $ var ->addServer ('127.0.0.1 ' , 11211 );
60
+ $ var ->addServer ('127.0.0.2 ' , 11212 );
83
61
84
- $ expectedServerHost1 = '127.0.0.1 ' ;
85
- $ expectedServerPort1 = 11211 ;
86
- $ expectedConnectionType1 = 'TCP ' ;
87
-
88
- $ expectedServerHost2 = '127.0.0.2 ' ;
89
- $ expectedServerPort2 = 11211 ;
90
- $ expectedConnectionType2 = 'TCP ' ;
91
-
92
- $ var ->addServer ($ expectedServerHost1 , $ expectedServerPort1 );
93
- $ var ->addServer ($ expectedServerHost2 , $ expectedServerPort2 );
94
-
95
- $ var ->setOptions (
96
- array (
97
- \Memcached::OPT_COMPRESSION => false ,
98
- //\Memcached::OPT_SERIALIZER => \Memcached::SERIALIZER_IGBINARY, // throws Memcached::setOptions(): invalid serializer provided
99
- \Memcached::OPT_PREFIX_KEY => 'prefix ' ,
100
- \Memcached::OPT_HASH => \Memcached::HASH_MD5 ,
101
- \Memcached::OPT_DISTRIBUTION => \Memcached::DISTRIBUTION_CONSISTENT ,
102
- \Memcached::OPT_LIBKETAMA_COMPATIBLE => true ,
103
- \Memcached::OPT_BUFFER_WRITES => true ,
104
- \Memcached::OPT_BINARY_PROTOCOL => true ,
105
- \Memcached::OPT_NO_BLOCK => true ,
106
- \Memcached::OPT_TCP_NODELAY => true ,
107
- //\Memcached::OPT_SOCKET_SEND_SIZE => 100, // varies by platform/kernel
108
- //\Memcached::OPT_SOCKET_RECV_SIZE => 100, // varies by platform/kernel
109
- \Memcached::OPT_CONNECT_TIMEOUT => 100 ,
110
- \Memcached::OPT_RETRY_TIMEOUT => 100 ,
111
- \Memcached::OPT_SEND_TIMEOUT => 100 ,
112
- \Memcached::OPT_RECV_TIMEOUT => 100 ,
113
- \Memcached::OPT_POLL_TIMEOUT => 100 ,
114
- //\Memcached::OPT_CACHE_LOOKUPS => true, // option deprecated
115
- \Memcached::OPT_SERVER_FAILURE_LIMIT => 1 ,
116
- )
117
- );
62
+ $ var ->setOptions ($ this ->getCustomOptions ());
63
+
64
+ $ expectedOptions = $ this ->getExpectedOptions ($ var );
118
65
119
- $ expectedOption1 = $ var ->getOption (\Memcached::OPT_COMPRESSION ) ? 'true ' : 'false ' ;
120
- $ expectedOption3 = $ var ->getOption (\Memcached::OPT_PREFIX_KEY );
121
- $ expectedOption4 = $ var ->getOption (\Memcached::OPT_HASH );
122
- $ expectedOption5 = $ var ->getOption (\Memcached::OPT_DISTRIBUTION );
123
- $ expectedOption6 = $ var ->getOption (\Memcached::OPT_LIBKETAMA_COMPATIBLE );
124
- $ expectedOption7 = $ var ->getOption (\Memcached::OPT_BUFFER_WRITES );
125
- $ expectedOption8 = $ var ->getOption (\Memcached::OPT_BINARY_PROTOCOL );
126
- $ expectedOption9 = $ var ->getOption (\Memcached::OPT_NO_BLOCK );
127
- $ expectedOption10 = $ var ->getOption (\Memcached::OPT_TCP_NODELAY );
128
- $ expectedOption11 = $ var ->getOption (\Memcached::OPT_SOCKET_SEND_SIZE );
129
- $ expectedOption12 = $ var ->getOption (\Memcached::OPT_SOCKET_RECV_SIZE );
130
- $ expectedOption13 = $ var ->getOption (\Memcached::OPT_CONNECT_TIMEOUT );
131
- $ expectedOption14 = $ var ->getOption (\Memcached::OPT_RETRY_TIMEOUT );
132
- $ expectedOption15 = $ var ->getOption (\Memcached::OPT_SEND_TIMEOUT );
133
- $ expectedOption16 = $ var ->getOption (\Memcached::OPT_RECV_TIMEOUT );
134
- $ expectedOption17 = $ var ->getOption (\Memcached::OPT_POLL_TIMEOUT );
135
- $ expectedOption18 = $ var ->getOption (\Memcached::OPT_CACHE_LOOKUPS );
136
- $ expectedOption19 = $ var ->getOption (\Memcached::OPT_SERVER_FAILURE_LIMIT );
66
+ $ expectedOptionsAsString = implode (PHP_EOL , $ expectedOptions );
137
67
138
68
$ expected = <<<EOTXT
139
69
Memcached {
140
70
servers: array:2 [
141
71
0 => array:3 [
142
- "host" => " $ expectedServerHost1 "
143
- "port" => $ expectedServerPort1
144
- "type" => " $ expectedConnectionType1 "
72
+ "host" => "127.0.0.1 "
73
+ "port" => 11211
74
+ "type" => "TCP "
145
75
]
146
76
1 => array:3 [
147
- "host" => " $ expectedServerHost2 "
148
- "port" => $ expectedServerPort2
149
- "type" => " $ expectedConnectionType2 "
77
+ "host" => "127.0.0.2 "
78
+ "port" => 11212
79
+ "type" => "TCP "
150
80
]
151
81
]
152
82
options: {
153
- OPT_COMPRESSION: $ expectedOption1
154
- OPT_PREFIX_KEY: " $ expectedOption3"
155
- OPT_HASH: $ expectedOption4
156
- OPT_DISTRIBUTION: $ expectedOption5
157
- OPT_LIBKETAMA_COMPATIBLE: $ expectedOption6
158
- OPT_BUFFER_WRITES: $ expectedOption7
159
- OPT_BINARY_PROTOCOL: $ expectedOption8
160
- OPT_NO_BLOCK: $ expectedOption9
161
- OPT_TCP_NODELAY: $ expectedOption10
162
- OPT_SOCKET_SEND_SIZE: $ expectedOption11
163
- OPT_SOCKET_RECV_SIZE: $ expectedOption12
164
- OPT_CONNECT_TIMEOUT: $ expectedOption13
165
- OPT_RETRY_TIMEOUT: $ expectedOption14
166
- OPT_SEND_TIMEOUT: $ expectedOption15
167
- OPT_RECV_TIMEOUT: $ expectedOption16
168
- OPT_POLL_TIMEOUT: $ expectedOption17
169
- OPT_CACHE_LOOKUPS: $ expectedOption18
170
- OPT_SERVER_FAILURE_LIMIT: $ expectedOption19
83
+ $ expectedOptionsAsString
171
84
}
172
85
}
173
86
EOTXT ;
87
+
174
88
$ this ->assertDumpEquals ($ expected , $ var );
175
89
}
90
+
91
+ private function getCustomOptions ()
92
+ {
93
+ $ optionsToIgnore = array (
94
+ \Memcached::OPT_SERIALIZER , // trying to set this option to something else then default throws Memcached::setOptions(): invalid serializer provided
95
+ \Memcached::OPT_CACHE_LOOKUPS , // option is deprecated
96
+ \Memcached::OPT_VERIFY_KEY , // trying to set this option throws Memcached::setOption(): error setting memcached option: INVALID ARGUMENTS
97
+ );
98
+
99
+ $ optionConstants = MemcachedCaster::getMemcachedOptionConstants ();
100
+ $ defaultOptions = MemcachedCaster::discoverDefaultMemcachedOptions ();
101
+ $ customOptions = array ();
102
+
103
+ foreach ($ optionConstants as $ optionConstantKey => $ optionConstantValue ) {
104
+ if (\in_array ($ optionConstantValue , $ optionsToIgnore , true )) {
105
+ continue ;
106
+ }
107
+
108
+ $ defaultOptionValue = $ defaultOptions [$ optionConstantKey ];
109
+
110
+ if (\is_bool ($ defaultOptionValue )) {
111
+ $ customOptions [$ optionConstants [$ optionConstantKey ]] = !$ defaultOptionValue ;
112
+ }
113
+
114
+ if (\is_string ($ defaultOptionValue )) {
115
+ $ customOptions [$ optionConstants [$ optionConstantKey ]] = 'custom ' ;
116
+ }
117
+
118
+ if (\is_int ($ defaultOptionValue )) {
119
+ $ customOptions [$ optionConstants [$ optionConstantKey ]] = $ defaultOptionValue + 1 ;
120
+ }
121
+ }
122
+
123
+ return $ customOptions ;
124
+ }
125
+
126
+ private function getExpectedOptions (\Memcached $ memcached )
127
+ {
128
+ $ expectedOptions = array ();
129
+ $ nonDefaultOptions = MemcachedCaster::getMemcachedNonDefaultValueOptions ($ memcached );
130
+
131
+ foreach ($ nonDefaultOptions as $ key => $ value ) {
132
+ if (\is_string ($ value )) {
133
+ $ value = '" ' .$ value .'" ' ;
134
+ }
135
+
136
+ if (\is_bool ($ value )) {
137
+ $ value = $ value ? 'true ' : 'false ' ;
138
+ }
139
+
140
+ $ expectedOptions [] = ' ' .$ key .': ' .$ value ;
141
+ }
142
+
143
+ return $ expectedOptions ;
144
+ }
176
145
}
0 commit comments