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,30 +26,21 @@ 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 );
29
+ $ var ->addServer ('127.0.0.1 ' , 11211 );
30
+ $ var ->addServer ('127.0.0.2 ' , 11212 );
39
31
40
32
$ expected = <<<EOTXT
41
33
Memcached {
42
34
servers: array:2 [
43
35
0 => array:3 [
44
- "host" => " $ expectedServerHost1 "
45
- "port" => $ expectedServerPort1
46
- "type" => " $ expectedConnectionType1 "
36
+ "host" => "127.0.0.1 "
37
+ "port" => 11211
38
+ "type" => "TCP "
47
39
]
48
40
1 => array:3 [
49
- "host" => " $ expectedServerHost2 "
50
- "port" => $ expectedServerPort2
51
- "type" => " $ expectedConnectionType2 "
41
+ "host" => "127.0.0.2 "
42
+ "port" => 11212
43
+ "type" => "TCP "
52
44
]
53
45
]
54
46
options: {}
@@ -64,71 +56,27 @@ public function testCastMemcachedWithCustomOptions()
64
56
}
65
57
66
58
$ var = new \Memcached ();
59
+ $ var ->addServer ('127.0.0.1 ' , 11211 );
60
+ $ var ->addServer ('127.0.0.2 ' , 11212 );
67
61
68
- $ expectedServerHost1 = '127.0.0.1 ' ;
69
- $ expectedServerPort1 = 11211 ;
70
- $ expectedConnectionType1 = 'TCP ' ;
71
-
72
- $ expectedServerHost2 = '127.0.0.2 ' ;
73
- $ expectedServerPort2 = 11211 ;
74
- $ expectedConnectionType2 = 'TCP ' ;
75
-
76
- $ var ->addServer ($ expectedServerHost1 , $ expectedServerPort1 );
77
- $ var ->addServer ($ expectedServerHost2 , $ expectedServerPort2 );
78
-
79
- $ var ->setOptions (
80
- array (
81
- \Memcached::OPT_COMPRESSION => false ,
82
- \Memcached::OPT_COMPRESSION_TYPE => false ,
83
- //\Memcached::OPT_SERIALIZER => \Memcached::SERIALIZER_IGBINARY, // throws Memcached::setOptions(): invalid serializer provided
84
- \Memcached::OPT_PREFIX_KEY => 'prefix ' ,
85
- \Memcached::OPT_HASH => \Memcached::HASH_MD5 ,
86
- \Memcached::OPT_DISTRIBUTION => \Memcached::DISTRIBUTION_CONSISTENT ,
87
- \Memcached::OPT_LIBKETAMA_COMPATIBLE => true ,
88
- \Memcached::OPT_BUFFER_WRITES => true ,
89
- \Memcached::OPT_BINARY_PROTOCOL => true ,
90
- \Memcached::OPT_NO_BLOCK => true ,
91
- \Memcached::OPT_TCP_NODELAY => true ,
92
- \Memcached::OPT_SOCKET_SEND_SIZE => 100 , // varies by platform/kernel
93
- \Memcached::OPT_SOCKET_RECV_SIZE => 100 , // varies by platform/kernel
94
- \Memcached::OPT_CONNECT_TIMEOUT => 100 ,
95
- \Memcached::OPT_RETRY_TIMEOUT => 100 ,
96
- \Memcached::OPT_DEAD_TIMEOUT => 100 ,
97
- \Memcached::OPT_SEND_TIMEOUT => 100 ,
98
- \Memcached::OPT_RECV_TIMEOUT => 100 ,
99
- \Memcached::OPT_POLL_TIMEOUT => 100 ,
100
- //\Memcached::OPT_CACHE_LOOKUPS => true, // option deprecated
101
- \Memcached::OPT_SERVER_FAILURE_LIMIT => 1 ,
102
- )
103
- );
104
-
105
- $ expectedOptions = array ();
106
-
107
- $ reflectedMemcached = new \ReflectionClass (\Memcached::class);
62
+ $ var ->setOptions ($ this ->getCustomOptions ());
108
63
109
- foreach ($ reflectedMemcached ->getConstants () as $ constantKey => $ value ) {
110
- if (substr ($ constantKey , 0 , 4 ) === 'OPT_ ' ) {
111
- if (null === $ option = $ this ->getOptionValue ($ var , $ value )) {
112
- continue ;
113
- }
114
- $ expectedOptions [] = ' ' .$ constantKey .': ' .$ this ->getOptionValue ($ var , $ value );
115
- }
116
- }
64
+ $ expectedOptions = $ this ->getExpectedOptions ($ var );
117
65
118
66
$ expectedOptionsAsString = implode (PHP_EOL , $ expectedOptions );
119
67
120
68
$ expected = <<<EOTXT
121
69
Memcached {
122
70
servers: array:2 [
123
71
0 => array:3 [
124
- "host" => " $ expectedServerHost1 "
125
- "port" => $ expectedServerPort1
126
- "type" => " $ expectedConnectionType1 "
72
+ "host" => "127.0.0.1 "
73
+ "port" => 11211
74
+ "type" => "TCP "
127
75
]
128
76
1 => array:3 [
129
- "host" => " $ expectedServerHost2 "
130
- "port" => $ expectedServerPort2
131
- "type" => " $ expectedConnectionType2 "
77
+ "host" => "127.0.0.2 "
78
+ "port" => 11212
79
+ "type" => "TCP "
132
80
]
133
81
]
134
82
options: {
@@ -140,26 +88,58 @@ public function testCastMemcachedWithCustomOptions()
140
88
$ this ->assertDumpEquals ($ expected , $ var );
141
89
}
142
90
143
- private function getOptionValue ( \ Memcached $ c , int $ option )
91
+ private function getCustomOptions ( )
144
92
{
145
- $ defaultMemcached = new \Memcached ();
146
- $ defaultMemcached ->addServer ('127.0.0.1 ' , 11211 );
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
+ );
147
98
148
- $ defaultOption = $ defaultMemcached ->getOption ($ option );
149
- $ currentOption = $ c ->getOption ($ option );
99
+ $ optionConstants = MemcachedCaster::getMemcachedOptionConstants ();
100
+ $ defaultOptions = MemcachedCaster::discoverDefaultMemcachedOptions ();
101
+ $ customOptions = array ();
150
102
151
- if ($ defaultOption === $ currentOption ) {
152
- return null ;
153
- }
103
+ foreach ($ optionConstants as $ optionConstantKey => $ optionConstantValue ) {
104
+ if (\in_array ($ optionConstantValue , $ optionsToIgnore , true )) {
105
+ continue ;
106
+ }
107
+
108
+ $ defaultOptionValue = $ defaultOptions [$ optionConstantKey ];
154
109
155
- if (is_bool ($ currentOption )) {
156
- return $ currentOption ? 'true ' : 'false ' ;
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
+ }
157
121
}
158
122
159
- if (is_string ($ currentOption )) {
160
- return '" ' .$ currentOption .'" ' ;
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 ;
161
141
}
162
142
163
- return $ currentOption ;
143
+ return $ expectedOptions ;
164
144
}
165
145
}
0 commit comments