11
11
12
12
namespace Symfony \Component \Cache \Tests \Adapter ;
13
13
14
+ use Symfony \Component \Cache \Adapter \AbstractAdapter ;
14
15
use Symfony \Component \Cache \Adapter \MemcachedAdapter ;
16
+ use Symfony \Component \Cache \Exception \CacheException ;
15
17
16
18
class MemcachedAdapterTest extends AdapterTestCase
17
19
{
@@ -28,23 +30,140 @@ public static function setupBeforeClass()
28
30
if (!MemcachedAdapter::isSupported ()) {
29
31
self ::markTestSkipped ('Extension memcached >=2.2.0 required. ' );
30
32
}
33
+ self ::$ client = AbstractAdapter::createConnection ('memcached:// ' .getenv ('MEMCACHED_HOST ' ));
34
+ self ::$ client ->get ('foo ' );
35
+ $ code = self ::$ client ->getResultCode ();
31
36
32
- self ::$ client = new \Memcached ();
33
- self ::$ client ->addServers (array (array (
34
- getenv ('MEMCACHED_HOST ' ) ?: '127.0.0.1 ' ,
35
- getenv ('MEMCACHED_PORT ' ) ?: 11211 ,
36
- )));
37
-
38
- parent ::setupBeforeClass ();
37
+ if (\Memcached::RES_SUCCESS !== $ code && \Memcached::RES_NOTFOUND !== $ code ) {
38
+ self ::markTestSkipped ('Memcached error: ' .strtolower (self ::$ client ->getResultMessage ()));
39
+ }
39
40
}
40
41
41
42
public function createCachePool ($ defaultLifetime = 0 )
42
43
{
43
44
return new MemcachedAdapter (self ::$ client , str_replace ('\\' , '. ' , __CLASS__ ), $ defaultLifetime );
44
45
}
45
46
46
- public function testIsSupported ()
47
+ public function testOptions ()
48
+ {
49
+ $ client = MemcachedAdapter::createConnection (array (), array (
50
+ 'libketama_compatible ' => false ,
51
+ 'distribution ' => 'modula ' ,
52
+ 'compression ' => true ,
53
+ 'serializer ' => 'php ' ,
54
+ 'hash ' => 'md5 ' ,
55
+ ));
56
+
57
+ $ this ->assertSame (\Memcached::SERIALIZER_PHP , $ client ->getOption (\Memcached::OPT_SERIALIZER ));
58
+ $ this ->assertSame (\Memcached::HASH_MD5 , $ client ->getOption (\Memcached::OPT_HASH ));
59
+ $ this ->assertTrue ($ client ->getOption (\Memcached::OPT_COMPRESSION ));
60
+ $ this ->assertSame (0 , $ client ->getOption (\Memcached::OPT_LIBKETAMA_COMPATIBLE ));
61
+ $ this ->assertSame (\Memcached::DISTRIBUTION_MODULA , $ client ->getOption (\Memcached::OPT_DISTRIBUTION ));
62
+ }
63
+
64
+ /**
65
+ * @dataProvider provideBadOptions
66
+ * @expectedException \ErrorException
67
+ * @expectedExceptionMessage constant(): Couldn't find constant Memcached::
68
+ */
69
+ public function testBadOptions ($ name , $ value )
70
+ {
71
+ MemcachedAdapter::createConnection (array (), array ($ name => $ value ));
72
+ }
73
+
74
+ public function provideBadOptions ()
75
+ {
76
+ return array (
77
+ array ('foo ' , 'bar ' ),
78
+ array ('hash ' , 'zyx ' ),
79
+ array ('serializer ' , 'zyx ' ),
80
+ array ('distribution ' , 'zyx ' ),
81
+ );
82
+ }
83
+
84
+ /**
85
+ * @dataProvider provideInvalidOptions
86
+ * @expectedException \Symfony\Component\Cache\Exception\CacheException
87
+ */
88
+ public function testInvalidOptions ($ name , $ value , $ expectedMessage )
89
+ {
90
+ $ this ->setExpectedException (CacheException::class, $ expectedMessage );
91
+ new MemcachedAdapter (MemcachedAdapter::createConnection (array (), array ($ name => $ value )));
92
+ }
93
+
94
+ public function provideInvalidOptions ()
95
+ {
96
+ return array (
97
+ array ('binary_protocol ' , false , 'MemcachedAdapter: OPT_BINARY_PROTOCOL must be used. ' ),
98
+ array ('no_block ' , true , 'MemcachedAdapter: OPT_NO_BLOCK must be disabled. ' ),
99
+ );
100
+ }
101
+
102
+ public function testDefaultOptions ()
47
103
{
48
104
$ this ->assertTrue (MemcachedAdapter::isSupported ());
105
+
106
+ $ client = MemcachedAdapter::createConnection (array ());
107
+
108
+ $ this ->assertTrue ($ client ->getOption (\Memcached::OPT_COMPRESSION ));
109
+ $ this ->assertSame (1 , $ client ->getOption (\Memcached::OPT_BINARY_PROTOCOL ));
110
+ $ this ->assertSame (1 , $ client ->getOption (\Memcached::OPT_LIBKETAMA_COMPATIBLE ));
111
+ }
112
+
113
+ /**
114
+ * @dataProvider provideServersSetting
115
+ */
116
+ public function testServersSetting ($ dsn , $ host , $ port )
117
+ {
118
+ $ client1 = MemcachedAdapter::createConnection ($ dsn );
119
+ $ client2 = MemcachedAdapter::createConnection (array ($ dsn ));
120
+ $ client3 = MemcachedAdapter::createConnection (array (array ($ host , $ port )));
121
+ $ expect = array (
122
+ 'host ' => $ host ,
123
+ 'port ' => $ port ,
124
+ );
125
+
126
+ $ f = function ($ s ) { return array ('host ' => $ s ['host ' ], 'port ' => $ s ['port ' ]); };
127
+ $ this ->assertSame (array ($ expect ), array_map ($ f , $ client1 ->getServerList ()));
128
+ $ this ->assertSame (array ($ expect ), array_map ($ f , $ client2 ->getServerList ()));
129
+ $ this ->assertSame (array ($ expect ), array_map ($ f , $ client3 ->getServerList ()));
130
+ }
131
+
132
+ public function provideServersSetting ()
133
+ {
134
+ yield array (
135
+ 'memcached://127.0.0.1/50 ' ,
136
+ '127.0.0.1 ' ,
137
+ 11211 ,
138
+ );
139
+ yield array (
140
+ 'memcached://localhost:11222?weight=25 ' ,
141
+ 'localhost ' ,
142
+ 11222 ,
143
+ );
144
+ if (ini_get ('memcached.use_sasl ' )) {
145
+ yield array (
146
+ 'memcached://user:[email protected] ?weight=50 ' ,
147
+ '127.0.0.1 ' ,
148
+ 11211 ,
149
+ );
150
+ }
151
+ yield array (
152
+ 'memcached:///var/run/memcached.sock?weight=25 ' ,
153
+ '/var/run/memcached.sock ' ,
154
+ 0 ,
155
+ );
156
+ yield array (
157
+ 'memcached:///var/local/run/memcached.socket?weight=25 ' ,
158
+ '/var/local/run/memcached.socket ' ,
159
+ 0 ,
160
+ );
161
+ if (ini_get ('memcached.use_sasl ' )) {
162
+ yield array (
163
+ 'memcached://user:password@/var/local/run/memcached.socket?weight=25 ' ,
164
+ '/var/local/run/memcached.socket ' ,
165
+ 0 ,
166
+ );
167
+ }
49
168
}
50
169
}
0 commit comments